52592b
diff --git a/server/util_script.c b/server/util_script.c
52592b
index 4121ae0..b7f8674 100644
52592b
--- a/server/util_script.c
52592b
+++ b/server/util_script.c
52592b
@@ -92,9 +92,21 @@ static void add_unless_null(apr_table_t *table, const char *name, const char *va
52592b
     }
52592b
 }
52592b
 
52592b
-static void env2env(apr_table_t *table, const char *name)
52592b
+/* Sets variable @name in table @dest from r->subprocess_env if
52592b
+ * available, else from the environment, else from @fallback if
52592b
+ * non-NULL. */
52592b
+static void env2env(apr_table_t *dest, request_rec *r,
52592b
+                    const char *name, const char *fallback)
52592b
 {
52592b
-    add_unless_null(table, name, getenv(name));
52592b
+    const char *val;
52592b
+
52592b
+    val = apr_table_get(r->subprocess_env, name);
52592b
+    if (!val)
52592b
+        val = apr_pstrdup(r->pool, getenv(name));
52592b
+    if (!val)
52592b
+        val = apr_pstrdup(r->pool, fallback);
52592b
+    if (val)
52592b
+        apr_table_addn(dest, name, val);
52592b
 }
52592b
 
52592b
 AP_DECLARE(char **) ap_create_environment(apr_pool_t *p, apr_table_t *t)
52592b
@@ -211,37 +223,29 @@ AP_DECLARE(void) ap_add_common_vars(request_rec *r)
52592b
             add_unless_null(e, http2env(r, hdrs[i].key), hdrs[i].val);
52592b
     }
52592b
 
52592b
-    env_temp = apr_table_get(r->subprocess_env, "PATH");
52592b
-    if (env_temp == NULL) {
52592b
-        env_temp = getenv("PATH");
52592b
-    }
52592b
-    if (env_temp == NULL) {
52592b
-        env_temp = DEFAULT_PATH;
52592b
-    }
52592b
-    apr_table_addn(e, "PATH", apr_pstrdup(r->pool, env_temp));
52592b
-
52592b
+    env2env(e, r, "PATH", DEFAULT_PATH);
52592b
 #if defined(WIN32)
52592b
-    env2env(e, "SystemRoot");
52592b
-    env2env(e, "COMSPEC");
52592b
-    env2env(e, "PATHEXT");
52592b
-    env2env(e, "WINDIR");
52592b
+    env2env(e, r, "SystemRoot", NULL);
52592b
+    env2env(e, r, "COMSPEC", NULL);
52592b
+    env2env(e, r, "PATHEXT", NULL);
52592b
+    env2env(e, r, "WINDIR", NULL);
52592b
 #elif defined(OS2)
52592b
-    env2env(e, "COMSPEC");
52592b
-    env2env(e, "ETC");
52592b
-    env2env(e, "DPATH");
52592b
-    env2env(e, "PERLLIB_PREFIX");
52592b
+    env2env(e, r, "COMSPEC", NULL);
52592b
+    env2env(e, r, "ETC", NULL);
52592b
+    env2env(e, r, "DPATH", NULL);
52592b
+    env2env(e, r, "PERLLIB_PREFIX", NULL);
52592b
 #elif defined(BEOS)
52592b
-    env2env(e, "LIBRARY_PATH");
52592b
+    env2env(e, r, "LIBRARY_PATH", NULL);
52592b
 #elif defined(DARWIN)
52592b
-    env2env(e, "DYLD_LIBRARY_PATH");
52592b
+    env2env(e, r, "DYLD_LIBRARY_PATH", NULL);
52592b
 #elif defined(_AIX)
52592b
-    env2env(e, "LIBPATH");
52592b
+    env2env(e, r, "LIBPATH", NULL);
52592b
 #elif defined(__HPUX__)
52592b
     /* HPUX PARISC 2.0W knows both, otherwise redundancy is harmless */
52592b
-    env2env(e, "SHLIB_PATH");
52592b
-    env2env(e, "LD_LIBRARY_PATH");
52592b
+    env2env(e, r, "SHLIB_PATH", NULL);
52592b
+    env2env(e, r, "LD_LIBRARY_PATH", NULL);
52592b
 #else /* Some Unix */
52592b
-    env2env(e, "LD_LIBRARY_PATH");
52592b
+    env2env(e, r, "LD_LIBRARY_PATH", NULL);
52592b
 #endif
52592b
 
52592b
     apr_table_addn(e, "SERVER_SIGNATURE", ap_psignature("", r));