[PATCH v2 3/3] kernelbase: Fix GetEnvironmentVariableW return for empty variables.

Vladimir Panteleev git at vladimir.panteleev.md
Tue Jan 21 05:37:38 CST 2020


Do the same thing as Vista and up.

Wine-Bug: https://bugs.winehq.org/show_bug.cgi?id=48471
Signed-off-by: Vladimir Panteleev <git at vladimir.panteleev.md>
---
 dlls/kernel32/tests/environ.c | 10 ++++++++++
 dlls/kernelbase/process.c     |  3 ++-
 2 files changed, 12 insertions(+), 1 deletion(-)

diff --git a/dlls/kernel32/tests/environ.c b/dlls/kernel32/tests/environ.c
index 7eaafc30fd..ca01167d89 100644
--- a/dlls/kernel32/tests/environ.c
+++ b/dlls/kernel32/tests/environ.c
@@ -272,6 +272,16 @@ static void test_GetSetEnvironmentVariableW(void)
        ret_size, GetLastError());
     ok(lstrcmpW(buf, empty_strW) == 0, "should copy an empty string\n");
 
+    SetLastError(0xdeadbeef);
+    ret_size = GetEnvironmentVariableW(name, NULL, 0);
+    ok(ret_size == 1 ||
+       broken(ret_size == 0), /* XP */
+       "should return 1 for empty string but ret_size=%d GetLastError=%d\n",
+       ret_size, GetLastError());
+    ok(GetLastError() == 0xdeadbeef ||
+       broken(GetLastError() == ERROR_MORE_DATA), /* XP */
+       "should not fail with zero size but GetLastError=%d\n", GetLastError());
+
     /* Test the limits */
     SetLastError(0xdeadbeef);
     ret_size = GetEnvironmentVariableW(NULL, NULL, 0);
diff --git a/dlls/kernelbase/process.c b/dlls/kernelbase/process.c
index 788f03f220..612b0b2a2f 100644
--- a/dlls/kernelbase/process.c
+++ b/dlls/kernelbase/process.c
@@ -1276,7 +1276,8 @@ DWORD WINAPI DECLSPEC_HOTPATCH GetEnvironmentVariableW( LPCWSTR name, LPWSTR val
     len = us_value.Length / sizeof(WCHAR);
     if (status == STATUS_BUFFER_TOO_SMALL) return len + 1;
     if (!set_ntstatus( status )) return 0;
-    if (size) val[len] = 0;
+    if (!size) return len + 1;
+    val[len] = 0;
     return len;
 }
 
-- 
2.25.0




More information about the wine-devel mailing list