Source-Changes-HG archive

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

[src/trunk]: src/usr.bin/make make(1): replace one instance of strncpy with s...



details:   https://anonhg.NetBSD.org/src/rev/9afef481bf5d
branches:  trunk
changeset: 936710:9afef481bf5d
user:      rillig <rillig%NetBSD.org@localhost>
date:      Sun Aug 02 08:06:35 2020 +0000

description:
make(1): replace one instance of strncpy with snprintf

GCC 9 incorrectly claims that the string might not be null-terminated.
Since objdir is a global variable, it is initialized to zero, and the +1
in the size guarantees that this byte is always 0.

Still, using strncpy to initialize a string is a waste of memory access,
since it is enough if only the actual data is copied, without zeroing
out all the remaining bytes.

diffstat:

 usr.bin/make/main.c |  8 ++++----
 1 files changed, 4 insertions(+), 4 deletions(-)

diffs (36 lines):

diff -r 043c5d50032a -r 9afef481bf5d usr.bin/make/main.c
--- a/usr.bin/make/main.c       Sun Aug 02 07:19:39 2020 +0000
+++ b/usr.bin/make/main.c       Sun Aug 02 08:06:35 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: main.c,v 1.294 2020/08/01 21:40:49 rillig Exp $        */
+/*     $NetBSD: main.c,v 1.295 2020/08/02 08:06:35 rillig Exp $        */
 
 /*
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -69,7 +69,7 @@
  */
 
 #ifndef MAKE_NATIVE
-static char rcsid[] = "$NetBSD: main.c,v 1.294 2020/08/01 21:40:49 rillig Exp $";
+static char rcsid[] = "$NetBSD: main.c,v 1.295 2020/08/02 08:06:35 rillig Exp $";
 #else
 #include <sys/cdefs.h>
 #ifndef lint
@@ -81,7 +81,7 @@
 #if 0
 static char sccsid[] = "@(#)main.c     8.3 (Berkeley) 3/19/94";
 #else
-__RCSID("$NetBSD: main.c,v 1.294 2020/08/01 21:40:49 rillig Exp $");
+__RCSID("$NetBSD: main.c,v 1.295 2020/08/02 08:06:35 rillig Exp $");
 #endif
 #endif /* not lint */
 #endif
@@ -745,7 +745,7 @@
                        (void)fprintf(stderr, "make warning: %s: %s.\n",
                                      path, strerror(errno));
                } else {
-                       strncpy(objdir, path, MAXPATHLEN);
+                       snprintf(objdir, sizeof objdir, "%s", path);
                        Var_Set(".OBJDIR", objdir, VAR_GLOBAL);
                        setenv("PWD", objdir, 1);
                        Dir_InitDot();



Home | Main Index | Thread Index | Old Index