Module Name:    src
Committed By:   rillig
Date:           Sat Aug 22 21:12:29 UTC 2020

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

Log Message:
make(1): fix undefined behavior when assigning to variable ""

Using a programming language with built-in array bounds checks would
have prevented this bug, and many others as well.


To generate a diff of this commit:
cvs rdiff -u -r1.258 -r1.259 src/usr.bin/make/parse.c
cvs rdiff -u -r1.3 -r1.4 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/parse.c
diff -u src/usr.bin/make/parse.c:1.258 src/usr.bin/make/parse.c:1.259
--- src/usr.bin/make/parse.c:1.258	Sat Aug 22 17:34:25 2020
+++ src/usr.bin/make/parse.c	Sat Aug 22 21:12:29 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: parse.c,v 1.258 2020/08/22 17:34:25 rillig Exp $	*/
+/*	$NetBSD: parse.c,v 1.259 2020/08/22 21:12:29 rillig Exp $	*/
 
 /*
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -69,14 +69,14 @@
  */
 
 #ifndef MAKE_NATIVE
-static char rcsid[] = "$NetBSD: parse.c,v 1.258 2020/08/22 17:34:25 rillig Exp $";
+static char rcsid[] = "$NetBSD: parse.c,v 1.259 2020/08/22 21:12:29 rillig Exp $";
 #else
 #include <sys/cdefs.h>
 #ifndef lint
 #if 0
 static char sccsid[] = "@(#)parse.c	8.3 (Berkeley) 3/19/94";
 #else
-__RCSID("$NetBSD: parse.c,v 1.258 2020/08/22 17:34:25 rillig Exp $");
+__RCSID("$NetBSD: parse.c,v 1.259 2020/08/22 21:12:29 rillig Exp $");
 #endif
 #endif /* not lint */
 #endif
@@ -1876,7 +1876,7 @@ Parse_DoVar(char *line, GNode *ctxt)
      * XXX Rather than counting () and {} we should look for $ and
      * then expand the variable.
      */
-    for (depth = 0, cp = line + 1; depth > 0 || *cp != '='; cp++) {
+    for (depth = 0, cp = line; depth > 0 || *cp != '='; cp++) {
 	if (*cp == '(' || *cp == '{') {
 	    depth++;
 	    continue;

Index: src/usr.bin/make/unit-tests/varname-empty.mk
diff -u src/usr.bin/make/unit-tests/varname-empty.mk:1.3 src/usr.bin/make/unit-tests/varname-empty.mk:1.4
--- src/usr.bin/make/unit-tests/varname-empty.mk:1.3	Sat Aug 22 21:02:56 2020
+++ src/usr.bin/make/unit-tests/varname-empty.mk	Sat Aug 22 21:12:29 2020
@@ -1,4 +1,4 @@
-# $NetBSD: varname-empty.mk,v 1.3 2020/08/22 21:02:56 rillig Exp $
+# $NetBSD: varname-empty.mk,v 1.4 2020/08/22 21:12:29 rillig Exp $
 #
 # Tests for the special variable with the empty name.
 #
@@ -9,7 +9,7 @@
 # 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.
 ?=	default
-#=	assigned	# XXX: probably undefined behavior
+=	assigned	# undefined behavior until 2020-08-22
 +=	appended
 :=	subst
 !=	echo 'value'

Reply via email to