Module Name:    src
Committed By:   rillig
Date:           Mon Dec 13 01:37:51 UTC 2021

Modified Files:
        src/usr.bin/make: var.c

Log Message:
make: clean up code for freeing short-lived variables

Since yesterday's addition of the short-lived "variable" named
.SUFFIXES, not only environment variables are short-lived.  Clean up the
code to prepare for fixing the remaining memory leaks.

No functional change.


To generate a diff of this commit:
cvs rdiff -u -r1.975 -r1.976 src/usr.bin/make/var.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/var.c
diff -u src/usr.bin/make/var.c:1.975 src/usr.bin/make/var.c:1.976
--- src/usr.bin/make/var.c:1.975	Mon Dec 13 01:00:10 2021
+++ src/usr.bin/make/var.c	Mon Dec 13 01:37:51 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: var.c,v 1.975 2021/12/13 01:00:10 rillig Exp $	*/
+/*	$NetBSD: var.c,v 1.976 2021/12/13 01:37:51 rillig Exp $	*/
 
 /*
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -140,7 +140,7 @@
 #include "metachar.h"
 
 /*	"@(#)var.c	8.3 (Berkeley) 3/19/94" */
-MAKE_RCSID("$NetBSD: var.c,v 1.975 2021/12/13 01:00:10 rillig Exp $");
+MAKE_RCSID("$NetBSD: var.c,v 1.976 2021/12/13 01:37:51 rillig Exp $");
 
 /*
  * Variables are defined using one of the VAR=value assignments.  Their
@@ -4456,18 +4456,16 @@ ParseVarnameLong(
 	return true;
 }
 
-/* Free the environment variable now since we own it. */
+/* Free the short-lived variable, since we own it. */
 static void
-FreeEnvVar(Var *v, Expr *expr)
+FreeShortLived(Var *v, Expr *expr)
 {
-	char *varValue = Buf_DoneData(&v->val);
-	if (expr->value.str == varValue)
-		expr->value.freeIt = varValue;
-	else
-		free(varValue);
-
-	FStr_Done(&v->name);
-	free(v);
+	if (expr->value.str == v->val.data) {
+		/* move ownership */
+		expr->value.freeIt = v->val.data;
+		v->val.data = NULL;
+	}
+	VarFreeEnv(v);
 }
 
 #if __STDC_VERSION__ >= 199901L
@@ -4658,7 +4656,7 @@ Var_Parse(const char **pp, GNode *scope,
 	*pp = p;
 
 	if (v->fromEnv) {
-		FreeEnvVar(v, &expr);
+		FreeShortLived(v, &expr);
 
 	} else if (expr.defined != DEF_REGULAR) {
 		if (expr.defined == DEF_UNDEF) {

Reply via email to