Module Name: src
Committed By: tron
Date: Thu Sep 30 12:41:33 UTC 2010
Modified Files:
src/lib/libc/stdlib: setenv.c unsetenv.c
Log Message:
Be slightly more careful about freeing memory allocated for environment
variables: only free memory if the current value points to the same
memory area as the allocated block. This will prevent crashes if an
application changes the order of the environment array.
Unfortunately this is still not enough to stop zsh 4.2.* from crashing.
zsh 4.3.* works fine before and after this change.
To generate a diff of this commit:
cvs rdiff -u -r1.37 -r1.38 src/lib/libc/stdlib/setenv.c
cvs rdiff -u -r1.8 -r1.9 src/lib/libc/stdlib/unsetenv.c
Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.
Modified files:
Index: src/lib/libc/stdlib/setenv.c
diff -u src/lib/libc/stdlib/setenv.c:1.37 src/lib/libc/stdlib/setenv.c:1.38
--- src/lib/libc/stdlib/setenv.c:1.37 Sat Sep 25 19:10:37 2010
+++ src/lib/libc/stdlib/setenv.c Thu Sep 30 12:41:33 2010
@@ -1,4 +1,4 @@
-/* $NetBSD: setenv.c,v 1.37 2010/09/25 19:10:37 tron Exp $ */
+/* $NetBSD: setenv.c,v 1.38 2010/09/30 12:41:33 tron 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.37 2010/09/25 19:10:37 tron Exp $");
+__RCSID("$NetBSD: setenv.c,v 1.38 2010/09/30 12:41:33 tron Exp $");
#endif
#endif /* LIBC_SCCS and not lint */
@@ -112,14 +112,15 @@
if ((c = malloc(size + l_value + 2)) == NULL)
goto bad;
+ if (environ[offset] == __environ_malloced[offset])
+ free(__environ_malloced[offset]);
+
environ[offset] = c;
+ __environ_malloced[offset] = c;
(void)memcpy(c, name, size);
c += size;
*c++ = '=';
- free(__environ_malloced[offset]);
- __environ_malloced[offset] = environ[offset];
-
copy:
(void)memcpy(c, value, l_value + 1);
good:
Index: src/lib/libc/stdlib/unsetenv.c
diff -u src/lib/libc/stdlib/unsetenv.c:1.8 src/lib/libc/stdlib/unsetenv.c:1.9
--- src/lib/libc/stdlib/unsetenv.c:1.8 Wed Sep 29 00:40:17 2010
+++ src/lib/libc/stdlib/unsetenv.c Thu Sep 30 12:41:33 2010
@@ -1,4 +1,4 @@
-/* $NetBSD: unsetenv.c,v 1.8 2010/09/29 00:40:17 enami Exp $ */
+/* $NetBSD: unsetenv.c,v 1.9 2010/09/30 12:41:33 tron Exp $ */
/*
* Copyright (c) 1987, 1993
@@ -34,7 +34,7 @@
#if 0
static char sccsid[] = "from: @(#)setenv.c 8.1 (Berkeley) 6/4/93";
#else
-__RCSID("$NetBSD: unsetenv.c,v 1.8 2010/09/29 00:40:17 enami Exp $");
+__RCSID("$NetBSD: unsetenv.c,v 1.9 2010/09/30 12:41:33 tron Exp $");
#endif
#endif /* LIBC_SCCS and not lint */
@@ -73,7 +73,8 @@
}
while (__findenv(name, &offset) != NULL) { /* if set multiple times */
- free(__environ_malloced[offset]);
+ if (environ[offset] == __environ_malloced[offset])
+ free(__environ_malloced[offset]);
while (environ[offset] != NULL) {
environ[offset] = environ[offset + 1];