[PATCH v2] kernel32: make GetEnvironmentStringsW returns a copy of the environment

Jon Doron arilou at gmail.com
Sat Mar 16 23:12:15 CDT 2019


There are certain applications which try to traverse the environement
being returned, but this is problematic since they cannot acquire the
PEB Lock (i.e cl.exe on Visual Studio 14.15) .

To resolve the issue provide a copy of the current environment same as
in GetEnvironmentStringsA .

Signed-off-by: Jon Doron <arilou at gmail.com>
---
 dlls/kernel32/environ.c | 27 +++++++++++++++++++++++++--
 1 file changed, 25 insertions(+), 2 deletions(-)

diff --git a/dlls/kernel32/environ.c b/dlls/kernel32/environ.c
index 99bf706e95..5a5f50340e 100644
--- a/dlls/kernel32/environ.c
+++ b/dlls/kernel32/environ.c
@@ -139,7 +139,30 @@ LPSTR WINAPI GetEnvironmentStringsA(void)
  */
 LPWSTR WINAPI GetEnvironmentStringsW(void)
 {
-    return NtCurrentTeb()->Peb->ProcessParameters->Environment;
+    LPWSTR ret, ptrW;
+    unsigned len;
+
+    RtlAcquirePebLock();
+
+    ptrW = NtCurrentTeb()->Peb->ProcessParameters->Environment;
+    if (!ptrW || !*ptrW)
+    {
+        RtlReleasePebLock();
+        return HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WCHAR) * 2);
+    }
+
+    while (*ptrW)
+        ptrW += strlenW(ptrW) + 1;
+
+    len = (ULONG_PTR)ptrW - (ULONG_PTR)NtCurrentTeb()->Peb->ProcessParameters->Environment;
+    ret = HeapAlloc(GetProcessHeap(), 0, len + sizeof(WCHAR));
+    if (ret) {
+        memcpy(ret, NtCurrentTeb()->Peb->ProcessParameters->Environment, len);
+        ret[len / 2] = 0;
+    }
+
+    RtlReleasePebLock();
+    return ret;
 }
 
 
@@ -157,7 +180,7 @@ BOOL WINAPI FreeEnvironmentStringsA( LPSTR ptr )
  */
 BOOL WINAPI FreeEnvironmentStringsW( LPWSTR ptr )
 {
-    return TRUE;
+    return HeapFree( GetProcessHeap(), 0, ptr );
 }
 
 
-- 
2.19.2




More information about the wine-devel mailing list