Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/bin/pax Don't use the length return from snprintf to write o...



details:   https://anonhg.NetBSD.org/src/rev/12cfdc30184f
branches:  trunk
changeset: 815654:12cfdc30184f
user:      dholland <dholland%NetBSD.org@localhost>
date:      Tue May 31 03:32:36 2016 +0000

description:
Don't use the length return from snprintf to write out the result
buffer. If snprintf truncated the output, the length returned will be
greater and we'll write trash. Just call strlen instead. (And since
what we're doing is writing progress messages to the user, checking
carefully for truncation isn't really worthwhile either.)

Spotted when attending to PR 50998 from David Binderman; the issue
there (computation of an unused value) popped up because one of the
prints was already calling strlen.

diffstat:

 bin/pax/ar_io.c |  18 ++++++++----------
 1 files changed, 8 insertions(+), 10 deletions(-)

diffs (66 lines):

diff -r 655d7c8ed236 -r 12cfdc30184f bin/pax/ar_io.c
--- a/bin/pax/ar_io.c   Tue May 31 03:25:46 2016 +0000
+++ b/bin/pax/ar_io.c   Tue May 31 03:32:36 2016 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: ar_io.c,v 1.56 2015/03/09 23:38:08 sevan Exp $ */
+/*     $NetBSD: ar_io.c,v 1.57 2016/05/31 03:32:36 dholland Exp $      */
 
 /*-
  * Copyright (c) 1992 Keith Muller.
@@ -42,7 +42,7 @@
 #if 0
 static char sccsid[] = "@(#)ar_io.c    8.2 (Berkeley) 4/18/94";
 #else
-__RCSID("$NetBSD: ar_io.c,v 1.56 2015/03/09 23:38:08 sevan Exp $");
+__RCSID("$NetBSD: ar_io.c,v 1.57 2016/05/31 03:32:36 dholland Exp $");
 #endif
 #endif /* not lint */
 
@@ -1631,7 +1631,6 @@
 ar_summary(int n)
 {
        time_t secs;
-       int len;
        char buf[BUFSIZ];
        char tbuf[MAXPATHLEN/4];        /* XXX silly size! */
        char s1buf[MAXPATHLEN/8];       /* XXX very silly size! */
@@ -1657,33 +1656,32 @@
         * could have written anything yet.
         */
        if (frmt == NULL && act != COPY) {
-               len = snprintf(buf, sizeof(buf),
+               snprintf(buf, sizeof(buf),
                    "unknown format, %s skipped in %s\n",
                    sizefmt(s1buf, sizeof(s1buf), rdcnt),
                    timefmt(tbuf, sizeof(tbuf), rdcnt, secs, "bytes"));
                if (n == 0)
                        (void)fprintf(outf, "%s: %s", argv0, buf);
                else
-                       (void)write(STDERR_FILENO, buf, len);
+                       (void)write(STDERR_FILENO, buf, strlen(buf));
                return;
        }
 
 
        if (n != 0 && *archd.name) {
-               len = snprintf(buf, sizeof(buf), "Working on `%s' (%s)\n",
+               snprintf(buf, sizeof(buf), "Working on `%s' (%s)\n",
                    archd.name, sizefmt(s1buf, sizeof(s1buf), archd.sb.st_size));
-               (void)write(STDERR_FILENO, buf, len);
-               len = 0;
+               (void)write(STDERR_FILENO, buf, strlen(buf));
        }
 
 
        if (act == COPY) {
-               len = snprintf(buf, sizeof(buf),
+               snprintf(buf, sizeof(buf),
                    "%lu files in %s\n",
                    (unsigned long)flcnt,
                    timefmt(tbuf, sizeof(tbuf), flcnt, secs, "files"));
        } else {
-               len = snprintf(buf, sizeof(buf),
+               snprintf(buf, sizeof(buf),
                    "%s vol %d, %lu files, %s read, %s written in %s\n",
                    frmt->name, arvol-1, (unsigned long)flcnt,
                    sizefmt(s1buf, sizeof(s1buf), rdcnt),



Home | Main Index | Thread Index | Old Index