Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/usr.bin/make make(1): clean up freeing of environment variab...



details:   https://anonhg.NetBSD.org/src/rev/7a434c242e15
branches:  trunk
changeset: 978416:7a434c242e15
user:      rillig <rillig%NetBSD.org@localhost>
date:      Sat Nov 21 15:32:52 2020 +0000

description:
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.

diffstat:

 usr.bin/make/var.c |  21 +++++++++++++--------
 1 files changed, 13 insertions(+), 8 deletions(-)

diffs (42 lines):

diff -r 80d51ecd5438 -r 7a434c242e15 usr.bin/make/var.c
--- a/usr.bin/make/var.c        Sat Nov 21 15:30:06 2020 +0000
+++ b/usr.bin/make/var.c        Sat Nov 21 15:32:52 2020 +0000
@@ -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 @@
     *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)) {



Home | Main Index | Thread Index | Old Index