Module Name: src Committed By: rillig Date: Tue Dec 28 21:56:13 UTC 2021
Modified Files: src/usr.bin/make: main.c Log Message: 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'. To generate a diff of this commit: cvs rdiff -u -r1.561 -r1.562 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.561 src/usr.bin/make/main.c:1.562 --- src/usr.bin/make/main.c:1.561 Tue Dec 28 01:20:24 2021 +++ src/usr.bin/make/main.c Tue Dec 28 21:56:13 2021 @@ -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 @@ ReadAllMakefiles(const StringList *makef static void ReadFirstDefaultMakefile(void) { + StringList makefiles = LST_INIT; StringListNode *ln; char *prefs; @@ -1300,18 +1301,13 @@ ReadFirstDefaultMakefile(void) 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); }