Module Name:    src
Committed By:   rillig
Date:           Sat Oct  3 10:42:09 UTC 2020

Modified Files:
        src/usr.bin/make: var.c
        src/usr.bin/make/unit-tests: make-exported.mk

Log Message:
make(1): ignore -env and -literal in assignments to .MAKE.EXPORTED

Previously, assigning the string "-env" to the variable .MAKE.EXPORTED
had the same effect as the .export-env directive.  This was only due to
a sloppy implementation, not by design.

For the string "-literal" and the directive .export-literal, the
situation was even worse since the actually executed code was a wild
mixture between .export and .export-literal that in the end exported the
expanded form of the variable.  Therefore there was no practical use
case of this implementation flaw.


To generate a diff of this commit:
cvs rdiff -u -r1.558 -r1.559 src/usr.bin/make/var.c
cvs rdiff -u -r1.3 -r1.4 src/usr.bin/make/unit-tests/make-exported.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.558 src/usr.bin/make/var.c:1.559
--- src/usr.bin/make/var.c:1.558	Sat Oct  3 10:31:05 2020
+++ src/usr.bin/make/var.c	Sat Oct  3 10:42:08 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: var.c,v 1.558 2020/10/03 10:31:05 rillig Exp $	*/
+/*	$NetBSD: var.c,v 1.559 2020/10/03 10:42:08 rillig Exp $	*/
 
 /*
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -121,7 +121,7 @@
 #include    "metachar.h"
 
 /*	"@(#)var.c	8.3 (Berkeley) 3/19/94" */
-MAKE_RCSID("$NetBSD: var.c,v 1.558 2020/10/03 10:31:05 rillig Exp $");
+MAKE_RCSID("$NetBSD: var.c,v 1.559 2020/10/03 10:42:08 rillig Exp $");
 
 #define VAR_DEBUG1(fmt, arg1) DEBUG1(VAR, fmt, arg1)
 #define VAR_DEBUG2(fmt, arg1, arg2) DEBUG2(VAR, fmt, arg1, arg2)
@@ -603,10 +603,10 @@ Var_Export(const char *str, Boolean isEx
 	return;
     }
 
-    if (strncmp(str, "-env", 4) == 0) {
+    if (isExport && strncmp(str, "-env", 4) == 0) {
 	str += 4;
     	flags = 0;
-    } else if (strncmp(str, "-literal", 8) == 0) {
+    } else if (isExport && strncmp(str, "-literal", 8) == 0) {
 	str += 8;
 	flags = VAR_EXPORT_LITERAL;
     } else {

Index: src/usr.bin/make/unit-tests/make-exported.mk
diff -u src/usr.bin/make/unit-tests/make-exported.mk:1.3 src/usr.bin/make/unit-tests/make-exported.mk:1.4
--- src/usr.bin/make/unit-tests/make-exported.mk:1.3	Sat Oct  3 10:31:05 2020
+++ src/usr.bin/make/unit-tests/make-exported.mk	Sat Oct  3 10:42:09 2020
@@ -1,4 +1,4 @@
-# $NetBSD: make-exported.mk,v 1.3 2020/10/03 10:31:05 rillig Exp $
+# $NetBSD: make-exported.mk,v 1.4 2020/10/03 10:42:09 rillig Exp $
 #
 # As of 2020-08-09, the code in Var_Export is shared between the .export
 # directive and the .MAKE.EXPORTED variable.  This leads to non-obvious
@@ -8,23 +8,17 @@
 -literal=	make-exported-value-literal
 UT_VAR=		${UNEXPANDED}
 
-# The following behavior is probably not intended.
-.MAKE.EXPORTED=		-env		# behaves like .export-env
+# Before 2020-10-03, the following line took the code path of .export-env,
+# which was surprising behavior.  Since 2020-10-03 this line tries to
+# export the variable named "-env", but that is rejected because the
+# variable name starts with a hyphen.
+.MAKE.EXPORTED=		-env
 
-# If the value of .MAKE.EXPORTED starts with "-literal", make behaves like
-# a mixture of .export-literal and a regular .export.
-# XXX: This is due to a sloppy implementation, reusing code in places where
-# it is not appropriate.
+# Before 2020-10-03, if the value of .MAKE.EXPORTED started with "-literal",
+# make behaved like a mixture of .export-literal and a regular .export.
 #
-# In Parse_DoVar, the code path for MAKE_EXPORTED is taken, calling Var_Export
-# in turn.  There, the code path for .export-literal is taken, and the
-# environment variable UT_VAR is set to ${UNEXPANDED}, as expected.
-# Later, in Compat_RunCommand, in the child process after vfork,
-# Var_ExportVars is called, which treats "-literal" as an ordinary variable
-# name, therefore exports it and also overwrites the previously exported
-# UT_VAR with the expanded value.
-#
-# Since 2020-10-03, the "variable" named "-literal" is not exported anymore.
+# Since 2020-10-03, the "variable" named "-literal" is not exported anymore,
+# it is just ignored since its name starts with '-'.
 .MAKE.EXPORTED=		-literal UT_VAR
 
 all:

Reply via email to