Hi all,

When making calls to apr_get_env on win32 platforms, if the given environment
variable exists, but has a zero-length value, the apr_get_env call should return
APR_SUCCESS with either an empty-string value or NULL.  APR_ENOENT should only
be returned if the requested environment variable does not exist in the
environment. Enclosed is a patch which fixes this behavior. This is recorded at http://issues.apache.org/bugzilla/show_bug.cgi?id=40764 including the patch to fix + a patch for a test case for the test suite.

 Issac


Index: misc/win32/env.c
===================================================================
--- misc/win32/env.c    (revision 464476)
+++ misc/win32/env.c    (working copy)
@@ -22,6 +22,7 @@
 #include "apr_env.h"
 #include "apr_errno.h"
 #include "apr_pools.h"
+#include "apr_strings.h"
 
 
 #if APR_HAS_UNICODE_FS
@@ -61,11 +62,18 @@
         if (status)
             return status;
 
+        SetLastError(0);
         size = GetEnvironmentVariableW(wenvvar, &dummy, 0);
-        if (size == 0)
+        if (GetLastError() == ERROR_ENVVAR_NOT_FOUND)
             /* The environment variable doesn't exist. */
             return APR_ENOENT;
 
+        if (size == 0) {
+            /* The environment value exists, but is zero-length. */
+            *value = apr_pstrdup(pool, "");
+            return APR_SUCCESS;
+        }
+
         wvalue = apr_palloc(pool, size * sizeof(*wvalue));
         size = GetEnvironmentVariableW(wenvvar, wvalue, size);
         if (size == 0)
@@ -85,11 +93,18 @@
     {
         char dummy;
 
+        SetLastError(0);
         size = GetEnvironmentVariableA(envvar, &dummy, 0);
-        if (size == 0)
+        if (GetLastError() == ERROR_ENVVAR_NOT_FOUND)
             /* The environment variable doesn't exist. */
             return APR_ENOENT;
 
+        if (size == 0) {
+            /* The environment value exists, but is zero-length. */
+            *value = apr_pstrdup(pool, "");
+            return APR_SUCCESS;
+        }
+
         val = apr_palloc(pool, size);
         size = GetEnvironmentVariableA(envvar, val, size);
         if (size == 0)

Reply via email to