summaryrefslogtreecommitdiff
path: root/lib/libc/stdlib/setenv.c
diff options
context:
space:
mode:
authortron <tron@NetBSD.org>2010-09-25 18:11:40 +0000
committertron <tron@NetBSD.org>2010-09-25 18:11:40 +0000
commit5cdca2e60020da2dfd58990aada65b6d71aab939 (patch)
treed558847cd606f13e38a295044af4770a0d99a4d0 /lib/libc/stdlib/setenv.c
parent15ed98d439d987f17cc3d8d7e7a1d20194be0586 (diff)
Remember memory used by allocated environment variables instead of
using a bitmap. This deals with the case where a variable is first set via setenv(3) or putenv(3), then overwritten by changing "environ" directory and afterwards overwritten with setenv(3) again. This stops "zsh" from crashing under NetBSD-current. Code reviewed by Christos Zoulas.
Diffstat (limited to 'lib/libc/stdlib/setenv.c')
-rw-r--r--lib/libc/stdlib/setenv.c20
1 files changed, 10 insertions, 10 deletions
diff --git a/lib/libc/stdlib/setenv.c b/lib/libc/stdlib/setenv.c
index e9352d85ef9..07e8ff4866b 100644
--- a/lib/libc/stdlib/setenv.c
+++ b/lib/libc/stdlib/setenv.c
@@ -1,4 +1,4 @@
-/* $NetBSD: setenv.c,v 1.35 2010/09/24 14:31:15 christos Exp $ */
+/* $NetBSD: setenv.c,v 1.36 2010/09/25 18:11:40 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.35 2010/09/24 14:31:15 christos Exp $");
+__RCSID("$NetBSD: setenv.c,v 1.36 2010/09/25 18:11:40 tron Exp $");
#endif
#endif /* LIBC_SCCS and not lint */
@@ -44,7 +44,6 @@ __RCSID("$NetBSD: setenv.c,v 1.35 2010/09/24 14:31:15 christos Exp $");
#include <errno.h>
#include <stdlib.h>
#include <string.h>
-#include <bitstring.h>
#include "reentrant.h"
#include "local.h"
@@ -56,9 +55,6 @@ __weak_alias(setenv,_setenv)
extern rwlock_t __environ_lock;
#endif
-extern char **environ;
-extern bitstr_t *__environ_malloced;
-
/*
* setenv --
* Set the value of the environmental variable "name" to be
@@ -76,7 +72,9 @@ setenv(const char *name, const char *value, int rewrite)
_DIAGASSERT(name != NULL);
_DIAGASSERT(value != NULL);
- rwlock_wrlock(&__environ_lock);
+ if (rwlock_wrlock(&__environ_lock) != 0)
+ return -1;
+
/* find if already exists */
c = __findenv(name, &offset);
@@ -113,13 +111,15 @@ setenv(const char *name, const char *value, int rewrite)
/* name + `=' + value */
if ((c = malloc(size + l_value + 2)) == NULL)
goto bad;
- if (bit_test(__environ_malloced, offset))
- free(environ[offset]);
+
environ[offset] = c;
(void)memcpy(c, name, size);
c += size;
*c++ = '=';
- bit_set(__environ_malloced, offset);
+
+ free(__environ_malloced[offset]);
+ __environ_malloced[offset] = c;
+
copy:
(void)memcpy(c, value, l_value + 1);
good: