From cbe1d3b6307020778153ec838f67bbcd9f5b46b0 Mon Sep 17 00:00:00 2001 From: Brian Somers Date: Mon, 20 Aug 2001 14:46:40 +0000 Subject: [PATCH] Handle snprintf() returning -1. MFC after: 2 weeks --- usr.bin/apply/apply.c | 8 ++++---- usr.bin/ftp/fetch.c | 2 +- usr.bin/ftp/util.c | 38 ++++++++++++++++++++++++++------------ usr.bin/mail/fio.c | 2 +- 4 files changed, 32 insertions(+), 18 deletions(-) diff --git a/usr.bin/apply/apply.c b/usr.bin/apply/apply.c index 1fc479634dad..bf0b9118ad12 100644 --- a/usr.bin/apply/apply.c +++ b/usr.bin/apply/apply.c @@ -143,13 +143,13 @@ main(int argc, char *argv[]) { p = cmd; offset = snprintf(cmd, cmdsize, EXEC "%s", argv[0]); - if ((size_t)offset >= cmdsize) + if (offset < 0 || (size_t)offset >= cmdsize) err(1, "snprintf() failed"); p += offset; cmdsize -= offset; for (i = 1; i <= nargs; i++) { offset = snprintf(p, cmdsize, " %c%d", magic, i); - if ((size_t)offset >= cmdsize) + if (offset < 0 || (size_t)offset >= cmdsize) err(1, "snprintf() failed"); p += offset; cmdsize -= offset; @@ -163,7 +163,7 @@ main(int argc, char *argv[]) { nargs = 1; } else { offset = snprintf(cmd, cmdsize, EXEC "%s", argv[0]); - if ((size_t)offset >= cmdsize) + if (offset < 0 || (size_t)offset >= cmdsize) err(1, "snprintf() failed"); nargs = n; } @@ -196,7 +196,7 @@ main(int argc, char *argv[]) { if (p[0] == magic && isdigit(p[1]) && p[1] != '0') { offset = snprintf(q, l, "%s", argv[(++p)[0] - '0']); - if ((size_t)offset >= l) + if (offset < 0 || (size_t)offset >= l) err(1, "snprintf() failed"); q += offset; l -= offset; diff --git a/usr.bin/ftp/fetch.c b/usr.bin/ftp/fetch.c index 2fc48eed4d9a..870c3ff54575 100644 --- a/usr.bin/ftp/fetch.c +++ b/usr.bin/ftp/fetch.c @@ -282,7 +282,7 @@ url_get(origline, proxyenv) printf("Requesting %s (via %s)\n", origline, proxyenv); len = snprintf(buf, sizeof(buf), "GET %s%s HTTP/1.0\r\n\r\n", proxy ? "" : "/", path); - if (write(s, buf, len) < len) { + if (len < 0 || write(s, buf, len) < len) { warn("Writing HTTP request"); goto cleanup_url_get; } diff --git a/usr.bin/ftp/util.c b/usr.bin/ftp/util.c index 00f0106635ee..1936baf3c14e 100644 --- a/usr.bin/ftp/util.c +++ b/usr.bin/ftp/util.c @@ -604,7 +604,7 @@ progressmeter(flag) struct timeval now, td, wait; off_t cursize, abbrevsize; double elapsed; - int ratio, barlength, i, len; + int ratio, barlength, i, len, n; off_t remaining; char buf[256]; @@ -623,16 +623,20 @@ progressmeter(flag) ratio = cursize * 100 / filesize; ratio = MAX(ratio, 0); ratio = MIN(ratio, 100); - len += snprintf(buf + len, sizeof(buf) - len, "\r%3d%% ", ratio); + n = snprintf(buf + len, sizeof(buf) - len, "\r%3d%% ", ratio); + if (n > 0) + len += n; barlength = ttywidth - 30; if (barlength > 0) { i = barlength * ratio / 100; - len += snprintf(buf + len, sizeof(buf) - len, + n = snprintf(buf + len, sizeof(buf) - len, "|%.*s%*s|", i, "*****************************************************************************" "*****************************************************************************", barlength - i, ""); + if (n > 0) + len += n; } i = 0; @@ -641,9 +645,11 @@ progressmeter(flag) i++; abbrevsize >>= 10; } - len += snprintf(buf + len, sizeof(buf) - len, + n = snprintf(buf + len, sizeof(buf) - len, " %5qd %c%c ", (long long)abbrevsize, prefixes[i], prefixes[i] == ' ' ? ' ' : 'B'); + if (n > 0) + len += n; timersub(&now, &lastupdate, &wait); if (cursize > lastsize) { @@ -660,30 +666,34 @@ progressmeter(flag) elapsed = td.tv_sec + (td.tv_usec / 1000000.0); if (bytes <= 0 || elapsed <= 0.0 || cursize > filesize) { - len += snprintf(buf + len, sizeof(buf) - len, + n = snprintf(buf + len, sizeof(buf) - len, " --:-- ETA"); } else if (wait.tv_sec >= STALLTIME) { - len += snprintf(buf + len, sizeof(buf) - len, + n = snprintf(buf + len, sizeof(buf) - len, " - stalled -"); } else { remaining = ((filesize - restart_point) / (bytes / elapsed) - elapsed); if (remaining >= 100 * SECSPERHOUR) - len += snprintf(buf + len, sizeof(buf) - len, + n = snprintf(buf + len, sizeof(buf) - len, " --:-- ETA"); else { i = remaining / SECSPERHOUR; if (i) - len += snprintf(buf + len, sizeof(buf) - len, + n = snprintf(buf + len, sizeof(buf) - len, "%2d:", i); else - len += snprintf(buf + len, sizeof(buf) - len, + n = snprintf(buf + len, sizeof(buf) - len, " "); + if (n > 0) + len += n; i = remaining % SECSPERHOUR; len += snprintf(buf + len, sizeof(buf) - len, "%02d:%02d ETA", i / 60, i % 60); } } + if (n > 0) + len += n; (void)write(STDOUT_FILENO, buf, len); if (flag == -1) { @@ -711,7 +721,7 @@ ptransfer(siginfo) struct timeval now, td; double elapsed; off_t bs; - int meg, remaining, hh, len; + int meg, n, remaining, hh, len; char buf[100]; if (!verbose && !siginfo) @@ -725,10 +735,12 @@ ptransfer(siginfo) if (bs > (1024 * 1024)) meg = 1; len = 0; - len += snprintf(buf + len, sizeof(buf) - len, + n = snprintf(buf + len, sizeof(buf) - len, "%qd byte%s %s in %.2f seconds (%.2f %sB/s)\n", (long long)bytes, bytes == 1 ? "" : "s", direction, elapsed, bs / (1024.0 * (meg ? 1024.0 : 1.0)), meg ? "M" : "K"); + if (n > 0) + len += n; if (siginfo && bytes > 0 && elapsed > 0.0 && filesize >= 0 && bytes + restart_point <= filesize) { remaining = (int)((filesize - restart_point) / @@ -736,9 +748,11 @@ ptransfer(siginfo) hh = remaining / SECSPERHOUR; remaining %= SECSPERHOUR; len--; /* decrement len to overwrite \n */ - len += snprintf(buf + len, sizeof(buf) - len, + n = snprintf(buf + len, sizeof(buf) - len, " ETA: %02d:%02d:%02d\n", hh, remaining / 60, remaining % 60); + if (n > 0) + len += n; } (void)write(siginfo ? STDERR_FILENO : STDOUT_FILENO, buf, len); } diff --git a/usr.bin/mail/fio.c b/usr.bin/mail/fio.c index 0fb86b33e7e6..0a017ce5e1b9 100644 --- a/usr.bin/mail/fio.c +++ b/usr.bin/mail/fio.c @@ -419,7 +419,7 @@ getfold(name, namelen) else copylen = snprintf(name, namelen, "%s/%s", homedir ? homedir : ".", folder); - return (copylen >= namelen ? (-1) : (0)); + return (copylen < 0 || copylen >= namelen ? (-1) : (0)); } /*