Module Name:    src
Committed By:   rillig
Date:           Sat Nov 21 15:32:52 UTC 2020

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

Log Message:
make(1): clean up freeing of environment variables in Var_Parse

The previous code with the extra boolean variable was a brain-twister
since the responsibility of freeing the memory was distributed over 3
different functions.


To generate a diff of this commit:
cvs rdiff -u -r1.691 -r1.692 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.691 src/usr.bin/make/var.c:1.692
--- src/usr.bin/make/var.c:1.691	Sat Nov 21 15:28:44 2020
+++ src/usr.bin/make/var.c	Sat Nov 21 15:32:52 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: var.c,v 1.691 2020/11/21 15:28:44 rillig Exp $	*/
+/*	$NetBSD: var.c,v 1.692 2020/11/21 15:32:52 rillig Exp $	*/
 
 /*
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -130,7 +130,7 @@
 #include "metachar.h"
 
 /*	"@(#)var.c	8.3 (Berkeley) 3/19/94" */
-MAKE_RCSID("$NetBSD: var.c,v 1.691 2020/11/21 15:28:44 rillig Exp $");
+MAKE_RCSID("$NetBSD: var.c,v 1.692 2020/11/21 15:32:52 rillig Exp $");
 
 #define VAR_DEBUG1(fmt, arg1) DEBUG1(VAR, fmt, arg1)
 #define VAR_DEBUG2(fmt, arg1, arg2) DEBUG2(VAR, fmt, arg1, arg2)
@@ -3925,12 +3925,17 @@ Var_Parse(const char **pp, GNode *ctxt, 
     *pp = p;
 
     if (v->flags & VAR_FROM_ENV) {
-	/* Free the environment variable now since we own it,
-	 * but don't free the variable value if it will be returned. */
-	Boolean keepValue = value == Buf_GetAll(&v->val, NULL);
-	if (keepValue)
-	    *out_val_freeIt = value;
-	(void)VarFreeEnv(v, !keepValue);
+	/* Free the environment variable now since we own it. */
+
+	char *varValue = Buf_Destroy(&v->val, FALSE);
+	if (value == varValue) {
+	    /* Don't free the variable value since it will be returned. */
+	    *out_val_freeIt = varValue;
+	} else
+	    free(varValue);
+
+	free(v->name_freeIt);
+	free(v);
 
     } else if (exprFlags & VEF_UNDEF) {
 	if (!(exprFlags & VEF_DEF)) {

Reply via email to