Source-Changes-HG archive

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

[src/trunk]: src/usr.bin/make Var_UnExport() and setenv() can both realloc en...



details:   https://anonhg.NetBSD.org/src/rev/02ec7a1323a0
branches:  trunk
changeset: 779596:02ec7a1323a0
user:      sjg <sjg%NetBSD.org@localhost>
date:      Mon Jun 04 20:34:20 2012 +0000

description:
Var_UnExport() and setenv() can both realloc environ.
Use a common variable (savedEnv) to track that to avoid wasting memory.
Also, if providing setenv and unsetenv, do getenv too to ensure a consistent
set.

diffstat:

 usr.bin/make/make.h |   3 ++-
 usr.bin/make/util.c |  27 +++++++++++++++++----------
 usr.bin/make/var.c  |  23 ++++++++++++++---------
 3 files changed, 33 insertions(+), 20 deletions(-)

diffs (158 lines):

diff -r 526e311a8007 -r 02ec7a1323a0 usr.bin/make/make.h
--- a/usr.bin/make/make.h       Mon Jun 04 20:19:28 2012 +0000
+++ b/usr.bin/make/make.h       Mon Jun 04 20:34:20 2012 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: make.h,v 1.87 2011/09/16 15:38:04 joerg Exp $  */
+/*     $NetBSD: make.h,v 1.88 2012/06/04 20:34:20 sjg Exp $    */
 
 /*
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -401,6 +401,7 @@
 extern char    curdir[];       /* Startup directory */
 extern char    *progname;      /* The program name */
 extern char    *makeDependfile; /* .depend */
+extern char    **savedEnv;      /* if we replaced environ this will be non-NULL */
 
 /*
  * We cannot vfork() in a child of vfork().
diff -r 526e311a8007 -r 02ec7a1323a0 usr.bin/make/util.c
--- a/usr.bin/make/util.c       Mon Jun 04 20:19:28 2012 +0000
+++ b/usr.bin/make/util.c       Mon Jun 04 20:34:20 2012 +0000
@@ -1,15 +1,15 @@
-/*     $NetBSD: util.c,v 1.51 2011/04/02 07:58:30 mbalmer Exp $        */
+/*     $NetBSD: util.c,v 1.52 2012/06/04 20:34:20 sjg Exp $    */
 
 /*
  * Missing stuff from OS's
  */
 
 #ifndef MAKE_NATIVE
-static char rcsid[] = "$NetBSD: util.c,v 1.51 2011/04/02 07:58:30 mbalmer Exp $";
+static char rcsid[] = "$NetBSD: util.c,v 1.52 2012/06/04 20:34:20 sjg Exp $";
 #else
 #include <sys/cdefs.h>
 #ifndef lint
-__RCSID("$NetBSD: util.c,v 1.51 2011/04/02 07:58:30 mbalmer Exp $");
+__RCSID("$NetBSD: util.c,v 1.52 2012/06/04 20:34:20 sjg Exp $");
 #endif
 #endif
 
@@ -61,6 +61,14 @@
        return NULL;
 }
 
+char *
+getenv(const char *name)
+{
+    int offset;
+
+    return(findenv(name, &offset));
+}
+
 int
 unsetenv(const char *name)
 {
@@ -83,7 +91,6 @@
 int
 setenv(const char *name, const char *value, int rewrite)
 {
-       static char **saveenv;  /* copy of previously allocated space */
        char *c, **newenv;
        const char *cc;
        size_t l_value, size;
@@ -106,20 +113,20 @@
                        goto copy;
        } else {                                        /* create new slot */
                size = sizeof(char *) * (offset + 2);
-               if (saveenv == environ) {               /* just increase size */
-                       if ((newenv = realloc(saveenv, size)) == NULL)
+               if (savedEnv == environ) {              /* just increase size */
+                       if ((newenv = realloc(savedEnv, size)) == NULL)
                                return -1;
-                       saveenv = newenv;
+                       savedEnv = newenv;
                } else {                                /* get new space */
                        /*
                         * We don't free here because we don't know if
                         * the first allocation is valid on all OS's
                         */
-                       if ((saveenv = malloc(size)) == NULL)
+                       if ((savedEnv = malloc(size)) == NULL)
                                return -1;
-                       (void)memcpy(saveenv, environ, size - sizeof(char *));
+                       (void)memcpy(savedEnv, environ, size - sizeof(char *));
                }
-               environ = saveenv;
+               environ = savedEnv;
                environ[offset + 1] = NULL;
        }
        for (cc = name; *cc && *cc != '='; ++cc)        /* no `=' in name */
diff -r 526e311a8007 -r 02ec7a1323a0 usr.bin/make/var.c
--- a/usr.bin/make/var.c        Mon Jun 04 20:19:28 2012 +0000
+++ b/usr.bin/make/var.c        Mon Jun 04 20:34:20 2012 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: var.c,v 1.169 2012/05/18 02:28:16 sjg Exp $    */
+/*     $NetBSD: var.c,v 1.170 2012/06/04 20:34:20 sjg Exp $    */
 
 /*
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -69,14 +69,14 @@
  */
 
 #ifndef MAKE_NATIVE
-static char rcsid[] = "$NetBSD: var.c,v 1.169 2012/05/18 02:28:16 sjg Exp $";
+static char rcsid[] = "$NetBSD: var.c,v 1.170 2012/06/04 20:34:20 sjg Exp $";
 #else
 #include <sys/cdefs.h>
 #ifndef lint
 #if 0
 static char sccsid[] = "@(#)var.c      8.3 (Berkeley) 3/19/94";
 #else
-__RCSID("$NetBSD: var.c,v 1.169 2012/05/18 02:28:16 sjg Exp $");
+__RCSID("$NetBSD: var.c,v 1.170 2012/06/04 20:34:20 sjg Exp $");
 #endif
 #endif /* not lint */
 #endif
@@ -140,6 +140,12 @@
 #include    "job.h"
 
 /*
+ * This lets us tell if we have replaced the original environ
+ * (which we cannot free).
+ */
+char **savedEnv = NULL;
+
+/*
  * This is a harmless return value for Var_Parse that can be used by Var_Subst
  * to determine if there was an error in parsing -- easier than returning
  * a flag, as things outside this module don't give a hoot.
@@ -762,24 +768,23 @@
     str += 8;
     unexport_env = (strncmp(str, "-env", 4) == 0);
     if (unexport_env) {
-       static char **savenv;
        char **newenv;
 
        cp = getenv(MAKE_LEVEL);        /* we should preserve this */
-       if (environ == savenv) {
+       if (environ == savedEnv) {
            /* we have been here before! */
            newenv = bmake_realloc(environ, 2 * sizeof(char *));
        } else {
-           if (savenv) {
-               free(savenv);
-               savenv = NULL;
+           if (savedEnv) {
+               free(savedEnv);
+               savedEnv = NULL;
            }
            newenv = bmake_malloc(2 * sizeof(char *));
        }
        if (!newenv)
            return;
        /* Note: we cannot safely free() the original environ. */
-       environ = savenv = newenv;
+       environ = savedEnv = newenv;
        newenv[0] = NULL;
        newenv[1] = NULL;
        setenv(MAKE_LEVEL, cp, 1);



Home | Main Index | Thread Index | Old Index