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.
This commit is contained in:
parent
58e1cf785c
commit
0cbd5c0e4f
@ -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;
|
||||
}
|
||||
|
||||
|
@ -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
|
||||
|
@ -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, ApplyModifiersState *st)
|
||||
* 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, ApplyModifiersState *st)
|
||||
|
||||
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 *ctxt, VarEvalFlags eflags,
|
||||
*/
|
||||
*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 *ctxt, VarEvalFlags eflags,
|
||||
*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;
|
||||
|
Loading…
Reference in New Issue
Block a user