Home | History | Annotate | Download | only in stdlib
History log of /src/lib/libc/stdlib/setenv.c
RevisionDateAuthorComments
 1.45  12-Aug-2021  andvar fix typos in "environment" word.
 1.44  20-Jan-2015  christos Fix non _REENTRANT build.
 1.43  14-Nov-2010  tron Improve and simplify implementation of *env(3) functions:
- Use RB tree to keep track of memory allocated via setenv(3) as
suggested by Enami Tsugutomo in private e-mail.
This simplifies the code a lot as we no longer need to keep the size
of "environ" in sync with an array of allocated environment variables.
It also makes it possible to free environment variables in unsetenv(3)
if something has changed the order of the "environ" array.
- Fix a bug in getenv(3) and getenv_r(3) which would return bogus
results e.g. for " getenv("A=B") " if an environment variable "A"
with value "B=C" exists.
- Clean up the internal functions:
- Don't expose the read/write lock for the environment to other parts
of "libc". Provide locking functions instead.
- Use "bool" to report success or failure.
- Use "ssize_t" or "size_t" instead of "int" for indexes.
- Provide internal functions with simpler interfaces e.g. don't
combine return values and reference arguments.
- Don't copy "environ" into an allocated block unless we really need
to grow it.

Code reviewed by Joerg Sonnenberger and Christos Zoulas, tested by
Joerg Sonnenberger and me. These changes also fix problems in
zsh 4.3.* and pam_ssh according to Joerg.
 1.42  03-Nov-2010  christos Handle the case where a program attempted to cleanup the environment by
setting *environ = NULL;
 1.41  16-Oct-2010  njoly Make setenv(3) follow the standard, by rejecting invalid strings. It
now fails with EINVAL errno when variable is NULL, empty or contains
an `=' character; or value is NULL.

Adjust the man page accordingly, and exercize them in the existing
environment testcase.
 1.40  02-Oct-2010  tron Restore binary compatibility with applications which use putenv(3)
on constant strings (e.g. postdrop(1)):
- Don't write to the environment string passed to putenv(3).
- Don't overwrite the value of an existing environment string
unless the memory was actually allocated by setenv(3).
 1.39  01-Oct-2010  christos make putenv POSIX compliant.
 1.38  30-Sep-2010  tron 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.
 1.37  25-Sep-2010  tron Remember the correct pointer which we will free later.
 1.36  25-Sep-2010  tron 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.
 1.35  24-Sep-2010  christos Fix memory leaks; thanks Enami!
 1.34  23-Sep-2010  christos Use a bit array to keep track of malloced environment entries so we can
free them.
 1.33  23-Sep-2010  christos PR/43899: Nicolas Joly: setenv(3)/unsetenv(3) memory leak.
Partial fix: Don't allocate a new string if the length is equal to the
old length, because presumably the old string was also nul terminated
so it has the extra byte needed.
The real fix is to keep an adjunct array of bits, one for each environment
variable and keep track if the entry was allocated or not so that we can
free it in unsetenv.
 1.32  02-Dec-2009  enami Retain old variables when failed to allocate memory.
 1.31  01-Nov-2008  christos branches: 1.31.2;
use the right variable in memcpy.
 1.30  31-Oct-2008  christos make __findenv return the offset for the new entry to be inserted when
the string is not found, to avoid an additional scan of the environ array
in setenv.
 1.29  17-Feb-2005  christos remove extraneous `*' that caused a core-dump.
 1.28  17-Feb-2005  christos do some manual CSE.
 1.27  17-Feb-2005  tron Fix lint warning caused by last change.
 1.26  17-Feb-2005  enami Jump to just before the copy instead of after it. Saves 8 byte on i386.
 1.25  17-Feb-2005  enami Simplify, KNF and plug leaks. Compile test only.
 1.24  17-Feb-2005  christos Simplify and KNF. Instead of keeping track if the pointer was allocated,
keep a copy of the pointer itself and only re-use it if the current
environment is equal to the saved pointer. Idea from OpenBSD. Avoids
problem of re-allocing a non-allocated pointer if a program resets the
environment after it has been grown.
 1.23  07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22280, verified by myself.
 1.22  07-Apr-2003  kleink 1003.1-2001 has introduced unsetenv() which differs from the current
implementation in not permitting a "name=value" argument.
* Add a conforming __unsetenv13() and do function renaming for
unsetenv(); preserve old symbol with old behavior.
* Make visible setenv() and unsetenv() for 1003.1-2001 feature selection
macros; resolves PR standards/20479.
 1.21  18-Jan-2003  thorpej Merge the nathanw_sa branch.
 1.20  11-Nov-2002  thorpej Fix signed/unsigned comparison warnings.
 1.19  20-Dec-2000  christos branches: 1.19.2;
avoid nested extern declaration
 1.18  22-Jan-2000  mycroft Delint.
Remove trailing ; from uses of __weak_alias(). The macro inserts this if
needed.
 1.17  20-Sep-1999  lukem back out the #ifdef _DIAGNOSTIC argument checks; too many people complained.
_DIAGASSERT() is still retained.
 1.16  16-Sep-1999  lukem * use _DIAGASSERT() to check pointer arguments against NULL and file
descriptors against -1 (as appropriate).
* add actual checks which to detect stuff that would trigger_DIAGASSERT(),
and attempt to return a sane error condition.
* knf some code
* remove some `register' decls.

the first two items result in the addition of code similar to the
following in various functions:

_DIAGASSERT(path != NULL)
#ifdef _DIAGNOSTIC
if (path == NULL) {
errno = EFAULT;
return (-1);
}
#endif
 1.15  15-Nov-1998  christos delint
 1.14  11-Sep-1998  kleink Add a multiple-reader/single-writer lock to protect environ.
 1.13  10-Aug-1998  perry bzero->memset, bcopy->memcpy, bcmp->memcmp
 1.12  03-Feb-1998  perry remove obsolete register declarations
 1.11  30-Jan-1998  perry update to lite-2
 1.10  21-Jul-1997  jtc If port provides __weak_alias(), provide an Standard C and POSIX pure
identifier namespace by renaming non standard functions and variables
such that they have a leading underscore. The library will use those
names internally. Weak aliases are used to provide the original names
to the API.

This is only the first part of this change. It is most of the functions
which are implemented in C for all NetBSD ports. Subsequent changes are
to add the same support to the remaining C files, to assembly files, and
to the automagically generated assembly source used for system calls.
When all of the above is done, ports with weak alias support should add
a definition for __weak_alias to <sys/cdefs.h>.
 1.9  13-Jul-1997  christos Add local.h for local prototypes.
Fix namespace issues for strtoq and strtouq
Fix gcc warnings.
Fix RCSID's
 1.8  28-Dec-1995  thorpej branches: 1.8.2;
New-style RCS ids.
 1.7  14-Jun-1995  jtc add explicit return type
 1.6  28-Feb-1995  jtc remove unneeded "#include <stddef.h>"
 1.5  15-Oct-1993  jtc branches: 1.5.4;
Rename _findenv() to __findenv(), as _findenv is in the user's namespace.
 1.4  26-Aug-1993  jtc Declare rcsid strings so they are stored in text segment.
 1.3  01-Aug-1993  mycroft Add RCS identifiers.
 1.2  18-Apr-1993  mycroft Eliminate GCC 2 warning due to prototype not being in scope.
 1.1  21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2  30-Jan-1998  perry import lite-2
 1.1.1.1  21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.5.4.1  02-May-1995  jtc #include "namespace.h"
 1.8.2.1  18-Sep-1996  jtc snapshot namespace cleanup: stdlib
 1.19.2.2  10-Dec-2002  thorpej Sync with HEAD.
 1.19.2.1  08-Aug-2001  nathanw _REENT -> _REENTRANT
 1.31.2.2  01-Nov-2008  christos use the right variable in memcpy.
 1.31.2.1  01-Nov-2008  christos file setenv.c was added on branch christos-time_t on 2008-11-01 00:54:48 +0000

RSS XML Feed