summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorscf <scf@FreeBSD.org>2008-08-02 02:34:35 +0000
committerscf <scf@FreeBSD.org>2008-08-02 02:34:35 +0000
commite0b5c971c2aa4250126a8dab9c31bf0c6495e746 (patch)
tree56a644b40c074e9be4e1f55022d96abde55efb77 /lib
parent8b381816e429dcf85d6f0731aa32111576ac8cc7 (diff)
downloadFreeBSD-src-e0b5c971c2aa4250126a8dab9c31bf0c6495e746.zip
FreeBSD-src-e0b5c971c2aa4250126a8dab9c31bf0c6495e746.tar.gz
Detect if the application has cleared the environ variable by setting
the first value (environ[0]) to NULL. This is in addition to the current detection of environ being replaced, which includes being set to NULL. Without this fix, the environment is not truly wiped, but appears to be by getenv() until an *env() call is made to alter the enviroment. This change is necessary to support those applications that use this method for clearing environ such as Dovecot and Postfix. Applications such as Sendmail and the base system's env replace environ (already detected). While neither of these methods are defined by SUSv3, it is best to support them due to historic reasons and in lieu of a clean, defined method. Add extra units tests for clearing environ using four different methods: 1. Set environ to NULL pointer. 2. Set environ[0] to NULL pointer. 3. Set environ to calloc()'d NULL-terminated array. 4. Set environ to static NULL-terminated array. Noticed by: Timo Sirainen MFC after: 3 days
Diffstat (limited to 'lib')
-rw-r--r--lib/libc/stdlib/getenv.c19
1 files changed, 12 insertions, 7 deletions
diff --git a/lib/libc/stdlib/getenv.c b/lib/libc/stdlib/getenv.c
index 74595b2..0f84082 100644
--- a/lib/libc/stdlib/getenv.c
+++ b/lib/libc/stdlib/getenv.c
@@ -1,5 +1,5 @@
/*-
- * Copyright (c) 2007 Sean C. Farley <scf@FreeBSD.org>
+ * Copyright (c) 2007-2008 Sean C. Farley <scf@FreeBSD.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@@ -431,11 +431,13 @@ getenv(const char *name)
/*
* Find environment variable via environ if no changes have been made
- * via a *env() call or environ has been replaced by a running program,
- * otherwise, use the rebuilt environment.
+ * via a *env() call or environ has been replaced or cleared by a
+ * running program, otherwise, use the rebuilt environment.
*/
if (envVars == NULL || environ != intEnviron)
return (__findenv_environ(name, nameLen));
+ else if (environ[0] == NULL)
+ return (NULL);
else {
envNdx = envVarsTotal - 1;
return (__findenv(name, nameLen, &envNdx, true));
@@ -525,8 +527,8 @@ __setenv(const char *name, size_t nameLen, const char *value, int overwrite)
/*
* If the program attempts to replace the array of environment variables
- * (environ) environ, then deactivate all variables and merge in the new list
- * from environ.
+ * (environ) environ or sets the first varible to NULL, then deactivate all
+ * variables and merge in the new list from environ.
*/
static int
__merge_environ(void)
@@ -534,8 +536,11 @@ __merge_environ(void)
char **env;
char *equals;
- /* environ has been replaced. clean up everything. */
- if (envVarsTotal > 0 && environ != intEnviron) {
+ /*
+ * Internally-built environ has been replaced or cleared. clean up
+ * everything.
+ */
+ if (envVarsTotal > 0 && (environ != intEnviron || environ[0] == NULL)) {
/* Deactivate all environment variables. */
if (envActive > 0) {
origEnviron = NULL;
OpenPOWER on IntegriCloud