From b35d86aa132b328871bef484f78888dda6e7c71c Mon Sep 17 00:00:00 2001
From: Uwe Bonnes <bon@elektron.ikp.physik.tu-darmstadt.de>
Date: Fri, 23 Nov 2001 18:30:19 +0000
Subject: [PATCH] MSVCRT_getenv: compare for the length of the key and return
 NULL in case of failure.

---
 dlls/msvcrt/environ.c | 45 +++++++++++++++++++++++--------------------
 1 file changed, 24 insertions(+), 21 deletions(-)

diff --git a/dlls/msvcrt/environ.c b/dlls/msvcrt/environ.c
index 8c513f5679f..58cf5fbcf48 100644
--- a/dlls/msvcrt/environ.c
+++ b/dlls/msvcrt/environ.c
@@ -21,22 +21,23 @@ char *MSVCRT_getenv(const char *name)
 {
   char *environ = GetEnvironmentStringsA();
   char *pp,*pos = NULL;
-  unsigned int length;
+  unsigned int length=strlen(name);
 
   for (pp = environ; (*pp); pp = pp + strlen(pp) +1)
   {
-    pos =strchr(pp,'=');
-    if (pos)
-      length = pos -pp;
-    else
-      length = strlen(pp);
-    if (!strncmp(pp,name,length)) break;
+      pos =strchr(pp,'=');
+      if ((pos) && ((pos - pp) == length))
+      {
+          if (!strncmp(pp,name,length)) break;
+      }
   }
-  if ((pp)&& (pos))
+  if ((*pp)&& (pos))
   {
      pp = pos+1;
      TRACE("got %s\n",pp);
   }
+  else
+    pp = 0;
   FreeEnvironmentStringsA( environ );
   return pp;
 }
@@ -48,24 +49,26 @@ WCHAR *_wgetenv(const WCHAR *name)
 {
   WCHAR* environ = GetEnvironmentStringsW();
   WCHAR* pp,*pos = NULL;
-  unsigned int length;
+  unsigned int length=strlenW(name);
 
   for (pp = environ; (*pp); pp = pp + strlenW(pp) + 1)
   {
-    pos = strrchrW(pp,'=');
-    if (pos)
-      length = pos -pp;
-    else
-      length = strlenW(pp);
-    if (!strncmpW(pp,name,length)) break;
-  }
-  if ((pp)&& (pos))
-  {
-     pp = pos+1;
-     TRACE("got %s\n",debugstr_w(pp));
+      pos = strchrW(pp,'=');
+      if ((pos) && ((pos - pp) == length))
+      {
+          if (!strncmpW(pp,name,length))
+          {
+              pp = pos+1;
+              TRACE("got %s\n",debugstr_w(pp));
+              /* can't free pointer since we are returning it */
+              /* should probably use MSVCRT_wenviron instead */
+              FIXME( "memory leak\n" );
+              return pp;
+          }
+      }
   }
   FreeEnvironmentStringsW( environ );
-  return pp;
+  return NULL;
 }
 
 /*********************************************************************
-- 
GitLab