From 90a4009c536fd5a4a6c04d62b226d8819f66a7ad Mon Sep 17 00:00:00 2001 From: Jonas 'Sortie' Termansen Date: Fri, 7 Sep 2012 12:54:02 +0200 Subject: [PATCH] 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..) --- libmaxsi/env.cpp | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/libmaxsi/env.cpp b/libmaxsi/env.cpp index 64444b9d..a95c69ef 100644 --- a/libmaxsi/env.cpp +++ b/libmaxsi/env.cpp @@ -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; }