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.
This commit is contained in:
rillig 2024-05-24 23:02:46 +00:00
parent e3cf24f959
commit 0ab57ba387
4 changed files with 36 additions and 9 deletions

View File

@ -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 *

View File

@ -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.

View File

@ -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

View File

@ -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