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): use snprintf instead of strncpy



details:   https://anonhg.NetBSD.org/src/rev/7e8fe6b66024
branches:  trunk
changeset: 974416:7e8fe6b66024
user:      rillig <rillig%NetBSD.org@localhost>
date:      Fri Jul 31 16:26:16 2020 +0000

description:
make(1): use snprintf instead of strncpy

strncpy is not suited for string processing, despite its name.

Even though the previous code used the correct code pattern for strncpy,
it still wasted cycles since strncpy always fills the whole target
buffer. That's not needed.

diffstat:

 usr.bin/make/arch.c |  12 +++++-------
 1 files changed, 5 insertions(+), 7 deletions(-)

diffs (46 lines):

diff -r 2f5a218272e2 -r 7e8fe6b66024 usr.bin/make/arch.c
--- a/usr.bin/make/arch.c       Fri Jul 31 15:16:05 2020 +0000
+++ b/usr.bin/make/arch.c       Fri Jul 31 16:26:16 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: arch.c,v 1.77 2020/07/28 16:42:22 rillig Exp $ */
+/*     $NetBSD: arch.c,v 1.78 2020/07/31 16:26:16 rillig Exp $ */
 
 /*
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -69,14 +69,14 @@
  */
 
 #ifndef MAKE_NATIVE
-static char rcsid[] = "$NetBSD: arch.c,v 1.77 2020/07/28 16:42:22 rillig Exp $";
+static char rcsid[] = "$NetBSD: arch.c,v 1.78 2020/07/31 16:26:16 rillig Exp $";
 #else
 #include <sys/cdefs.h>
 #ifndef lint
 #if 0
 static char sccsid[] = "@(#)arch.c     8.2 (Berkeley) 1/2/94";
 #else
-__RCSID("$NetBSD: arch.c,v 1.77 2020/07/28 16:42:22 rillig Exp $");
+__RCSID("$NetBSD: arch.c,v 1.78 2020/07/31 16:26:16 rillig Exp $");
 #endif
 #endif /* not lint */
 #endif
@@ -562,8 +562,7 @@
 
            if (len > AR_MAX_NAME_LEN) {
                len = AR_MAX_NAME_LEN;
-               strncpy(copy, member, AR_MAX_NAME_LEN);
-               copy[AR_MAX_NAME_LEN] = '\0';
+               snprintf(copy, sizeof copy, "%s", member);
            }
            if ((he = Hash_FindEntry(&ar->members, copy)) != NULL)
                return (struct ar_hdr *)Hash_GetValue(he);
@@ -814,8 +813,7 @@
        fprintf(debug_file, "Replaced %s with %s\n", name, &ar->fnametab[entry]);
     }
 
-    (void)strncpy(name, &ar->fnametab[entry], MAXPATHLEN);
-    name[MAXPATHLEN] = '\0';
+    snprintf(name, MAXPATHLEN + 1, "%s", &ar->fnametab[entry]);
     return 1;
 }
 #endif



Home | Main Index | Thread Index | Old Index