Module Name:    src
Committed By:   rillig
Date:           Sat Dec 12 19:31:18 UTC 2020

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

Log Message:
make(1): extract ExportVars from Var_Export


To generate a diff of this commit:
cvs rdiff -u -r1.725 -r1.726 src/usr.bin/make/var.c
cvs rdiff -u -r1.2 -r1.3 src/usr.bin/make/unit-tests/directive-export.exp
cvs rdiff -u -r1.4 -r1.5 src/usr.bin/make/unit-tests/directive-export.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.725 src/usr.bin/make/var.c:1.726
--- src/usr.bin/make/var.c:1.725	Sat Dec 12 18:53:53 2020
+++ src/usr.bin/make/var.c	Sat Dec 12 19:31:17 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: var.c,v 1.725 2020/12/12 18:53:53 rillig Exp $	*/
+/*	$NetBSD: var.c,v 1.726 2020/12/12 19:31:17 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.725 2020/12/12 18:53:53 rillig Exp $");
+MAKE_RCSID("$NetBSD: var.c,v 1.726 2020/12/12 19:31:17 rillig Exp $");
 
 /* A string that may need to be freed after use. */
 typedef struct FStr {
@@ -671,6 +671,28 @@ Var_ReexportVars(void)
 	free(val);
 }
 
+static void
+ExportVars(const char *varnames, Boolean isExport, VarExportFlags flags)
+{
+	if (varnames[0] != '\0') {
+		Words words = Str_Words(varnames, FALSE);
+
+		size_t i;
+		for (i = 0; i < words.len; i++) {
+			const char *name = words.words[i];
+			if (ExportVar(name, flags)) {
+				if (var_exportedVars == VAR_EXPORTED_NONE)
+					var_exportedVars = VAR_EXPORTED_SOME;
+				if (isExport && (flags & VAR_EXPORT_PARENT)) {
+					Var_Append(MAKE_EXPORTED, name,
+					    VAR_GLOBAL);
+				}
+			}
+		}
+		Words_Free(words);
+	}
+}
+
 /*
  * This is called when .export is seen or .MAKE.EXPORTED is modified.
  *
@@ -683,7 +705,7 @@ void
 Var_Export(const char *str, Boolean isExport)
 {
 	VarExportFlags flags;
-	char *val;
+	char *varnames;
 
 	if (isExport && str[0] == '\0') {
 		var_exportedVars = VAR_EXPORTED_ALL; /* use with caution! */
@@ -700,26 +722,10 @@ Var_Export(const char *str, Boolean isEx
 		flags = VAR_EXPORT_PARENT;
 	}
 
-	(void)Var_Subst(str, VAR_GLOBAL, VARE_WANTRES, &val);
+	(void)Var_Subst(str, VAR_GLOBAL, VARE_WANTRES, &varnames);
 	/* TODO: handle errors */
-	if (val[0] != '\0') {
-		Words words = Str_Words(val, FALSE);
-
-		size_t i;
-		for (i = 0; i < words.len; i++) {
-			const char *name = words.words[i];
-			if (ExportVar(name, flags)) {
-				if (var_exportedVars == VAR_EXPORTED_NONE)
-					var_exportedVars = VAR_EXPORTED_SOME;
-				if (isExport && (flags & VAR_EXPORT_PARENT)) {
-					Var_Append(MAKE_EXPORTED, name,
-					    VAR_GLOBAL);
-				}
-			}
-		}
-		Words_Free(words);
-	}
-	free(val);
+	ExportVars(varnames, isExport, flags);
+	free(varnames);
 }
 
 

Index: src/usr.bin/make/unit-tests/directive-export.exp
diff -u src/usr.bin/make/unit-tests/directive-export.exp:1.2 src/usr.bin/make/unit-tests/directive-export.exp:1.3
--- src/usr.bin/make/unit-tests/directive-export.exp:1.2	Tue Nov  3 17:17:31 2020
+++ src/usr.bin/make/unit-tests/directive-export.exp	Sat Dec 12 19:31:18 2020
@@ -1,4 +1,4 @@
-make: "directive-export.mk" line 25: Unknown directive "expor"
+make: "directive-export.mk" line 29: Unknown directive "expor"
 make: Fatal errors encountered -- cannot continue
 make: stopped in unit-tests
 exit status 1

Index: src/usr.bin/make/unit-tests/directive-export.mk
diff -u src/usr.bin/make/unit-tests/directive-export.mk:1.4 src/usr.bin/make/unit-tests/directive-export.mk:1.5
--- src/usr.bin/make/unit-tests/directive-export.mk:1.4	Tue Nov  3 17:17:31 2020
+++ src/usr.bin/make/unit-tests/directive-export.mk	Sat Dec 12 19:31:18 2020
@@ -1,4 +1,4 @@
-# $NetBSD: directive-export.mk,v 1.4 2020/11/03 17:17:31 rillig Exp $
+# $NetBSD: directive-export.mk,v 1.5 2020/12/12 19:31:18 rillig Exp $
 #
 # Tests for the .export directive.
 
@@ -7,6 +7,10 @@
 INDIRECT=	indirect
 VAR=		value $$ ${INDIRECT}
 
+# Before 2020-12-13, this unusual expression invoked undefined behavior since
+# it accessed out-of-bounds memory via Var_Export -> ExportVar -> MayExport.
+.export ${:U }
+
 # A variable is exported using the .export directive.
 # During that, its value is expanded, just like almost everywhere else.
 .export VAR

Reply via email to