Module Name:    src
Committed By:   rillig
Date:           Fri May 24 23:02:46 UTC 2024

Modified Files:
        src/usr.bin/make: main.c test-variants.mk var.c
        src/usr.bin/make/unit-tests: Makefile

Log Message:
make: in -DCLEANUP mode, free variables and their values

The variables in the 3 scopes must be freed before the scopes themselves
are freed by Targ_End.

The test opt-m-include-dir creates a directory of the form '*.tmp', thus
it must be removed before attempting to only remove regular files of
this name.

POSIX requires setenv to copy the passed name and value, so there is no
need to keep that memory allocated any longer.


To generate a diff of this commit:
cvs rdiff -u -r1.616 -r1.617 src/usr.bin/make/main.c
cvs rdiff -u -r1.6 -r1.7 src/usr.bin/make/test-variants.mk
cvs rdiff -u -r1.1109 -r1.1110 src/usr.bin/make/var.c
cvs rdiff -u -r1.344 -r1.345 src/usr.bin/make/unit-tests/Makefile

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.616 src/usr.bin/make/main.c:1.617
--- src/usr.bin/make/main.c:1.616	Sun May 19 17:55:54 2024
+++ src/usr.bin/make/main.c	Fri May 24 23:02:46 2024
@@ -1,4 +1,4 @@
-/*	$NetBSD: main.c,v 1.616 2024/05/19 17:55:54 sjg Exp $	*/
+/*	$NetBSD: main.c,v 1.617 2024/05/24 23:02:46 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.616 2024/05/19 17:55:54 sjg Exp $");
+MAKE_RCSID("$NetBSD: main.c,v 1.617 2024/05/24 23:02:46 rillig Exp $");
 #if defined(MAKE_NATIVE)
 __COPYRIGHT("@(#) Copyright (c) 1988, 1989, 1990, 1993 "
 	    "The Regents of the University of California.  "
@@ -1583,9 +1583,9 @@ main_CleanUp(void)
 	meta_finish();
 #endif
 	Suff_End();
+	Var_End();
 	Targ_End();
 	Arch_End();
-	Var_End();
 	Parse_End();
 	Dir_End();
 	Job_End();
@@ -2119,6 +2119,7 @@ Main_ExportMAKEFLAGS(bool first)
 	/* TODO: handle errors */
 	if (flags[0] != '\0')
 		setenv("MAKEFLAGS", flags, 1);
+	free(flags);
 }
 
 char *

Index: src/usr.bin/make/test-variants.mk
diff -u src/usr.bin/make/test-variants.mk:1.6 src/usr.bin/make/test-variants.mk:1.7
--- src/usr.bin/make/test-variants.mk:1.6	Fri Mar  1 17:47:05 2024
+++ src/usr.bin/make/test-variants.mk	Fri May 24 23:02:46 2024
@@ -1,4 +1,4 @@
-# $NetBSD: test-variants.mk,v 1.6 2024/03/01 17:47:05 rillig Exp $
+# $NetBSD: test-variants.mk,v 1.7 2024/05/24 23:02:46 rillig Exp $
 #
 # Build several variants of make and run the tests on them.
 #
@@ -20,6 +20,11 @@ TESTS+=			default
 #SKIP.default=		yes
 #SKIP_TESTS.default=	varmod-subst
 
+TESTS+=			sanitize
+ENV.sanitize=		MKSANITIZER=yes LIMIT_RESOURCES=:
+CPPFLAGS.sanitize=	-DCLEANUP
+CFLAGS.sanitize=	-O2 -ggdb
+
 # Try a different compiler, with slightly different warnings and error
 # messages.  Clang has a few stricter checks than GCC, concerning enums
 # and non-literal format strings.

Index: src/usr.bin/make/var.c
diff -u src/usr.bin/make/var.c:1.1109 src/usr.bin/make/var.c:1.1110
--- src/usr.bin/make/var.c:1.1109	Tue May  7 18:26:22 2024
+++ src/usr.bin/make/var.c	Fri May 24 23:02:46 2024
@@ -1,4 +1,4 @@
-/*	$NetBSD: var.c,v 1.1109 2024/05/07 18:26:22 sjg Exp $	*/
+/*	$NetBSD: var.c,v 1.1110 2024/05/24 23:02:46 rillig Exp $	*/
 
 /*
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -132,7 +132,7 @@
 #include "metachar.h"
 
 /*	"@(#)var.c	8.3 (Berkeley) 3/19/94" */
-MAKE_RCSID("$NetBSD: var.c,v 1.1109 2024/05/07 18:26:22 sjg Exp $");
+MAKE_RCSID("$NetBSD: var.c,v 1.1110 2024/05/24 23:02:46 rillig Exp $");
 
 /*
  * Variables are defined using one of the VAR=value assignments.  Their
@@ -4753,11 +4753,32 @@ Var_Init(void)
 	SCOPE_CMDLINE = GNode_New("Command");
 }
 
+#ifdef CLEANUP
+static void
+Var_DeleteAll(GNode *scope)
+{
+	for (;;) {
+		HashIter hi;
+		HashIter_Init(&hi, &scope->vars);
+		if (!HashIter_Next(&hi))
+			return;
+		((Var *)hi.entry->value)->readOnly = false;
+		Var_Delete(scope, hi.entry->key);
+	}
+}
+
+#endif
+
 /* Clean up the variables module. */
 void
 Var_End(void)
 {
 	Var_Stats();
+#ifdef CLEANUP
+	Var_DeleteAll(SCOPE_CMDLINE);
+	Var_DeleteAll(SCOPE_GLOBAL);
+	Var_DeleteAll(SCOPE_INTERNAL);
+#endif
 }
 
 void

Index: src/usr.bin/make/unit-tests/Makefile
diff -u src/usr.bin/make/unit-tests/Makefile:1.344 src/usr.bin/make/unit-tests/Makefile:1.345
--- src/usr.bin/make/unit-tests/Makefile:1.344	Tue Apr 30 16:41:32 2024
+++ src/usr.bin/make/unit-tests/Makefile	Fri May 24 23:02:46 2024
@@ -1,4 +1,4 @@
-# $NetBSD: Makefile,v 1.344 2024/04/30 16:41:32 sjg Exp $
+# $NetBSD: Makefile,v 1.345 2024/05/24 23:02:46 rillig Exp $
 #
 # Unit tests for make(1)
 #
@@ -678,11 +678,11 @@ all: ${OUTFILES}
 CLEANFILES=		*.rawout *.out *.status *.tmp *.core *.tmp
 CLEANFILES+=		obj*.[och] lib*.a	# posix1.mk
 CLEANFILES+=		issue* .[ab]*		# suffixes.mk
-CLEANDIRS=		dir dummy		# posix1.mk
+CLEANDIRS=		dir dummy *.tmp		# posix1.mk
 
 clean:
-	rm -f ${CLEANFILES}
 	rm -rf ${CLEANDIRS}
+	rm -f ${CLEANFILES}
 
 TEST_MAKE?=	${.MAKE}
 TOOL_SED?=	sed

Reply via email to