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: fix double-free in CLEANUP mode (since 20...



details:   https://anonhg.NetBSD.org/src/rev/9e7865f51c27
branches:  trunk
changeset: 1029266:9e7865f51c27
user:      rillig <rillig%NetBSD.org@localhost>
date:      Tue Dec 28 21:56:13 2021 +0000

description:
make: fix double-free in CLEANUP mode (since 2021.12.27.23.11.55)

When make is run without the '-f' option, it searches for the files
'makefile' and 'Makefile' in the current directory.  The function
ReadFirstDefaultMakefile allocated memory for these filenames, added the
filenames to opts.makefiles and then freed the memory.  From that
moment, opts.makefiles contained dangling pointers.

The function main_CleanUp cleans the list, but only if make is compiled
with -DCLEANUP.  Since main.c 1.557 from 2021.12.27.23.11.55, the
strings in opts.makefiles are freed as well, before that, only the list
nodes were freed.  Freeing the strings led to the double-free.

Fix this bug by using a separate list for these short-lived strings.  At
the point where ReadFirstDefaultMakefile is called, opts.makefiles is
not used anymore, therefore there are no side effects.

To reproduce, run 'make test-coverage', which compiles with -DCLEANUP.
The test opt-chdir failed with a segmentation fault in main_Cleanup.
This test may be the only one that doesn't use the option '-f'.

diffstat:

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

diffs (48 lines):

diff -r 43145e2ff74e -r 9e7865f51c27 usr.bin/make/main.c
--- a/usr.bin/make/main.c       Tue Dec 28 19:43:42 2021 +0000
+++ b/usr.bin/make/main.c       Tue Dec 28 21:56:13 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: main.c,v 1.561 2021/12/28 01:20:24 rillig Exp $        */
+/*     $NetBSD: main.c,v 1.562 2021/12/28 21:56:13 rillig Exp $        */
 
 /*
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -111,7 +111,7 @@
 #include "trace.h"
 
 /*     "@(#)main.c     8.3 (Berkeley) 3/19/94" */
-MAKE_RCSID("$NetBSD: main.c,v 1.561 2021/12/28 01:20:24 rillig Exp $");
+MAKE_RCSID("$NetBSD: main.c,v 1.562 2021/12/28 21:56:13 rillig Exp $");
 #if defined(MAKE_NATIVE) && !defined(lint)
 __COPYRIGHT("@(#) Copyright (c) 1988, 1989, 1990, 1993 "
            "The Regents of the University of California.  "
@@ -1293,6 +1293,7 @@
 static void
 ReadFirstDefaultMakefile(void)
 {
+       StringList makefiles = LST_INIT;
        StringListNode *ln;
        char *prefs;
 
@@ -1300,18 +1301,13 @@
            SCOPE_CMDLINE, VARE_WANTRES, &prefs);
        /* TODO: handle errors */
 
-       /*
-        * XXX: This should use a local list instead of opts.makefiles since
-        * these makefiles do not come from the command line.  They also have
-        * different semantics in that only the first file that is found is
-        * processed.  See ReadAllMakefiles.
-        */
-       (void)str2Lst_Append(&opts.makefiles, prefs);
+       (void)str2Lst_Append(&makefiles, prefs);
 
-       for (ln = opts.makefiles.first; ln != NULL; ln = ln->next)
+       for (ln = makefiles.first; ln != NULL; ln = ln->next)
                if (ReadMakefile(ln->datum))
                        break;
 
+       Lst_Done(&makefiles);
        free(prefs);
 }
 



Home | Main Index | Thread Index | Old Index