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): remove unreached code from bmake_strndup



details:   https://anonhg.NetBSD.org/src/rev/acad3e3ee362
branches:  trunk
changeset: 974999:acad3e3ee362
user:      rillig <rillig%NetBSD.org@localhost>
date:      Thu Aug 20 06:35:14 2020 +0000

description:
make(1): remove unreached code from bmake_strndup

The "at most" branch was never taken since all call sites in var.c only
ever need a substring, and the target buffer is not limited.  Therefore
rename the function and make it simpler.

It's ok that bmake_strldup is defined as estrndup in case of USE_EMALLOC
since that function's implementation is compatible to the "copy
exactly", it just contains some extra null checks that will never match
since the variable values cannot (well, or should not) contain null
bytes.  Theoretically they can, but the behavior then depends on the
exact implementation and is unreliable, therefore nobody does this.
After all, Makefiles are used for text processing, not for binary data.

diffstat:

 usr.bin/make/make_malloc.c |  20 +++++---------------
 usr.bin/make/make_malloc.h |   6 +++---
 usr.bin/make/var.c         |  14 +++++++-------
 3 files changed, 15 insertions(+), 25 deletions(-)

diffs (132 lines):

diff -r 80c717265be1 -r acad3e3ee362 usr.bin/make/make_malloc.c
--- a/usr.bin/make/make_malloc.c        Thu Aug 20 05:54:32 2020 +0000
+++ b/usr.bin/make/make_malloc.c        Thu Aug 20 06:35:14 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: make_malloc.c,v 1.14 2020/08/12 18:47:21 rillig Exp $  */
+/*     $NetBSD: make_malloc.c,v 1.15 2020/08/20 06:35:14 rillig Exp $  */
 
 /*-
  * Copyright (c) 2009 The NetBSD Foundation, Inc.
@@ -28,7 +28,7 @@
 
 #ifdef MAKE_NATIVE
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: make_malloc.c,v 1.14 2020/08/12 18:47:21 rillig Exp $");
+__RCSID("$NetBSD: make_malloc.c,v 1.15 2020/08/20 06:35:14 rillig Exp $");
 #endif
 
 #include <stdio.h>
@@ -82,23 +82,13 @@
        return memcpy(p, str, len);
 }
 
-/*
- * bmake_strndup --
- *     strndup, but die on error.
- */
+/* Allocate a string starting from str with exactly len characters. */
 char *
-bmake_strndup(const char *str, size_t max_len)
+bmake_strldup(const char *str, size_t len)
 {
-       size_t len;
-       char *p;
-
-       for (len = 0; len < max_len; len++)
-           if (str[len] == '\0')
-               break;
-       p = bmake_malloc(len + 1);
+       char *p = bmake_malloc(len + 1);
        memcpy(p, str, len);
        p[len] = '\0';
-
        return p;
 }
 
diff -r 80c717265be1 -r acad3e3ee362 usr.bin/make/make_malloc.h
--- a/usr.bin/make/make_malloc.h        Thu Aug 20 05:54:32 2020 +0000
+++ b/usr.bin/make/make_malloc.h        Thu Aug 20 06:35:14 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: make_malloc.h,v 1.6 2020/08/01 14:47:49 rillig Exp $   */
+/*     $NetBSD: make_malloc.h,v 1.7 2020/08/20 06:35:14 rillig Exp $   */
 
 /*-
  * Copyright (c) 2009 The NetBSD Foundation, Inc.
@@ -30,13 +30,13 @@
 void *bmake_malloc(size_t);
 void *bmake_realloc(void *, size_t);
 char *bmake_strdup(const char *);
-char *bmake_strndup(const char *, size_t);
+char *bmake_strldup(const char *, size_t);
 #else
 #include <util.h>
 #define bmake_malloc(x)         emalloc(x)
 #define bmake_realloc(x,y)      erealloc(x,y)
 #define bmake_strdup(x)         estrdup(x)
-#define bmake_strndup(x,y)      estrndup(x,y)
+#define bmake_strldup(x,y)      estrndup(x,y)
 #endif
 
 /* Thin wrapper around free(3) to avoid the extra function call in case
diff -r 80c717265be1 -r acad3e3ee362 usr.bin/make/var.c
--- a/usr.bin/make/var.c        Thu Aug 20 05:54:32 2020 +0000
+++ b/usr.bin/make/var.c        Thu Aug 20 06:35:14 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: var.c,v 1.449 2020/08/13 04:12:13 rillig Exp $ */
+/*     $NetBSD: var.c,v 1.450 2020/08/20 06:35:14 rillig Exp $ */
 
 /*
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -69,14 +69,14 @@
  */
 
 #ifndef MAKE_NATIVE
-static char rcsid[] = "$NetBSD: var.c,v 1.449 2020/08/13 04:12:13 rillig Exp $";
+static char rcsid[] = "$NetBSD: var.c,v 1.450 2020/08/20 06:35:14 rillig Exp $";
 #else
 #include <sys/cdefs.h>
 #ifndef lint
 #if 0
 static char sccsid[] = "@(#)var.c      8.3 (Berkeley) 3/19/94";
 #else
-__RCSID("$NetBSD: var.c,v 1.449 2020/08/13 04:12:13 rillig Exp $");
+__RCSID("$NetBSD: var.c,v 1.450 2020/08/20 06:35:14 rillig Exp $");
 #endif
 #endif /* not lint */
 #endif
@@ -2280,7 +2280,7 @@
         * Either Var_Subst or ModifyWords will need a
         * nul-terminated string soon, so construct one now.
         */
-       pattern = bmake_strndup(mod + 1, (size_t)(endpat - (mod + 1)));
+       pattern = bmake_strldup(mod + 1, (size_t)(endpat - (mod + 1)));
     }
 
     if (needSubst) {
@@ -2894,7 +2894,7 @@
 
     if (mod[1] == '=') {
        size_t n = strcspn(mod + 2, ":)}");
-       char *name = bmake_strndup(mod + 2, n);
+       char *name = bmake_strldup(mod + 2, n);
        Var_Set(name, st->val, st->ctxt);
        free(name);
        *pp = mod + 2 + n;
@@ -3510,7 +3510,7 @@
                 */
                *lengthPtr = (int)(size_t)(tstr - str) + 1;
                if (dynamic) {
-                   char *pstr = bmake_strndup(str, (size_t)*lengthPtr);
+                   char *pstr = bmake_strldup(str, (size_t)*lengthPtr);
                    *freePtr = pstr;
                    Buf_Destroy(&namebuf, TRUE);
                    return pstr;
@@ -3605,7 +3605,7 @@
                *freePtr = NULL;
            }
            if (dynamic) {
-               nstr = bmake_strndup(str, (size_t)*lengthPtr);
+               nstr = bmake_strldup(str, (size_t)*lengthPtr);
                *freePtr = nstr;
            } else {
                nstr = (eflags & VARE_UNDEFERR) ? var_Error : varNoError;



Home | Main Index | Thread Index | Old Index