Module Name:    src
Committed By:   rillig
Date:           Fri Sep 25 23:18:59 UTC 2020

Modified Files:
        src/usr.bin/make: main.c

Log Message:
make(1): fix undefined behavior for .MAKEFLAGS: -f file

Since at least 1993-03-21, adding other makefiles in a .MAKEFILES
dependency has invoked undefined behavior because the command line
arguments were copied directly into the global makefiles variable,
without a proper strdup.  Shortly after that, the word list created by
Str_Words (formerly brk_string) was freed.

This applies to both the -f and the -v and -V options.  Luckily it is an
edge case to use these options in .MAKEFLAGS at all.

The -T option had already been fixed at 2000-12-30, but not the other
options.


To generate a diff of this commit:
cvs rdiff -u -r1.344 -r1.345 src/usr.bin/make/main.c

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/main.c
diff -u src/usr.bin/make/main.c:1.344 src/usr.bin/make/main.c:1.345
--- src/usr.bin/make/main.c:1.344	Fri Sep 25 19:40:23 2020
+++ src/usr.bin/make/main.c	Fri Sep 25 23:18:59 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: main.c,v 1.344 2020/09/25 19:40:23 rillig Exp $	*/
+/*	$NetBSD: main.c,v 1.345 2020/09/25 23:18:59 rillig Exp $	*/
 
 /*
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -126,7 +126,7 @@
 #endif
 
 /*	"@(#)main.c	8.3 (Berkeley) 3/19/94"	*/
-MAKE_RCSID("$NetBSD: main.c,v 1.344 2020/09/25 19:40:23 rillig Exp $");
+MAKE_RCSID("$NetBSD: main.c,v 1.345 2020/09/25 23:18:59 rillig Exp $");
 #if defined(MAKE_NATIVE) && !defined(lint)
 __COPYRIGHT("@(#) Copyright (c) 1988, 1989, 1990, 1993\
  The Regents of the University of California.  All rights reserved.");
@@ -389,20 +389,13 @@ is_relpath(const char *path)
 	return FALSE;
 }
 
-/*-
- * MainParseArgs --
- *	Parse a given argument vector. Called from main() and from
- *	Main_ParseArgLine() when the .MAKEFLAGS target is used.
- *
- *	XXX: Deal with command line overriding .MAKEFLAGS in makefile
+/* Parse the given arguments.  Called from main() and from
+ * Main_ParseArgLine() when the .MAKEFLAGS target is used.
  *
- * Results:
- *	None
+ * The arguments must be treated as read-only and will be freed after the
+ * call.
  *
- * Side Effects:
- *	Various global and local flags will be set depending on the flags
- *	given
- */
+ * XXX: Deal with command line overriding .MAKEFLAGS in makefile */
 static void
 MainParseArgs(int argc, char **argv)
 {
@@ -543,7 +536,7 @@ rearg:
 		case 'v':
 			if (argvalue == NULL) goto noarg;
 			printVars = c == 'v' ? EXPAND_VARS : COMPAT_VARS;
-			Lst_Append(variables, argvalue);
+			Lst_Append(variables, bmake_strdup(argvalue));
 			Var_Append(MAKEFLAGS, "-V", VAR_GLOBAL);
 			Var_Append(MAKEFLAGS, argvalue, VAR_GLOBAL);
 			break;
@@ -571,7 +564,7 @@ rearg:
 			break;
 		case 'f':
 			if (argvalue == NULL) goto noarg;
-			Lst_Append(makefiles, argvalue);
+			Lst_Append(makefiles, bmake_strdup(argvalue));
 			break;
 		case 'i':
 			ignoreErrors = TRUE;

Reply via email to