Module Name:    src
Committed By:   rillig
Date:           Sun Oct  4 21:53:28 UTC 2020

Modified Files:
        src/usr.bin/make: parse.c
        src/usr.bin/make/unit-tests: varname-dot-curdir.mk

Log Message:
make(1): fix assignment to .CURDIR via the shell assignment operator

This is probably an edge case that nobody will ever stumble upon, since
.CURDIR is usually regarded as a read-only variable.

The other variable that is affected by this code path is .MAKE.EXPORTED,
and for this variable as well, it would be unusual to assign it a value
from a shell command.


To generate a diff of this commit:
cvs rdiff -u -r1.361 -r1.362 src/usr.bin/make/parse.c
cvs rdiff -u -r1.3 -r1.4 src/usr.bin/make/unit-tests/varname-dot-curdir.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/parse.c
diff -u src/usr.bin/make/parse.c:1.361 src/usr.bin/make/parse.c:1.362
--- src/usr.bin/make/parse.c:1.361	Sun Oct  4 21:41:44 2020
+++ src/usr.bin/make/parse.c	Sun Oct  4 21:53:28 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: parse.c,v 1.361 2020/10/04 21:41:44 rillig Exp $	*/
+/*	$NetBSD: parse.c,v 1.362 2020/10/04 21:53:28 rillig Exp $	*/
 
 /*
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -131,7 +131,7 @@
 #include "pathnames.h"
 
 /*	"@(#)parse.c	8.3 (Berkeley) 3/19/94"	*/
-MAKE_RCSID("$NetBSD: parse.c,v 1.361 2020/10/04 21:41:44 rillig Exp $");
+MAKE_RCSID("$NetBSD: parse.c,v 1.362 2020/10/04 21:53:28 rillig Exp $");
 
 /* types and constants */
 
@@ -1850,29 +1850,26 @@ VarAssign_Eval(VarAssign *var, GNode *ct
 
 	Var_Set(name, avalue, ctxt);
     } else if (type == VAR_SHELL) {
-	char *res;
-	const char *error;
-
-	if (strchr(uvalue, '$') != NULL) {
-	    char *evalue;
-	    /*
-	     * There's a dollar sign in the command, so perform variable
-	     * expansion on the whole thing. The resulting string will need
-	     * freeing when we're done.
-	     */
-	    (void)Var_Subst(uvalue, VAR_CMD, VARE_UNDEFERR|VARE_WANTRES,
-			    &evalue);
+        const char *cmd, *errfmt;
+        char *cmdOut;
+        void *cmd_freeIt = NULL;
+
+	cmd = uvalue;
+	if (strchr(cmd, '$') != NULL) {
+	    char *ecmd;
+	    (void)Var_Subst(cmd, VAR_CMD, VARE_UNDEFERR|VARE_WANTRES, &ecmd);
 	    /* TODO: handle errors */
-	    avalue = evalue;
-	    avalue_freeIt = evalue;
+	    cmd = cmd_freeIt = ecmd;
 	}
 
-	res = Cmd_Exec(avalue, &error);
-	Var_Set(name, res, ctxt);
-	free(res);
+	cmdOut = Cmd_Exec(cmd, &errfmt);
+	Var_Set(name, cmdOut, ctxt);
+	avalue = avalue_freeIt = cmdOut;
+
+	if (errfmt)
+	    Parse_Error(PARSE_WARNING, errfmt, cmd);
 
-	if (error)
-	    Parse_Error(PARSE_WARNING, error, avalue);
+	free(cmd_freeIt);
     } else {
 	if (type == VAR_DEFAULT && Var_Exists(var->varname, ctxt)) {
 	    *out_avalue_freeIt = NULL;

Index: src/usr.bin/make/unit-tests/varname-dot-curdir.mk
diff -u src/usr.bin/make/unit-tests/varname-dot-curdir.mk:1.3 src/usr.bin/make/unit-tests/varname-dot-curdir.mk:1.4
--- src/usr.bin/make/unit-tests/varname-dot-curdir.mk:1.3	Sun Oct  4 20:06:48 2020
+++ src/usr.bin/make/unit-tests/varname-dot-curdir.mk	Sun Oct  4 21:53:28 2020
@@ -1,15 +1,17 @@
-# $NetBSD: varname-dot-curdir.mk,v 1.3 2020/10/04 20:06:48 rillig Exp $
+# $NetBSD: varname-dot-curdir.mk,v 1.4 2020/10/04 21:53:28 rillig Exp $
 #
 # Tests for the special .CURDIR variable.
 
 # TODO: Implementation
 
-# As of 2020-10-04, assigning the result of a shell command to .CURDIR tries
-# to add the shell command to the .PATH instead of the output of the shell
-# command.  Since "echo /" does not exist, the .PATH is left unmodified.
-# See Parse_DoVar at the very bottom.
+# Until 2020-10-04, assigning the result of a shell assignment to .CURDIR
+# tried to add the shell command ("echo /") to the .PATH instead of the
+# output of the shell command ("/").  Since "echo /" does not exist, the
+# .PATH was left unmodified.  See VarAssign_Eval.
+#
+# Since 2020-10-04, the output of the shell command is added to .PATH.
 .CURDIR!=	echo /
-.if ${.PATH:M/}
+.if ${.PATH:M/} != "/"
 .  error
 .endif
 
@@ -17,7 +19,7 @@
 # Appending to .CURDIR does not make sense, therefore it doesn't matter that
 # this code path is buggy as well.
 .CURDIR=	/
-.if !${.PATH:M/}
+.if ${.PATH:M/} != "/"
 .  error
 .endif
 

Reply via email to