winpr-env: fix in GetEnvironmentVariableEBA
authorBernhard Miklautz <bernhard.miklautz@thincast.com>
Fri, 14 Feb 2014 10:00:13 +0000 (11:00 +0100)
committerBernhard Miklautz <bernhard.miklautz@thincast.com>
Fri, 14 Feb 2014 10:12:21 +0000 (11:12 +0100)
GetEnvironmentVariableEBA didn't do exact matching of the requested variable name. If a variable
with similar but shorter name was set in the environment block it was returned.
For example if HOME was set in the environment block and HOMEX was requested the value of HOME was
returned.

winpr/libwinpr/environment/environment.c

index 2be5fcd..5c45ccb 100644 (file)
@@ -165,50 +165,57 @@ DWORD GetEnvironmentVariableA(LPCSTR lpName, LPSTR lpBuffer, DWORD nSize)
 
 DWORD GetEnvironmentVariableEBA(LPCSTR envBlock, LPCSTR lpName, LPSTR lpBuffer, DWORD nSize)
 {
-       int length = 0;
+       int vLength = 0;
        char* env = NULL;
        const char * penvb = envBlock;
        char *foundEquals;
+       int nLength, fLength, lpNameLength;
+
+       if (!lpName)
+               return 0;
+
+       lpNameLength = strlen(lpName);
+       if (0 == lpNameLength)
+               return 0;
 
        while (*penvb && *(penvb+1))
        {
-
-               length = strlen(penvb);
+               fLength = strlen(penvb);
                foundEquals = strstr(penvb,"=");
-               if (foundEquals == NULL) {
+               if (foundEquals == NULL)
+               {
                        /* if no = sign is found the envBlock is broken */
                        return 0;
                }
+               nLength = foundEquals - penvb;
+               if (nLength != lpNameLength)
+               {
+                       penvb += (fLength +1);
+                       continue;
+               }
 #ifdef _WIN32
-               if (strnicmp(penvb,lpName,foundEquals - penvb) == 0) {
+               if (strnicmp(penvb,lpName,nLength) == 0)
 #else
-               if (strncmp(penvb,lpName,foundEquals - penvb) == 0) {
+               if (strncmp(penvb,lpName,nLength) == 0)
 #endif
-                       if (*(penvb + (foundEquals - penvb)) == '=') {
-                               // found variable ...
-                               if (foundEquals == NULL) {
-                                       return 0;
-                               } else {
-                                       env = foundEquals + 1;
-                                       break;
-                               }
-                       }
+               {
+                       env = foundEquals + 1;
+                       break;
                }
-               penvb += (length +1);
+               penvb += (fLength +1);
        }
 
-
        if (!env)
                return 0;
 
-       length = strlen(env);
+       vLength = strlen(env);
 
-       if ((length + 1 > nSize) || (!lpBuffer))
-               return length + 1;
+       if ((vLength + 1 > nSize) || (!lpBuffer))
+               return vLength + 1;
 
-       CopyMemory(lpBuffer, env, length + 1);
+       CopyMemory(lpBuffer, env, vLength + 1);
 
-       return length;
+       return vLength;
 }