Module Name:    src
Committed By:   rillig
Date:           Wed Feb  3 08:40:47 UTC 2021

Modified Files:
        src/usr.bin/make: var.c
        src/usr.bin/make/unit-tests: var-op-append.exp var-op-append.mk
            varname-empty.exp

Log Message:
make: fix double expansion when appending to a new variable


To generate a diff of this commit:
cvs rdiff -u -r1.792 -r1.793 src/usr.bin/make/var.c
cvs rdiff -u -r1.3 -r1.4 src/usr.bin/make/unit-tests/var-op-append.exp
cvs rdiff -u -r1.7 -r1.8 src/usr.bin/make/unit-tests/var-op-append.mk
cvs rdiff -u -r1.6 -r1.7 src/usr.bin/make/unit-tests/varname-empty.exp

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/var.c
diff -u src/usr.bin/make/var.c:1.792 src/usr.bin/make/var.c:1.793
--- src/usr.bin/make/var.c:1.792	Wed Feb  3 08:08:18 2021
+++ src/usr.bin/make/var.c	Wed Feb  3 08:40:47 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: var.c,v 1.792 2021/02/03 08:08:18 rillig Exp $	*/
+/*	$NetBSD: var.c,v 1.793 2021/02/03 08:40:47 rillig Exp $	*/
 
 /*
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -131,7 +131,7 @@
 #include "metachar.h"
 
 /*	"@(#)var.c	8.3 (Berkeley) 3/19/94" */
-MAKE_RCSID("$NetBSD: var.c,v 1.792 2021/02/03 08:08:18 rillig Exp $");
+MAKE_RCSID("$NetBSD: var.c,v 1.793 2021/02/03 08:40:47 rillig Exp $");
 
 typedef enum VarFlags {
 	VAR_NONE	= 0,
@@ -916,6 +916,12 @@ SetVar(const char *name, const char *val
 {
 	Var *v;
 
+	assert(val != NULL);
+	if (name[0] == '\0') {
+		DEBUG0(VAR, "SetVar: variable name is empty - ignored\n");
+		return;
+	}
+
 	if (ctxt == VAR_GLOBAL) {
 		v = VarFind(name, VAR_CMDLINE, FALSE);
 		if (v != NULL) {
@@ -1034,12 +1040,7 @@ Var_Set(const char *name, const char *va
 void
 Global_Set(const char *name, const char *value)
 {
-	assert(value != NULL);
-
-	if (name[0] == '\0')
-		DEBUG0(VAR, "Variable name empty - ignored\n");
-	else
-		SetVar(name, value, VAR_GLOBAL, VAR_SET_NONE);
+	SetVar(name, value, VAR_GLOBAL, VAR_SET_NONE);
 }
 
 void
@@ -1093,8 +1094,7 @@ Var_Append(const char *name, const char 
 	v = VarFind(name, ctxt, ctxt == VAR_GLOBAL);
 
 	if (v == NULL) {
-		/* XXX: name is expanded for the second time */
-		Var_Set(name, val, ctxt);
+		SetVar(name, val, ctxt, VAR_SET_NONE);
 	} else if (v->flags & VAR_READONLY) {
 		DEBUG1(VAR, "Ignoring append to %s since it is read-only\n",
 		    name);

Index: src/usr.bin/make/unit-tests/var-op-append.exp
diff -u src/usr.bin/make/unit-tests/var-op-append.exp:1.3 src/usr.bin/make/unit-tests/var-op-append.exp:1.4
--- src/usr.bin/make/unit-tests/var-op-append.exp:1.3	Tue Feb  2 16:18:16 2021
+++ src/usr.bin/make/unit-tests/var-op-append.exp	Wed Feb  3 08:40:47 2021
@@ -1,7 +1,7 @@
 Var_Parse: ${:U\$\$\$\$\$\$\$\$} with VARE_WANTRES
 Applying ${:U...} to "" (VARE_WANTRES, none, VES_UNDEF)
 Result of ${:U\$\$\$\$\$\$\$\$} is "$$$$$$$$" (VARE_WANTRES, none, VES_DEF)
-Global:VAR.$$$$ = dollars
+Global:VAR.$$$$$$$$ = dollars
 Global:.MAKEFLAGS =  -r -k -d v -d
 Global:.MAKEFLAGS =  -r -k -d v -d 0
 exit status 0

Index: src/usr.bin/make/unit-tests/var-op-append.mk
diff -u src/usr.bin/make/unit-tests/var-op-append.mk:1.7 src/usr.bin/make/unit-tests/var-op-append.mk:1.8
--- src/usr.bin/make/unit-tests/var-op-append.mk:1.7	Fri Oct 30 20:36:33 2020
+++ src/usr.bin/make/unit-tests/var-op-append.mk	Wed Feb  3 08:40:47 2021
@@ -1,4 +1,4 @@
-# $NetBSD: var-op-append.mk,v 1.7 2020/10/30 20:36:33 rillig Exp $
+# $NetBSD: var-op-append.mk,v 1.8 2021/02/03 08:40:47 rillig Exp $
 #
 # Tests for the += variable assignment operator, which appends to a variable,
 # creating it if necessary.
@@ -32,17 +32,15 @@ C++=	value
 .  error
 .endif
 
-# Try out how often the variable name is expanded when appending to a
-# nonexistent variable.
-# As of 2020-10-30, that's two times.
-# XXX: That's one time too often.
-# See Var_Append, the call to Var_Set.
+# Before var.c 1.793 from 2021-02-03, the variable name of a newly created
+# variable was expanded two times in a row, which was unexpected but
+# irrelevant in practice since variable names containing dollars lead to
+# strange side effects in several other places as well.
 .MAKEFLAGS: -dv
 VAR.${:U\$\$\$\$\$\$\$\$}+=	dollars
 .MAKEFLAGS: -d0
-.if ${VAR.${:U\$\$\$\$}} != "dollars"
+.if ${VAR.${:U\$\$\$\$\$\$\$\$}} != "dollars"
 .  error
 .endif
 
 all:
-	@:;

Index: src/usr.bin/make/unit-tests/varname-empty.exp
diff -u src/usr.bin/make/unit-tests/varname-empty.exp:1.6 src/usr.bin/make/unit-tests/varname-empty.exp:1.7
--- src/usr.bin/make/unit-tests/varname-empty.exp:1.6	Wed Feb  3 08:34:15 2021
+++ src/usr.bin/make/unit-tests/varname-empty.exp	Wed Feb  3 08:40:47 2021
@@ -20,7 +20,7 @@ Global:delete .INCLUDEDFROMDIR (not foun
 Global:delete .INCLUDEDFROMFILE (not found)
 Var_Set("", "default", ...) name expands to empty string - ignored
 Var_Set("", "assigned", ...) name expands to empty string - ignored
-Var_Set("", "appended", ...) name expands to empty string - ignored
+SetVar: variable name is empty - ignored
 Var_Set("", "", ...) name expands to empty string - ignored
 Var_Set("", "subst", ...) name expands to empty string - ignored
 Var_Set("", "shell-output", ...) name expands to empty string - ignored

Reply via email to