Source-Changes-HG archive

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

[src/trunk]: src/bin/pax Remove undefined behavior in buf(); use buf() as int...



details:   https://anonhg.NetBSD.org/src/rev/d208313b1575
branches:  trunk
changeset: 815636:d208313b1575
user:      dholland <dholland%NetBSD.org@localhost>
date:      Mon May 30 17:34:35 2016 +0000

description:
Remove undefined behavior in buf(); use buf() as intended in intarg().
While here also add includes to fix the build. Retires PR 50999 from
David Binderman.

diffstat:

 bin/pax/dumptar.c |  19 ++++++++++++++-----
 1 files changed, 14 insertions(+), 5 deletions(-)

diffs (52 lines):

diff -r 6a30fb80b4cc -r d208313b1575 bin/pax/dumptar.c
--- a/bin/pax/dumptar.c Mon May 30 17:26:29 2016 +0000
+++ b/bin/pax/dumptar.c Mon May 30 17:34:35 2016 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: dumptar.c,v 1.2 2008/04/28 20:22:51 martin Exp $       */
+/*     $NetBSD: dumptar.c,v 1.3 2016/05/30 17:34:35 dholland Exp $     */
 
 /*-
  * Copyright (c) 2004 The NetBSD Foundation, Inc.
@@ -30,8 +30,12 @@
  */
 
 #include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <fcntl.h>
 #include <err.h>
-#include <fcntl.h>
+#include <assert.h>
 #include <sys/stat.h>
 #include <sys/mman.h>
 
@@ -39,20 +43,25 @@
 
 #define ussum(a) 1
 
+/*
+ * Ensure null termination.
+ */
 static char *
 buf(const char *p, size_t s)
 {
        static char buf[1024];
-       (void)snprintf(buf, sizeof(buf), "%s", p);
+
+       assert(s < sizeof(buf));
+       memcpy(buf, p, s);
        buf[s] = '\0';
        return buf;
 }
 
-int
+static int
 intarg(const char *p, size_t s)
 {
        char *ep, *b = buf(p, s);
-       int r = (int)strtol(p, &ep, 8);
+       int r = (int)strtol(b, &ep, 8);
        return r;
 }
 



Home | Main Index | Thread Index | Old Index