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

Jon Doron arilou at gmail.com
Sat Mar 16 13:57:16 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 | 19 +++++++++++++++++--
 1 file changed, 17 insertions(+), 2 deletions(-)

diff --git a/dlls/kernel32/environ.c b/dlls/kernel32/environ.c
index 99bf706e95..c8f32aa0fb 100644
--- a/dlls/kernel32/environ.c
+++ b/dlls/kernel32/environ.c
@@ -139,7 +139,22 @@ LPSTR WINAPI GetEnvironmentStringsA(void)
  */
 LPWSTR WINAPI GetEnvironmentStringsW(void)
 {
-    return NtCurrentTeb()->Peb->ProcessParameters->Environment;
+    LPWSTR ret, ptrW;
+    unsigned len;
+
+    RtlAcquirePebLock();
+
+    ptrW = NtCurrentTeb()->Peb->ProcessParameters->Environment;
+    while (*ptrW)
+        ptrW += strlenW(ptrW) + 1;
+
+    len = (ULONG_PTR)ptrW - (ULONG_PTR)NtCurrentTeb()->Peb->ProcessParameters->Environment;
+    ret = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, len + (sizeof(WCHAR) * 2));
+    if (ret)
+        memcpy(ret, NtCurrentTeb()->Peb->ProcessParameters->Environment, len);
+
+    RtlReleasePebLock();
+    return ret;
 }
 
 
@@ -157,7 +172,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