Make putenv(3) copy the input string.

This violates POSIX but POSIX is stupid in this respect. Of course, this
will ever so subtly break some applications. Not sure how this stupid design
can be fixed without breaking backwards compatibility. Perhaps remove the
putenv(3) function and replace it with a better replacement? (Or perhaps you
should just use setenv(3) anyways..)
This commit is contained in:
Jonas 'Sortie' Termansen 2012-09-07 12:54:02 +02:00
parent 550f9db140
commit 90a4009c53
1 changed files with 10 additions and 1 deletions

View File

@ -164,7 +164,16 @@ extern "C" int setenv(const char* name, const char* value, int overwrite)
extern "C" int putenv(char* str)
{
if ( !strchr(str, '=') ) { errno = EINVAL; return -1; }
if ( !doputenv(str, NULL, true) ) { return -1; }
// TODO: HACK: This voilates POSIX as it mandates that callers must be able
// to modify the environment by modifying the string. However, this is bad
// design! It also means we got a memory leak since we can't safely free
// strings from the environment when overriding them. Therefore we create
// a copy of the strings here and use the copy instead. This is a problem
// for some applications that will subtly break.
char* strcopy = strdup(str);
if ( !strcopy )
return -1;
if ( !doputenv(strcopy, strcopy, true) ) { return -1; }
return 0;
}