summaryrefslogtreecommitdiffstats
path: root/Utilities/cmcurl/lib/getenv.c
diff options
context:
space:
mode:
authorBrad King <brad.king@kitware.com>2020-03-04 19:34:39 (GMT)
committerBrad King <brad.king@kitware.com>2020-03-04 19:34:39 (GMT)
commitd61c3bd50520ce0179f52a129d5c660aebad88ba (patch)
tree4476c31b8b03de36811d94b5d9282318e84784b2 /Utilities/cmcurl/lib/getenv.c
parent84dc14a967b78431c95ed3203e5dd301b6897267 (diff)
parent735ea3001ae98636a4cb7caf15a40960c0da39a1 (diff)
downloadCMake-d61c3bd50520ce0179f52a129d5c660aebad88ba.zip
CMake-d61c3bd50520ce0179f52a129d5c660aebad88ba.tar.gz
CMake-d61c3bd50520ce0179f52a129d5c660aebad88ba.tar.bz2
Merge branch 'upstream-curl' into update-curl
* upstream-curl: curl 2020-03-04 (b8d13668)
Diffstat (limited to 'Utilities/cmcurl/lib/getenv.c')
-rw-r--r--Utilities/cmcurl/lib/getenv.c47
1 files changed, 35 insertions, 12 deletions
diff --git a/Utilities/cmcurl/lib/getenv.c b/Utilities/cmcurl/lib/getenv.c
index 89d181d..9385b8f 100644
--- a/Utilities/cmcurl/lib/getenv.c
+++ b/Utilities/cmcurl/lib/getenv.c
@@ -5,7 +5,7 @@
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
- * Copyright (C) 1998 - 2012, Daniel Stenberg, <daniel@haxx.se>, et al.
+ * Copyright (C) 1998 - 2020, Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
@@ -27,25 +27,48 @@
#include "memdebug.h"
-static
-char *GetEnv(const char *variable)
+static char *GetEnv(const char *variable)
{
#if defined(_WIN32_WCE) || defined(CURL_WINDOWS_APP)
(void)variable;
return NULL;
-#else
-#ifdef WIN32
- char env[MAX_PATH]; /* MAX_PATH is from windef.h */
- char *temp = getenv(variable);
- env[0] = '\0';
- if(temp != NULL)
- ExpandEnvironmentStringsA(temp, env, sizeof(env));
- return (env[0] != '\0')?strdup(env):NULL;
+#elif defined(WIN32)
+ /* This uses Windows API instead of C runtime getenv() to get the environment
+ variable since some changes aren't always visible to the latter. #4774 */
+ char *buf = NULL;
+ char *tmp;
+ DWORD bufsize;
+ DWORD rc = 1;
+ const DWORD max = 32768; /* max env var size from MSCRT source */
+
+ for(;;) {
+ tmp = realloc(buf, rc);
+ if(!tmp) {
+ free(buf);
+ return NULL;
+ }
+
+ buf = tmp;
+ bufsize = rc;
+
+ /* It's possible for rc to be 0 if the variable was found but empty.
+ Since getenv doesn't make that distinction we ignore it as well. */
+ rc = GetEnvironmentVariableA(variable, buf, bufsize);
+ if(!rc || rc == bufsize || rc > max) {
+ free(buf);
+ return NULL;
+ }
+
+ /* if rc < bufsize then rc is bytes written not including null */
+ if(rc < bufsize)
+ return buf;
+
+ /* else rc is bytes needed, try again */
+ }
#else
char *env = getenv(variable);
return (env && env[0])?strdup(env):NULL;
#endif
-#endif
}
char *curl_getenv(const char *v)