summaryrefslogtreecommitdiff
path: root/lib/libc/stdlib/getenv.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/getenv.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/getenv.c')
-rw-r--r--lib/libc/stdlib/getenv.c32
1 files changed, 15 insertions, 17 deletions
diff --git a/lib/libc/stdlib/getenv.c b/lib/libc/stdlib/getenv.c
index f4143339f13..ef78366979a 100644
--- a/lib/libc/stdlib/getenv.c
+++ b/lib/libc/stdlib/getenv.c
@@ -1,4 +1,4 @@
-/* $NetBSD: getenv.c,v 1.20 2010/09/23 17:30:49 christos Exp $ */
+/* $NetBSD: getenv.c,v 1.21 2010/09/25 18:11:40 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.20 2010/09/23 17:30:49 christos Exp $");
+__RCSID("$NetBSD: getenv.c,v 1.21 2010/09/25 18:11:40 tron Exp $");
#endif
#endif /* LIBC_SCCS and not lint */
@@ -43,15 +43,14 @@ __RCSID("$NetBSD: getenv.c,v 1.20 2010/09/23 17:30:49 christos Exp $");
#include <errno.h>
#include <stdlib.h>
#include <string.h>
-#include <bitstring.h>
#include "reentrant.h"
#include "local.h"
#ifdef _REENTRANT
rwlock_t __environ_lock = RWLOCK_INITIALIZER;
#endif
-bitstr_t *__environ_malloced;
-static size_t environ_bitlen;
+char **__environ_malloced;
+static size_t environ_malloced_len;
__weak_alias(getenv_r, _getenv_r)
@@ -104,28 +103,27 @@ out:
int
__allocenv(int offset)
{
- bitstr_t *s;
+ char **p;
size_t nl;
if (offset == -1) {
char **ptr;
- for (ptr = environ, offset = 0; *ptr; ptr++)
+ for (ptr = environ, offset = 0; *ptr != NULL; ptr++)
offset++;
}
- nl = bitstr_size(offset + 2);
- if (__environ_malloced == NULL) {
- s = malloc(nl);
- } else if (environ_bitlen < nl)
- s = realloc(__environ_malloced, nl);
- else
+
+ if ((size_t)offset < environ_malloced_len)
return 0;
- if (s == NULL)
+ nl = offset + 2;
+ p = realloc(__environ_malloced, nl * sizeof(char *));
+ if (p == NULL)
return -1;
- (void)memset(&s[environ_bitlen], 0, nl - environ_bitlen);
- environ_bitlen = nl;
- __environ_malloced = s;
+ (void)memset(&p[environ_malloced_len], 0,
+ (nl - environ_malloced_len) * sizeof(char *));
+ environ_malloced_len = nl;
+ __environ_malloced = p;
return 0;
}