Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/usr.bin/make make(1): fix undefined behavior for .MAKEFLAGS:...



details:   https://anonhg.NetBSD.org/src/rev/b156846735a5
branches:  trunk
changeset: 955278:b156846735a5
user:      rillig <rillig%NetBSD.org@localhost>
date:      Fri Sep 25 23:18:59 2020 +0000

description:
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.

diffstat:

 usr.bin/make/main.c |  25 +++++++++----------------
 1 files changed, 9 insertions(+), 16 deletions(-)

diffs (62 lines):

diff -r bd2bcd8c932b -r b156846735a5 usr.bin/make/main.c
--- a/usr.bin/make/main.c       Fri Sep 25 21:13:44 2020 +0000
+++ b/usr.bin/make/main.c       Fri Sep 25 23:18:59 2020 +0000
@@ -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 @@
        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 @@
                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 @@
                        break;
                case 'f':
                        if (argvalue == NULL) goto noarg;
-                       Lst_Append(makefiles, argvalue);
+                       Lst_Append(makefiles, bmake_strdup(argvalue));
                        break;
                case 'i':
                        ignoreErrors = TRUE;



Home | Main Index | Thread Index | Old Index