Module Name:    src
Committed By:   rillig
Date:           Sat Mar 26 12:44:57 UTC 2022

Modified Files:
        src/usr.bin/make: var.c
        src/usr.bin/make/unit-tests: Makefile directive-undef.mk

Log Message:
make: fix crash on .undef of an environment variable (since 2020-10-06)


To generate a diff of this commit:
cvs rdiff -u -r1.1013 -r1.1014 src/usr.bin/make/var.c
cvs rdiff -u -r1.310 -r1.311 src/usr.bin/make/unit-tests/Makefile
cvs rdiff -u -r1.11 -r1.12 src/usr.bin/make/unit-tests/directive-undef.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.1013 src/usr.bin/make/var.c:1.1014
--- src/usr.bin/make/var.c:1.1013	Thu Mar  3 19:52:41 2022
+++ src/usr.bin/make/var.c	Sat Mar 26 12:44:57 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: var.c,v 1.1013 2022/03/03 19:52:41 rillig Exp $	*/
+/*	$NetBSD: var.c,v 1.1014 2022/03/26 12:44:57 rillig Exp $	*/
 
 /*
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -139,7 +139,7 @@
 #include "metachar.h"
 
 /*	"@(#)var.c	8.3 (Berkeley) 3/19/94" */
-MAKE_RCSID("$NetBSD: var.c,v 1.1013 2022/03/03 19:52:41 rillig Exp $");
+MAKE_RCSID("$NetBSD: var.c,v 1.1014 2022/03/26 12:44:57 rillig Exp $");
 
 /*
  * Variables are defined using one of the VAR=value assignments.  Their
@@ -1088,20 +1088,14 @@ Var_Append(GNode *scope, const char *nam
 		DEBUG3(VAR, "%s: %s = %s\n", scope->name, name, v->val.data);
 
 		if (v->fromEnvironment) {
-			/*
-			 * The variable originally came from the environment.
-			 * Install it in the global scope (we could place it
-			 * in the environment, but then we should provide a
-			 * way to export other variables...)
-			 */
-			v->fromEnvironment = false;
+			/* See VarAdd. */
+			HashEntry *he =
+			    HashTable_CreateEntry(&scope->vars, name, NULL);
+			HashEntry_Set(he, v);
+			FStr_Done(&v->name);
+			v->name = FStr_InitRefer(/* aliased to */ he->key);
 			v->shortLived = false;
-			/*
-			 * This is the only place where a variable is
-			 * created in a scope, where v->name does not alias
-			 * scope->vars->key.
-			 */
-			HashTable_Set(&scope->vars, name, v);
+			v->fromEnvironment = false;
 		}
 	}
 }

Index: src/usr.bin/make/unit-tests/Makefile
diff -u src/usr.bin/make/unit-tests/Makefile:1.310 src/usr.bin/make/unit-tests/Makefile:1.311
--- src/usr.bin/make/unit-tests/Makefile:1.310	Fri Mar 25 22:38:39 2022
+++ src/usr.bin/make/unit-tests/Makefile	Sat Mar 26 12:44:57 2022
@@ -1,4 +1,4 @@
-# $NetBSD: Makefile,v 1.310 2022/03/25 22:38:39 rillig Exp $
+# $NetBSD: Makefile,v 1.311 2022/03/26 12:44:57 rillig Exp $
 #
 # Unit tests for make(1)
 #
@@ -467,6 +467,7 @@ TESTS:= ${TESTS:${BROKEN_TESTS:S,^,N,:ts
 # Additional environment variables for some of the tests.
 # The base environment is -i PATH="$PATH".
 ENV.depsrc-optional+=   TZ=UTC
+ENV.directive-undef=	ENV_VAR=env-value
 ENV.envfirst=		FROM_ENV=value-from-env
 ENV.varmisc=		FROM_ENV=env
 ENV.varmisc+=		FROM_ENV_BEFORE=env

Index: src/usr.bin/make/unit-tests/directive-undef.mk
diff -u src/usr.bin/make/unit-tests/directive-undef.mk:1.11 src/usr.bin/make/unit-tests/directive-undef.mk:1.12
--- src/usr.bin/make/unit-tests/directive-undef.mk:1.11	Fri Mar 25 23:03:47 2022
+++ src/usr.bin/make/unit-tests/directive-undef.mk	Sat Mar 26 12:44:57 2022
@@ -1,4 +1,4 @@
-# $NetBSD: directive-undef.mk,v 1.11 2022/03/25 23:03:47 rillig Exp $
+# $NetBSD: directive-undef.mk,v 1.12 2022/03/26 12:44:57 rillig Exp $
 #
 # Tests for the .undef directive.
 #
@@ -130,4 +130,16 @@ INDIRECT=	in-${DIRECT}
 .endif
 
 
+# Since var.c 1.570 from 2020-10-06 and before var.c 1.1014 from 2022-03-26,
+# make ran into an assertion failure when trying to undefine a variable that
+# was based on an environment variable.
+.if ${ENV_VAR} != "env-value"	# see ./Makefile, ENV.directive-undef
+.  error
+.endif
+ENV_VAR+=	appended	# moves the short-lived variable to the
+				# global scope
+.undef ENV_VAR			# removes the variable from both the global
+				# scope and from the environment
+
+
 all:

Reply via email to