[PATCH 1/4] msvcrt: Do not put cmd.exe special environment variables into the environ

Jason Edmeades us at edmeades.me.uk
Tue Sep 25 02:39:59 CDT 2018


All the special environment variables from the command shell which track directory use are stripped out from
the C runtime environ/wenviron.

Wine-Bug: https://bugs.winehq.org/show_bug.cgi?id=45320
---

Note this cannot be tested by the test suites because it only gets triggered for C applications launched from
the command shell - if you launch them from explorer on windows for example, they are not set either. I've
attached a comprehensive set of tests onto the bug as an attachment, which tests the interaction of these
special variables, the current directory and the C runtime vs the command shell.

The way I have chosen to implement this leaves the special variables content in the allocated memory, allowing
the memcpy of the environment strings, but does not put a pointer to that data in the char** array of strings. An
alternative implementation would copy in string by string and skip them, but I have chosen to leave that unless
something is broken because of it.

Wine-Bug: https://bugs.winehq.org/show_bug.cgi?id=45320
Signed-off-by: Jason Edmeades <us at edmeades.me.uk>
---
 dlls/msvcrt/data.c | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)

diff --git a/dlls/msvcrt/data.c b/dlls/msvcrt/data.c
index 06e16a865e..f08d9b1683 100644
--- a/dlls/msvcrt/data.c
+++ b/dlls/msvcrt/data.c
@@ -73,7 +73,8 @@ char ** msvcrt_SnapshotOfEnvironmentA(char **blk)
 
   for (ptr = environ_strings; *ptr; ptr += strlen(ptr) + 1)
   {
-    count++;
+    /* Don't count environment variables starting with '=' which are command shell specific */
+    if (*ptr != '=') count++;
     len += strlen(ptr) + 1;
   }
   if (blk)
@@ -88,7 +89,8 @@ char ** msvcrt_SnapshotOfEnvironmentA(char **blk)
 	  memcpy(&blk[count],environ_strings,len);
 	  for (ptr = (char*) &blk[count]; *ptr; ptr += strlen(ptr) + 1)
 	    {
-	      blk[i++] = ptr;
+	      /* Skip special environment strings set by the command shell */
+	      if (*ptr != '=') blk[i++] = ptr;
 	    }
 	}
       blk[i] = NULL;
@@ -105,7 +107,8 @@ MSVCRT_wchar_t ** msvcrt_SnapshotOfEnvironmentW(MSVCRT_wchar_t **wblk)
 
   for (wptr = wenviron_strings; *wptr; wptr += strlenW(wptr) + 1)
   {
-    count++;
+    /* Don't count environment variables starting with '=' which are command shell specific */
+    if (*wptr != '=') count++;
     len += strlenW(wptr) + 1;
   }
   if (wblk)
@@ -119,7 +122,8 @@ MSVCRT_wchar_t ** msvcrt_SnapshotOfEnvironmentW(MSVCRT_wchar_t **wblk)
 	  memcpy(&wblk[count],wenviron_strings,len * sizeof(MSVCRT_wchar_t));
 	  for (wptr = (MSVCRT_wchar_t*)&wblk[count]; *wptr; wptr += strlenW(wptr) + 1)
 	    {
-	      wblk[i++] = wptr;
+	      /* Skip special environment strings set by the command shell */
+	      if (*wptr != '=') wblk[i++] = wptr;
 	    }
 	}
       wblk[i] = NULL;
-- 
2.17.1




More information about the wine-devel mailing list