Module Name:    src
Committed By:   rillig
Date:           Sat Aug 22 20:31:50 UTC 2020

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

Log Message:
make(1): prevent assignment to the variable with the empty name


To generate a diff of this commit:
cvs rdiff -u -r1.457 -r1.458 src/usr.bin/make/var.c
cvs rdiff -u -r1.1 -r1.2 src/usr.bin/make/unit-tests/varname-empty.exp \
    src/usr.bin/make/unit-tests/varname-empty.mk

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.457 src/usr.bin/make/var.c:1.458
--- src/usr.bin/make/var.c:1.457	Sat Aug 22 19:30:58 2020
+++ src/usr.bin/make/var.c	Sat Aug 22 20:31:50 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: var.c,v 1.457 2020/08/22 19:30:58 sjg Exp $	*/
+/*	$NetBSD: var.c,v 1.458 2020/08/22 20:31:50 rillig Exp $	*/
 
 /*
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -69,14 +69,14 @@
  */
 
 #ifndef MAKE_NATIVE
-static char rcsid[] = "$NetBSD: var.c,v 1.457 2020/08/22 19:30:58 sjg Exp $";
+static char rcsid[] = "$NetBSD: var.c,v 1.458 2020/08/22 20:31:50 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.457 2020/08/22 19:30:58 sjg Exp $");
+__RCSID("$NetBSD: var.c,v 1.458 2020/08/22 20:31:50 rillig Exp $");
 #endif
 #endif /* not lint */
 #endif
@@ -778,6 +778,7 @@ void
 Var_Set_with_flags(const char *name, const char *val, GNode *ctxt,
 		   VarSet_Flags flags)
 {
+    const char *unexpanded_name = name;
     char *name_freeIt = NULL;
     Var *v;
 
@@ -786,16 +787,15 @@ Var_Set_with_flags(const char *name, con
      * here will override anything in a lower context, so there's not much
      * point in searching them all just to save a bit of memory...
      */
-    if (strchr(name, '$') != NULL) {
-	const char *unexpanded_name = name;
+    if (strchr(name, '$') != NULL)
 	name = name_freeIt = Var_Subst(name, ctxt, VARE_WANTRES);
-	if (name[0] == '\0') {
-	    VAR_DEBUG("Var_Set(\"%s\", \"%s\", ...) "
-		      "name expands to empty string - ignored\n",
-		      unexpanded_name, val);
-	    free(name_freeIt);
-	    return;
-	}
+
+    if (name[0] == '\0') {
+	VAR_DEBUG("Var_Set(\"%s\", \"%s\", ...) "
+		  "name expands to empty string - ignored\n",
+		  unexpanded_name, val);
+	free(name_freeIt);
+	return;
     }
 
     if (ctxt == VAR_GLOBAL) {

Index: src/usr.bin/make/unit-tests/varname-empty.exp
diff -u src/usr.bin/make/unit-tests/varname-empty.exp:1.1 src/usr.bin/make/unit-tests/varname-empty.exp:1.2
--- src/usr.bin/make/unit-tests/varname-empty.exp:1.1	Sat Aug 22 20:23:14 2020
+++ src/usr.bin/make/unit-tests/varname-empty.exp	Sat Aug 22 20:31:50 2020
@@ -1,3 +1,3 @@
-value
-value value value
+fallback
+1 2 3
 exit status 0
Index: src/usr.bin/make/unit-tests/varname-empty.mk
diff -u src/usr.bin/make/unit-tests/varname-empty.mk:1.1 src/usr.bin/make/unit-tests/varname-empty.mk:1.2
--- src/usr.bin/make/unit-tests/varname-empty.mk:1.1	Sat Aug 22 20:23:14 2020
+++ src/usr.bin/make/unit-tests/varname-empty.mk	Sat Aug 22 20:31:50 2020
@@ -1,4 +1,4 @@
-# $NetBSD: varname-empty.mk,v 1.1 2020/08/22 20:23:14 rillig Exp $
+# $NetBSD: varname-empty.mk,v 1.2 2020/08/22 20:31:50 rillig Exp $
 #
 # Tests for the special variable with the empty name.
 #
@@ -6,7 +6,8 @@
 # This is because it is heavily used in the .for loop expansion,
 # as well as to generate arbitrary strings, as in ${:Ufallback}.
 
-# Oops.
+# Until 2020-08-22 it was possible to assign a value to the variable with
+# the empty name, leading to all kinds of unexpected effects.
 !=	echo 'value'
 
 # The .for loop expands the expression ${i} to ${:U1}, ${:U2} and so on.

Reply via email to