From a01cce726f175f43ee4b2bb733ad3d54d0de8415 Mon Sep 17 00:00:00 2001 From: Patrick Gaskin Date: Sun, 30 May 2021 15:24:56 -0400 Subject: [PATCH] win32: Fix environment variables set with pa_{unset,set}_env not taking effect SetEnvironmentVariable is not visible to getenv. See https://github.com/curl/curl/issues/4774. See https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/getenv-wgetenv. Part-of: --- src/pulsecore/core-util.c | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/src/pulsecore/core-util.c b/src/pulsecore/core-util.c index 24736e8..89e37d5 100644 --- a/src/pulsecore/core-util.c +++ b/src/pulsecore/core-util.c @@ -3005,7 +3005,16 @@ void pa_set_env(const char *key, const char *value) { /* This is not thread-safe */ #ifdef OS_IS_WIN32 - SetEnvironmentVariable(key, value); + int kl = strlen(key); + int vl = strlen(value); + char *tmp = pa_xmalloc(kl+vl+2); + memcpy(tmp, key, kl); + memcpy(tmp+kl+1, value, vl); + tmp[kl] = '='; + tmp[kl+1+vl] = '\0'; + putenv(tmp); + /* Even though it should be safe to free it on Windows, we don't want to + * rely on undocumented behaviour. */ #else setenv(key, value, 1); #endif @@ -3017,7 +3026,14 @@ void pa_unset_env(const char *key) { /* This is not thread-safe */ #ifdef OS_IS_WIN32 - SetEnvironmentVariable(key, NULL); + int kl = strlen(key); + char *tmp = pa_xmalloc(kl+2); + memcpy(tmp, key, kl); + tmp[kl] = '='; + tmp[kl+1] = '\0'; + putenv(tmp); + /* Even though it should be safe to free it on Windows, we don't want to + * rely on undocumented behaviour. */ #else unsetenv(key); #endif -- 2.7.4