Handle the case where a program attempted to cleanup the environment by

setting *environ = NULL;
This commit is contained in:
christos 2010-11-03 15:01:07 +00:00
parent 94dfcbab95
commit 3954ad831d
4 changed files with 41 additions and 11 deletions

View File

@ -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 */
@ -153,6 +153,28 @@ __allocenv(int offset)
return 0;
}
/*
* 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.

View File

@ -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;

View File

@ -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 @@ putenv(char *str)
}
environ[offset] = str;
if (p == NULL)
__scrubenv(offset);
rwlock_unlock(&__environ_lock);
return 0;
bad:

View File

@ -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 @@ __weak_alias(setenv,_setenv)
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 @@ setenv(const char *name, const char *value, int rewrite)
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 @@ setenv(const char *name, const char *value, int rewrite)
* existing value.
*/
if (environ[offset] == __environ_malloced[offset] &&
strlen(c) >= l_value) {
strlen(f) >= l_value) {
c = f;
goto copy;
}
}
@ -110,6 +111,10 @@ setenv(const char *name, const char *value, int rewrite)
environ[offset] = c;
__environ_malloced[offset] = c;
if (f == NULL)
__scrubenv(offset);
(void)memcpy(c, name, size);
c += size;
*c++ = '=';