setenv, unsetenv: work around various bugs
authorEric Blake <ebb9@byu.net>
Sun, 15 Nov 2009 05:13:10 +0000 (22:13 -0700)
committerEric Blake <ebb9@byu.net>
Tue, 17 Nov 2009 03:39:14 +0000 (20:39 -0700)
commit8dffeb6f35aff7e201bed55c43ea83b62139803e
tree81c1dfa69c1acdb32eab15a975a93e599dcb961e
parent1c32714559eaa0e6747ae153eff36dc1f97585f9
setenv, unsetenv: work around various bugs

POSIX requires setenv(NULL,"",0), setenv("a=b","",0),
unsetenv(NULL), and unsetenv("a=b") to fail with EINVAL, but
many BSD implementations lack validation.  The gnulib
replacement for void unsetenv did not do validation, and the
replacement for setenv was out of sync with glibc.  Also, some
BSD implementations of setenv("a","==",1) eat the leading '='.

See also some recent Austin Group interpretations on environ:
http://austingroupbugs.net/view.php?id=167
http://austingroupbugs.net/view.php?id=185

* lib/setenv.c (setenv) [!HAVE_SETENV]: Resync from glibc.
(setenv) [HAVE_SETENV]: Work around bugs.
* lib/unsetenv.c (unsetenv) [HAVE_UNSETENV]: Work around bugs.
* m4/setenv.m4 (gl_FUNC_SETENV_SEPARATE, gl_FUNC_UNSETENV): Check
for bugs.
(gl_FUNC_SETENV): Write in terms of gl_FUNC_SETENV_SEPARATE.
* m4/environ.m4 (gl_ENVIRON): Avoid expand-before-require.
* m4/stdlib_h.m4 (gl_STDLIB_H_DEFAULTS): Update defaults.
* modules/stdlib (Makefile.am): Update substitutions.
* lib/stdlib.in.h (setenv, unsetenv): Update prototypes.
* doc/posix-functions/setenv.texi (setenv): Document the bugs.
* doc/posix-functions/unsetenv.texi (unsetenv): Likewise.
* modules/setenv-tests: New test.
* modules/unsetenv-tests: Likewise.
* tests/test-setenv.c: New file.
* tests/test-unsetenv.c: Likewise.

Signed-off-by: Eric Blake <ebb9@byu.net>
14 files changed:
ChangeLog
doc/posix-functions/setenv.texi
doc/posix-functions/unsetenv.texi
lib/setenv.c
lib/stdlib.in.h
lib/unsetenv.c
m4/environ.m4
m4/setenv.m4
m4/stdlib_h.m4
modules/setenv-tests [new file with mode: 0644]
modules/stdlib
modules/unsetenv-tests [new file with mode: 0644]
tests/test-setenv.c [new file with mode: 0644]
tests/test-unsetenv.c [new file with mode: 0644]