Sun Jul 26 19:16:18 2020 UTC ()
make(1): eliminate another local variable in Var_Parse


(rillig)
diff -r1.326 -r1.327 src/usr.bin/make/var.c

cvs diff -r1.326 -r1.327 src/usr.bin/make/var.c (expand / switch to unified diff)

--- src/usr.bin/make/var.c 2020/07/26 19:13:42 1.326
+++ src/usr.bin/make/var.c 2020/07/26 19:16:17 1.327
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: var.c,v 1.326 2020/07/26 19:13:42 rillig Exp $ */ 1/* $NetBSD: var.c,v 1.327 2020/07/26 19:16:17 rillig Exp $ */
2 2
3/* 3/*
4 * Copyright (c) 1988, 1989, 1990, 1993 4 * Copyright (c) 1988, 1989, 1990, 1993
5 * The Regents of the University of California. All rights reserved. 5 * The Regents of the University of California. All rights reserved.
6 * 6 *
7 * This code is derived from software contributed to Berkeley by 7 * This code is derived from software contributed to Berkeley by
8 * Adam de Boor. 8 * Adam de Boor.
9 * 9 *
10 * Redistribution and use in source and binary forms, with or without 10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions 11 * modification, are permitted provided that the following conditions
12 * are met: 12 * are met:
13 * 1. Redistributions of source code must retain the above copyright 13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer. 14 * notice, this list of conditions and the following disclaimer.
@@ -59,34 +59,34 @@ @@ -59,34 +59,34 @@
59 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 59 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
60 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 60 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
61 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 61 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
62 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 62 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
63 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 63 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
64 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 64 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
65 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 65 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
66 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 66 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
67 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 67 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
68 * SUCH DAMAGE. 68 * SUCH DAMAGE.
69 */ 69 */
70 70
71#ifndef MAKE_NATIVE 71#ifndef MAKE_NATIVE
72static char rcsid[] = "$NetBSD: var.c,v 1.326 2020/07/26 19:13:42 rillig Exp $"; 72static char rcsid[] = "$NetBSD: var.c,v 1.327 2020/07/26 19:16:17 rillig Exp $";
73#else 73#else
74#include <sys/cdefs.h> 74#include <sys/cdefs.h>
75#ifndef lint 75#ifndef lint
76#if 0 76#if 0
77static char sccsid[] = "@(#)var.c 8.3 (Berkeley) 3/19/94"; 77static char sccsid[] = "@(#)var.c 8.3 (Berkeley) 3/19/94";
78#else 78#else
79__RCSID("$NetBSD: var.c,v 1.326 2020/07/26 19:13:42 rillig Exp $"); 79__RCSID("$NetBSD: var.c,v 1.327 2020/07/26 19:16:17 rillig Exp $");
80#endif 80#endif
81#endif /* not lint */ 81#endif /* not lint */
82#endif 82#endif
83 83
84/*- 84/*-
85 * var.c -- 85 * var.c --
86 * Variable-handling functions 86 * Variable-handling functions
87 * 87 *
88 * Interface: 88 * Interface:
89 * Var_Set Set the value of a variable in the given 89 * Var_Set Set the value of a variable in the given
90 * context. The variable is created if it doesn't 90 * context. The variable is created if it doesn't
91 * yet exist. 91 * yet exist.
92 * 92 *
@@ -3373,49 +3373,47 @@ cleanup: @@ -3373,49 +3373,47 @@ cleanup:
3373 * specification is invalid. The length of the specification is 3373 * specification is invalid. The length of the specification is
3374 * placed in *lengthPtr (for invalid specifications, this is just 3374 * placed in *lengthPtr (for invalid specifications, this is just
3375 * 2...?). 3375 * 2...?).
3376 * If *freePtr is non-NULL then it's a pointer that the caller 3376 * If *freePtr is non-NULL then it's a pointer that the caller
3377 * should pass to free() to free memory used by the result. 3377 * should pass to free() to free memory used by the result.
3378 * 3378 *
3379 * Side Effects: 3379 * Side Effects:
3380 * None. 3380 * None.
3381 * 3381 *
3382 *----------------------------------------------------------------------- 3382 *-----------------------------------------------------------------------
3383 */ 3383 */
3384/* coverity[+alloc : arg-*4] */ 3384/* coverity[+alloc : arg-*4] */
3385char * 3385char *
3386Var_Parse(const char *str, GNode *ctxt, VarEvalFlags flags, 3386Var_Parse(const char * const str, GNode *ctxt, VarEvalFlags flags,
3387 int *lengthPtr, void **freePtr) 3387 int *lengthPtr, void **freePtr)
3388{ 3388{
3389 const char *tstr; /* Pointer into str */ 3389 const char *tstr; /* Pointer into str */
3390 Var *v; /* Variable in invocation */ 3390 Var *v; /* Variable in invocation */
3391 Boolean haveModifier; /* TRUE if have modifiers for the variable */ 3391 Boolean haveModifier; /* TRUE if have modifiers for the variable */
3392 char endc; /* Ending character when variable in parens 3392 char endc; /* Ending character when variable in parens
3393 * or braces */ 3393 * or braces */
3394 char startc; /* Starting character when variable in parens 3394 char startc; /* Starting character when variable in parens
3395 * or braces */ 3395 * or braces */
3396 int vlen; /* Length of variable name */ 3396 int vlen; /* Length of variable name */
3397 const char *start; /* Points to original start of str */ 
3398 char *nstr; /* New string, used during expansion */ 3397 char *nstr; /* New string, used during expansion */
3399 Boolean dynamic; /* TRUE if the variable is local and we're 3398 Boolean dynamic; /* TRUE if the variable is local and we're
3400 * expanding it in a non-local context. This 3399 * expanding it in a non-local context. This
3401 * is done to support dynamic sources. The 3400 * is done to support dynamic sources. The
3402 * result is just the invocation, unaltered */ 3401 * result is just the invocation, unaltered */
3403 const char *extramodifiers; /* extra modifiers to apply first */ 3402 const char *extramodifiers; /* extra modifiers to apply first */
3404 3403
3405 *freePtr = NULL; 3404 *freePtr = NULL;
3406 extramodifiers = NULL; 3405 extramodifiers = NULL;
3407 dynamic = FALSE; 3406 dynamic = FALSE;
3408 start = str; 
3409 3407
3410 startc = str[1]; 3408 startc = str[1];
3411 if (startc != PROPEN && startc != BROPEN) { 3409 if (startc != PROPEN && startc != BROPEN) {
3412 /* 3410 /*
3413 * If it's not bounded by braces of some sort, life is much simpler. 3411 * If it's not bounded by braces of some sort, life is much simpler.
3414 * We just need to check for the first character and return the 3412 * We just need to check for the first character and return the
3415 * value if it exists. 3413 * value if it exists.
3416 */ 3414 */
3417 3415
3418 /* Error out some really stupid names */ 3416 /* Error out some really stupid names */
3419 if (startc == '\0' || strchr(")}:$", startc)) { 3417 if (startc == '\0' || strchr(")}:$", startc)) {
3420 *lengthPtr = 1; 3418 *lengthPtr = 1;
3421 return var_Error; 3419 return var_Error;
@@ -3567,29 +3565,29 @@ Var_Parse(const char *str, GNode *ctxt,  @@ -3567,29 +3565,29 @@ Var_Parse(const char *str, GNode *ctxt,
3567 (strncmp(varname, ".ARCHIVE", len) == 0) || 3565 (strncmp(varname, ".ARCHIVE", len) == 0) ||
3568 (strncmp(varname, ".PREFIX", len) == 0) || 3566 (strncmp(varname, ".PREFIX", len) == 0) ||
3569 (strncmp(varname, ".MEMBER", len) == 0)) 3567 (strncmp(varname, ".MEMBER", len) == 0))
3570 { 3568 {
3571 dynamic = TRUE; 3569 dynamic = TRUE;
3572 } 3570 }
3573 } 3571 }
3574 3572
3575 if (!haveModifier) { 3573 if (!haveModifier) {
3576 /* 3574 /*
3577 * No modifiers -- have specification length so we can return 3575 * No modifiers -- have specification length so we can return
3578 * now. 3576 * now.
3579 */ 3577 */
3580 *lengthPtr = tstr - start + 1; 3578 *lengthPtr = tstr - str + 1;
3581 if (dynamic) { 3579 if (dynamic) {
3582 char *pstr = bmake_strndup(start, *lengthPtr); 3580 char *pstr = bmake_strndup(str, *lengthPtr);
3583 *freePtr = pstr; 3581 *freePtr = pstr;
3584 Buf_Destroy(&buf, TRUE); 3582 Buf_Destroy(&buf, TRUE);
3585 return pstr; 3583 return pstr;
3586 } else { 3584 } else {
3587 Buf_Destroy(&buf, TRUE); 3585 Buf_Destroy(&buf, TRUE);
3588 return (flags & VARE_UNDEFERR) ? var_Error : varNoError; 3586 return (flags & VARE_UNDEFERR) ? var_Error : varNoError;
3589 } 3587 }
3590 } else { 3588 } else {
3591 /* 3589 /*
3592 * Still need to get to the end of the variable specification, 3590 * Still need to get to the end of the variable specification,
3593 * so kludge up a Var structure for the modifications 3591 * so kludge up a Var structure for the modifications
3594 */ 3592 */
3595 v = bmake_malloc(sizeof(Var)); 3593 v = bmake_malloc(sizeof(Var));
@@ -3637,54 +3635,54 @@ Var_Parse(const char *str, GNode *ctxt,  @@ -3637,54 +3635,54 @@ Var_Parse(const char *str, GNode *ctxt,
3637 3635
3638 if (haveModifier) { 3636 if (haveModifier) {
3639 /* Skip initial colon. */ 3637 /* Skip initial colon. */
3640 tstr++; 3638 tstr++;
3641 3639
3642 nstr = ApplyModifiers(nstr, tstr, startc, endc, 3640 nstr = ApplyModifiers(nstr, tstr, startc, endc,
3643 v, ctxt, flags, &used, freePtr); 3641 v, ctxt, flags, &used, freePtr);
3644 tstr += used; 3642 tstr += used;
3645 free(extraFree); 3643 free(extraFree);
3646 } else { 3644 } else {
3647 *freePtr = extraFree; 3645 *freePtr = extraFree;
3648 } 3646 }
3649 } 3647 }
3650 *lengthPtr = tstr - start + (*tstr ? 1 : 0); 3648 *lengthPtr = tstr - str + (*tstr ? 1 : 0);
3651 3649
3652 if (v->flags & VAR_FROM_ENV) { 3650 if (v->flags & VAR_FROM_ENV) {
3653 Boolean destroy = FALSE; 3651 Boolean destroy = FALSE;
3654 3652
3655 if (nstr != Buf_GetAll(&v->val, NULL)) { 3653 if (nstr != Buf_GetAll(&v->val, NULL)) {
3656 destroy = TRUE; 3654 destroy = TRUE;
3657 } else { 3655 } else {
3658 /* 3656 /*
3659 * Returning the value unmodified, so tell the caller to free 3657 * Returning the value unmodified, so tell the caller to free
3660 * the thing. 3658 * the thing.
3661 */ 3659 */
3662 *freePtr = nstr; 3660 *freePtr = nstr;
3663 } 3661 }
3664 VarFreeEnv(v, destroy); 3662 VarFreeEnv(v, destroy);
3665 } else if (v->flags & VAR_JUNK) { 3663 } else if (v->flags & VAR_JUNK) {
3666 /* 3664 /*
3667 * Perform any free'ing needed and set *freePtr to NULL so the caller 3665 * Perform any free'ing needed and set *freePtr to NULL so the caller
3668 * doesn't try to free a static pointer. 3666 * doesn't try to free a static pointer.
3669 * If VAR_KEEP is also set then we want to keep str(?) as is. 3667 * If VAR_KEEP is also set then we want to keep str(?) as is.
3670 */ 3668 */
3671 if (!(v->flags & VAR_KEEP)) { 3669 if (!(v->flags & VAR_KEEP)) {
3672 if (*freePtr) { 3670 if (*freePtr) {
3673 free(nstr); 3671 free(nstr);
3674 *freePtr = NULL; 3672 *freePtr = NULL;
3675 } 3673 }
3676 if (dynamic) { 3674 if (dynamic) {
3677 nstr = bmake_strndup(start, *lengthPtr); 3675 nstr = bmake_strndup(str, *lengthPtr);
3678 *freePtr = nstr; 3676 *freePtr = nstr;
3679 } else { 3677 } else {
3680 nstr = (flags & VARE_UNDEFERR) ? var_Error : varNoError; 3678 nstr = (flags & VARE_UNDEFERR) ? var_Error : varNoError;
3681 } 3679 }
3682 } 3680 }
3683 if (nstr != Buf_GetAll(&v->val, NULL)) 3681 if (nstr != Buf_GetAll(&v->val, NULL))
3684 Buf_Destroy(&v->val, TRUE); 3682 Buf_Destroy(&v->val, TRUE);
3685 free(v->name); 3683 free(v->name);
3686 free(v); 3684 free(v);
3687 } 3685 }
3688 return nstr; 3686 return nstr;
3689} 3687}
3690 3688