From 90418d34c5517691fa78d90e632fa5f9f6b35c03 Mon Sep 17 00:00:00 2001 From: Ron Yorston Date: Fri, 21 Aug 2020 09:42:00 +0100 Subject: win32: allow putenv() to set empty values WIN32 _putenv() can't set an environment variable with an empty value because 'NAME=' is treated as a request to delete the variable. Allow mingw_putenv() to set an empty value by first setting a fake value and then truncating it. This problem has always been present in mingw_putenv() but it became particularly pressing when ash started treating all applets as NOEXEC. The environment of NOEXEC applets is created by clearing the current environment and using putenv() to set a new one from the shell variables. Empty variable were not being set. See GitHub issue #197. --- win32/env.c | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/win32/env.c b/win32/env.c index a7ab7851a..4d4e9c8fd 100644 --- a/win32/env.c +++ b/win32/env.c @@ -78,16 +78,30 @@ int clearenv(void) int mingw_putenv(const char *env) { - char *s; + char *s, **envp; + int ret = 0; if ( (s=strchr(env, '=')) == NULL ) { return unsetenv(env); } - if ( s[1] != '\0' ) { + if (s[1] != '\0') { + /* setting non-empty value is fine */ return _putenv(env); } + else { + /* set empty value by setting a non-empty one then truncating */ + char *envstr = xasprintf("%s0", env); + ret = _putenv(envstr); + + for (envp = environ; *envp; ++envp) { + if (strcmp(*envp, envstr) == 0) { + (*envp)[s - env + 1] = '\0'; + break; + } + } + free(envstr); + } - /* can't set empty value */ - return 0; + return ret; } -- cgit v1.2.3-55-g6feb