Module Name: src Committed By: rillig Date: Thu Aug 20 06:35:14 UTC 2020
Modified Files: src/usr.bin/make: make_malloc.c make_malloc.h var.c Log Message: 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. To generate a diff of this commit: cvs rdiff -u -r1.14 -r1.15 src/usr.bin/make/make_malloc.c cvs rdiff -u -r1.6 -r1.7 src/usr.bin/make/make_malloc.h cvs rdiff -u -r1.449 -r1.450 src/usr.bin/make/var.c Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files.
Modified files: Index: src/usr.bin/make/make_malloc.c diff -u src/usr.bin/make/make_malloc.c:1.14 src/usr.bin/make/make_malloc.c:1.15 --- src/usr.bin/make/make_malloc.c:1.14 Wed Aug 12 18:47:21 2020 +++ src/usr.bin/make/make_malloc.c Thu Aug 20 06:35:14 2020 @@ -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 @@ bmake_strdup(const char *str) 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; } Index: src/usr.bin/make/make_malloc.h diff -u src/usr.bin/make/make_malloc.h:1.6 src/usr.bin/make/make_malloc.h:1.7 --- src/usr.bin/make/make_malloc.h:1.6 Sat Aug 1 14:47:49 2020 +++ src/usr.bin/make/make_malloc.h Thu Aug 20 06:35:14 2020 @@ -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 Index: src/usr.bin/make/var.c diff -u src/usr.bin/make/var.c:1.449 src/usr.bin/make/var.c:1.450 --- src/usr.bin/make/var.c:1.449 Thu Aug 13 04:12:13 2020 +++ src/usr.bin/make/var.c Thu Aug 20 06:35:14 2020 @@ -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 @@ ApplyModifier_Match(const char **pp, App * 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 @@ ApplyModifier_Remember(const char **pp, 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 @@ Var_Parse(const char * const str, GNode */ *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 @@ Var_Parse(const char * const str, GNode *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;