Source-Changes-HG archive

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

[src/trunk]: src/lib/libc/stdlib Handle the case where a program attempted to...



details:   https://anonhg.NetBSD.org/src/rev/bda31e532fb1
branches:  trunk
changeset: 758365:bda31e532fb1
user:      christos <christos%NetBSD.org@localhost>
date:      Wed Nov 03 15:01:07 2010 +0000

description:
Handle the case where a program attempted to cleanup the environment by
setting *environ = NULL;

diffstat:

 lib/libc/stdlib/getenv.c |  26 ++++++++++++++++++++++++--
 lib/libc/stdlib/local.h  |   3 ++-
 lib/libc/stdlib/putenv.c |   6 ++++--
 lib/libc/stdlib/setenv.c |  17 +++++++++++------
 4 files changed, 41 insertions(+), 11 deletions(-)

diffs (156 lines):

diff -r 237a1b60239b -r bda31e532fb1 lib/libc/stdlib/getenv.c
--- a/lib/libc/stdlib/getenv.c  Wed Nov 03 14:37:56 2010 +0000
+++ b/lib/libc/stdlib/getenv.c  Wed Nov 03 15:01:07 2010 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: getenv.c,v 1.28 2010/11/02 03:44:05 enami Exp $        */
+/*     $NetBSD: getenv.c,v 1.29 2010/11/03 15:01:07 christos Exp $     */
 
 /*
  * Copyright (c) 1987, 1993
@@ -34,7 +34,7 @@
 #if 0
 static char sccsid[] = "@(#)getenv.c   8.1 (Berkeley) 6/4/93";
 #else
-__RCSID("$NetBSD: getenv.c,v 1.28 2010/11/02 03:44:05 enami Exp $");
+__RCSID("$NetBSD: getenv.c,v 1.29 2010/11/03 15:01:07 christos Exp $");
 #endif
 #endif /* LIBC_SCCS and not lint */
 
@@ -154,6 +154,28 @@
 }
 
 /*
+ * Handle the case where a program tried to cleanup the environment
+ * by setting *environ = NULL; we attempt to cleanup all the malloced
+ * environ entries and we make sure that the entry following the new
+ * entry is NULL.
+ */
+void
+__scrubenv(int offset)
+{
+       if (environ[++offset] == NULL)
+               return;
+
+       while (environ[offset] &&
+           environ[offset] == __environ_malloced[offset]) {
+               free(__environ_malloced[offset]);
+               environ[offset] = __environ_malloced[offset] = NULL;
+               offset++;
+       }
+
+       environ[offset] = __environ_malloced[offset] = NULL;
+}
+
+/*
  * __findenv --
  *     Returns pointer to value associated with name, if any, else NULL.
  *     Sets offset to be the offset of the name/value combination in the
diff -r 237a1b60239b -r bda31e532fb1 lib/libc/stdlib/local.h
--- a/lib/libc/stdlib/local.h   Wed Nov 03 14:37:56 2010 +0000
+++ b/lib/libc/stdlib/local.h   Wed Nov 03 15:01:07 2010 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: local.h,v 1.4 2010/09/25 18:11:40 tron Exp $   */
+/*     $NetBSD: local.h,v 1.5 2010/11/03 15:01:07 christos Exp $       */
 
 /*
  * Copyright (c) 1997 Christos Zoulas.  All rights reserved.
@@ -26,6 +26,7 @@
 
 char *__findenv(const char *, int *);
 int __allocenv(int);
+void __scrubenv(int);
 
 #ifdef _REENTRANT
 extern rwlock_t __environ_lock;
diff -r 237a1b60239b -r bda31e532fb1 lib/libc/stdlib/putenv.c
--- a/lib/libc/stdlib/putenv.c  Wed Nov 03 14:37:56 2010 +0000
+++ b/lib/libc/stdlib/putenv.c  Wed Nov 03 15:01:07 2010 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: putenv.c,v 1.17 2010/10/25 20:35:36 njoly Exp $        */
+/*     $NetBSD: putenv.c,v 1.18 2010/11/03 15:01:07 christos Exp $     */
 
 /*-
  * Copyright (c) 1988, 1993
@@ -34,7 +34,7 @@
 #if 0
 static char sccsid[] = "@(#)putenv.c   8.2 (Berkeley) 3/27/94";
 #else
-__RCSID("$NetBSD: putenv.c,v 1.17 2010/10/25 20:35:36 njoly Exp $");
+__RCSID("$NetBSD: putenv.c,v 1.18 2010/11/03 15:01:07 christos Exp $");
 #endif
 #endif /* LIBC_SCCS and not lint */
 
@@ -78,6 +78,8 @@
        }
 
        environ[offset] = str;
+       if (p == NULL)
+               __scrubenv(offset);
        rwlock_unlock(&__environ_lock);
        return 0;
 bad:
diff -r 237a1b60239b -r bda31e532fb1 lib/libc/stdlib/setenv.c
--- a/lib/libc/stdlib/setenv.c  Wed Nov 03 14:37:56 2010 +0000
+++ b/lib/libc/stdlib/setenv.c  Wed Nov 03 15:01:07 2010 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: setenv.c,v 1.41 2010/10/16 11:23:41 njoly Exp $        */
+/*     $NetBSD: setenv.c,v 1.42 2010/11/03 15:01:07 christos Exp $     */
 
 /*
  * Copyright (c) 1987, 1993
@@ -34,7 +34,7 @@
 #if 0
 static char sccsid[] = "@(#)setenv.c   8.1 (Berkeley) 6/4/93";
 #else
-__RCSID("$NetBSD: setenv.c,v 1.41 2010/10/16 11:23:41 njoly Exp $");
+__RCSID("$NetBSD: setenv.c,v 1.42 2010/11/03 15:01:07 christos Exp $");
 #endif
 #endif /* LIBC_SCCS and not lint */
 
@@ -59,7 +59,7 @@
 int
 setenv(const char *name, const char *value, int rewrite)
 {
-       char *c;
+       char *c, *f;
        size_t l_value, size;
        int offset;
 
@@ -81,14 +81,14 @@
                return -1;
 
        /* find if already exists */
-       c = __findenv(name, &offset);
+       f = __findenv(name, &offset);
 
        if (__allocenv(offset) == -1)
                goto bad;
 
        l_value = strlen(value);
 
-       if (c != NULL) {
+       if (f != NULL) {
                if (!rewrite)
                        goto good;
                /*
@@ -97,7 +97,8 @@
                 * existing value.
                 */
                if (environ[offset] == __environ_malloced[offset] &&
-                   strlen(c) >= l_value) {
+                   strlen(f) >= l_value) {
+                       c = f;
                        goto copy;
                }
        }
@@ -110,6 +111,10 @@
 
        environ[offset] = c;
        __environ_malloced[offset] = c;
+
+       if (f == NULL)
+               __scrubenv(offset);
+
        (void)memcpy(c, name, size);
        c += size;
        *c++ = '=';



Home | Main Index | Thread Index | Old Index