win32: Fix environment variables set with pa_{unset,set}_env not taking effect
authorPatrick Gaskin <patrick@pgaskin.net>
Sun, 30 May 2021 19:24:56 +0000 (15:24 -0400)
committerPulseAudio Marge Bot <pulseaudio-maintainers@lists.freedesktop.org>
Wed, 16 Jun 2021 09:05:58 +0000 (09:05 +0000)
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>

src/pulsecore/core-util.c

index 24736e8..89e37d5 100644 (file)
@@ -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