From: David Levine Date: Sun, 15 Mar 2020 21:18:58 +0000 (-0400) Subject: Refined number of bytes copied to buffer in several places. X-Git-Url: https://diplodocus.org/git/nmh/commitdiff_plain/361f9cbca968760822f53a80b407f24ce1713ccd?hp=283d1d6f7d1e3ef16af638b4b943e2386330e68e Refined number of bytes copied to buffer in several places. This fixed "output may be truncated writing up to [m] bytes into a region of size [n]" warnings found by gcc -Wformat-truncation. --- diff --git a/test/fakesmtp.c b/test/fakesmtp.c index c4fa3e7c..8050e553 100644 --- a/test/fakesmtp.c +++ b/test/fakesmtp.c @@ -14,6 +14,10 @@ #include #include +#ifndef min +# define min(a,b) ((a) < (b) ? (a) : (b)) +#endif + #define PIDFILE "/tmp/fakesmtp.pid" #define LINESIZE 1024 @@ -155,9 +159,9 @@ getsmtp(int socket, char *data) if (bytesinbuf > 0 && (p = strchr(buffer, '\r')) && *(p + 1) == '\n') { *p = '\0'; - strncpy(data, buffer, LINESIZE); - data[LINESIZE - 1] = '\0'; cc = strlen(buffer); + memcpy(data, buffer, min(cc + 1, LINESIZE)); + data[LINESIZE - 1] = '\0'; /* * Shuffle leftover bytes back to the beginning diff --git a/uip/dropsbr.c b/uip/dropsbr.c index 630de111..4a6bb7a0 100644 --- a/uip/dropsbr.c +++ b/uip/dropsbr.c @@ -247,10 +247,12 @@ mbx_copy (char *mailbox, int mbx_style, int md, int fd, * If there is already a "From " line, * then leave it alone. Else we add one. */ - char tmpbuffer[sizeof buffer-7]; + char tmpbuffer[sizeof buffer-21]; char *tp, *ep; - strncpy(tmpbuffer, buffer, sizeof(tmpbuffer)); + (void) strncpy(tmpbuffer, buffer, + min(strlen(buffer) + 1, + sizeof(tmpbuffer) - 1)); ep = "nobody@nowhere"; tp = dctime(dlocaltimenow()); snprintf (buffer, sizeof(buffer), "From %s %s%s", ep, tp, tmpbuffer); diff --git a/uip/mkstemp.c b/uip/mkstemp.c index b7fa5ae0..7a653ea8 100644 --- a/uip/mkstemp.c +++ b/uip/mkstemp.c @@ -88,8 +88,8 @@ build_template(const char *directory, const char *prefix, const char *suffix) prefix_len = strlen(prefix); suffix_len = strlen(suffix); /* sizeof pattern includes its final NULL, so don't add another. */ - len = directory_len + pathsep_len + prefix_len + sizeof pattern + - suffix_len; + len = + directory_len + pathsep_len + prefix_len + sizeof pattern + suffix_len; if ((template = malloc(len))) { char *tp = template; @@ -102,7 +102,7 @@ build_template(const char *directory, const char *prefix, const char *suffix) (void) strncpy(tp, prefix, prefix_len); tp += prefix_len; - (void) strncpy(tp, pattern, sizeof pattern - 1); + (void) memcpy(tp, pattern, sizeof pattern); tp += sizeof pattern - 1; (void) strncpy(tp, suffix, suffix_len); diff --git a/uip/popsbr.c b/uip/popsbr.c index c1d520e9..3b606334 100644 --- a/uip/popsbr.c +++ b/uip/popsbr.c @@ -703,7 +703,8 @@ multiline (void) strncpy (response, buffer + LEN(TRM), sizeof(response)); } else - strncpy (response, buffer, sizeof(response)); + strncpy (response, buffer, + min (strlen(buffer) + 1, sizeof(response) - 1)); return OK; }