From a2bef12ce094bc6edca35ab929dfc358fa3db148 Mon Sep 17 00:00:00 2001 From: Olof Hagsand Date: Tue, 17 Nov 2020 07:35:50 -0600 Subject: [PATCH] strncpy warnings fixed: stringop-truncation and stringop-overflow --- apps/cli/cli_common.c | 15 +++++++++++---- lib/clixon/clixon_string.h | 2 +- lib/src/clixon_err.c | 4 ++-- lib/src/clixon_proto.c | 32 +++++++++++++++++++------------- 4 files changed, 33 insertions(+), 20 deletions(-) diff --git a/apps/cli/cli_common.c b/apps/cli/cli_common.c index f04113e5..54d6fc6f 100644 --- a/apps/cli/cli_common.c +++ b/apps/cli/cli_common.c @@ -648,14 +648,14 @@ compare_xmls(cxobj *xc1, FILE *f; char filename1[MAXPATHLEN]; char filename2[MAXPATHLEN]; - char cmd[MAXPATHLEN]; int retval = -1; cxobj *xc; + cbuf *cb = NULL; snprintf(filename1, sizeof(filename1), "/tmp/cliconXXXXXX"); snprintf(filename2, sizeof(filename2), "/tmp/cliconXXXXXX"); if ((fd = mkstemp(filename1)) < 0){ - clicon_err(OE_UNDEF, errno, "tmpfile: %s", strerror (errno)); + clicon_err(OE_UNDEF, errno, "tmpfile"); goto done; } if ((f = fdopen(fd, "w")) == NULL) @@ -687,12 +687,19 @@ compare_xmls(cxobj *xc1, fclose(f); close(fd); - snprintf(cmd, sizeof(cmd), "/usr/bin/diff -dU 1 %s %s | grep -v @@ | sed 1,2d", filename1, filename2); - if (system(cmd) < 0) + if ((cb = cbuf_new()) == NULL){ + clicon_err(OE_CFG, errno, "cbuf_new"); + goto done; + } + cprintf(cb, "/usr/bin/diff -dU 1 %s %s | grep -v @@ | sed 1,2d", + filename1, filename2); + if (system(cbuf_get(cb)) < 0) goto done; retval = 0; done: + if (cb) + cbuf_free(cb); unlink(filename1); unlink(filename2); return retval; diff --git a/lib/clixon/clixon_string.h b/lib/clixon/clixon_string.h index c35c2e5a..d941f774 100644 --- a/lib/clixon/clixon_string.h +++ b/lib/clixon/clixon_string.h @@ -80,7 +80,7 @@ static inline char * strdup4(char *str) len = align4(strlen(str)+1); if ((dup = (char*) malloc(len)) == NULL) return NULL; - strncpy(dup, str, len); + memcpy(dup, str, strlen(str)+1); return dup; } diff --git a/lib/src/clixon_err.c b/lib/src/clixon_err.c index 73a0ab47..c91f32f2 100644 --- a/lib/src/clixon_err.c +++ b/lib/src/clixon_err.c @@ -224,7 +224,7 @@ clicon_err_save(void) return NULL; es->es_errno = clicon_errno; es->es_suberrno = clicon_suberrno; - strncpy(es->es_reason, clicon_err_reason, ERR_STRLEN-1); + strncpy(es->es_reason, clicon_err_reason, ERR_STRLEN); return (void*)es; } @@ -238,7 +238,7 @@ clicon_err_restore(void* handle) if ((es = (struct err_state *)handle) != NULL){ clicon_errno = es->es_errno; clicon_suberrno = es->es_suberrno; - strncpy(clicon_err_reason, es->es_reason, ERR_STRLEN-1); + strncpy(clicon_err_reason, es->es_reason, ERR_STRLEN); free(es); } return 0; diff --git a/lib/src/clixon_proto.c b/lib/src/clixon_proto.c index 930fdf61..3ebf3a5c 100644 --- a/lib/src/clixon_proto.c +++ b/lib/src/clixon_proto.c @@ -292,26 +292,32 @@ atomicio(ssize_t (*fn) (int, void *, size_t), static int msg_dump(struct clicon_msg *msg) { - int i; - char buf[9*8+1]; - char buf2[9*8+1]; + int retval = -1; + cbuf *cb = NULL; + int i; - memset(buf2, 0, sizeof(buf2)); - snprintf(buf2, sizeof(buf2), "%s:", __FUNCTION__); + if ((cb = cbuf_new()) == NULL){ + clicon_err(OE_CFG, errno, "cbuf_new"); + goto done; + } + cprintf(cb, "%s:", __FUNCTION__); for (i=0; iop_len); i++){ - snprintf(buf, sizeof(buf), "%s%02x", buf2, ((char*)msg)[i]&0xff); + cprintf(cb, "%02x", ((char*)msg)[i]&0xff); if ((i+1)%32==0){ - clicon_debug(2, "%s", buf); - snprintf(buf, sizeof(buf), "%s:", __FUNCTION__); + clicon_debug(2, "%s", cbuf_get(cb)); + cbuf_reset(cb); + cprintf(cb, "%s:", __FUNCTION__); } else if ((i+1)%4==0) - snprintf(buf, sizeof(buf), "%s ", buf2); - strncpy(buf2, buf, sizeof(buf2)); + cprintf(cb, " "); } - if (i%32) - clicon_debug(2, "%s", buf); - return 0; + clicon_debug(2, "%s", cbuf_get(cb)); + retval = 0; + done: + if (cb) + cbuf_free(cb); + return retval; } /*! Send a CLICON netconf message