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: <https://gitlab.freedesktop.org/pulseaudio/pulseaudio/-/merge_requests/546>
This commit is contained in:
Patrick Gaskin 2021-05-30 15:24:56 -04:00 committed by PulseAudio Marge Bot
parent ac8e786026
commit a01cce726f

View file

@ -3005,7 +3005,16 @@ void pa_set_env(const char *key, const char *value) {
/* This is not thread-safe */ /* This is not thread-safe */
#ifdef OS_IS_WIN32 #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 #else
setenv(key, value, 1); setenv(key, value, 1);
#endif #endif
@ -3017,7 +3026,14 @@ void pa_unset_env(const char *key) {
/* This is not thread-safe */ /* This is not thread-safe */
#ifdef OS_IS_WIN32 #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 #else
unsetenv(key); unsetenv(key);
#endif #endif