Source-Changes-HG archive

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

[src/trunk]: src/lib/libc/stdlib Make sure that all extra elements of the env...



details:   https://anonhg.NetBSD.org/src/rev/9537404ef4a5
branches:  trunk
changeset: 757898:9537404ef4a5
user:      tron <tron%NetBSD.org@localhost>
date:      Sat Oct 02 10:51:07 2010 +0000

description:
Make sure that all extra elements of the environment vector are set
to NULL. This stop su(1) from crashing.

diffstat:

 lib/libc/stdlib/getenv.c |  9 ++++++---
 1 files changed, 6 insertions(+), 3 deletions(-)

diffs (38 lines):

diff -r 874e088a191b -r 9537404ef4a5 lib/libc/stdlib/getenv.c
--- a/lib/libc/stdlib/getenv.c  Sat Oct 02 10:05:55 2010 +0000
+++ b/lib/libc/stdlib/getenv.c  Sat Oct 02 10:51:07 2010 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: getenv.c,v 1.24 2010/10/01 20:11:32 christos Exp $     */
+/*     $NetBSD: getenv.c,v 1.25 2010/10/02 10:51:07 tron 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.24 2010/10/01 20:11:32 christos Exp $");
+__RCSID("$NetBSD: getenv.c,v 1.25 2010/10/02 10:51:07 tron Exp $");
 #endif
 #endif /* LIBC_SCCS and not lint */
 
@@ -120,15 +120,18 @@
        if (saveenv == environ) {               /* just increase size */
                if ((p = realloc(saveenv, nl * sizeof(*p))) == NULL)
                        return -1;
+               (void)memset(&p[environ_malloced_len], 0,
+                   (nl - environ_malloced_len) * sizeof(*p));
                saveenv = p;
        } else {                                /* get new space */
                free(saveenv);
                if ((saveenv = malloc(nl * sizeof(*saveenv))) == NULL)
                        return -1;
                (void)memcpy(saveenv, environ, (nl - 2) * sizeof(*saveenv));
+               saveenv[nl - 2] = NULL;
+               saveenv[nl - 1] = NULL;
        }
        environ = saveenv;
-       environ[offset + 1] = NULL;
 
        p = realloc(__environ_malloced, nl * sizeof(*p));
        if (p == NULL)



Home | Main Index | Thread Index | Old Index