[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[gnurl] 115/282: tool_homedir: Change GetEnv() to use libcurl's curl_get
From: |
gnunet |
Subject: |
[gnurl] 115/282: tool_homedir: Change GetEnv() to use libcurl's curl_getenv() |
Date: |
Wed, 01 Apr 2020 14:29:40 +0200 |
This is an automated email from the git hooks/post-receive script.
ng0 pushed a commit to branch master
in repository gnurl.
commit 9dc350b60c1345aea548847de414c55468a4d123
Author: Jay Satiro <address@hidden>
AuthorDate: Wed Jan 29 03:23:55 2020 -0500
tool_homedir: Change GetEnv() to use libcurl's curl_getenv()
- Deduplicate GetEnv() code.
- On Windows change ultimate call to use Windows API
GetEnvironmentVariable() instead of C runtime getenv().
Prior to this change both libcurl and the tool had their own GetEnv
which over time diverged. Now the tool's GetEnv is a wrapper around
curl_getenv (libcurl API function which is itself a wrapper around
libcurl's GetEnv).
Furthermore this change fixes a bug in that Windows API
GetEnvironmentVariable() is called instead of C runtime getenv() to get
the environment variable since some changes aren't always visible to the
latter.
Reported-by: Christoph M. Becker
Fixes https://github.com/curl/curl/issues/4774
Closes https://github.com/curl/curl/pull/4863
---
lib/getenv.c | 45 +++++++++++++++++++++++++++++++++-----------
src/tool_homedir.c | 55 ++++++++++++++++++++++++------------------------------
2 files changed, 58 insertions(+), 42 deletions(-)
diff --git a/lib/getenv.c b/lib/getenv.c
index e444a6a3a..a50226e73 100644
--- a/lib/getenv.c
+++ b/lib/getenv.c
@@ -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[4096];
- 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)
diff --git a/src/tool_homedir.c b/src/tool_homedir.c
index 6bc69551e..e4ea97d38 100644
--- a/src/tool_homedir.c
+++ b/src/tool_homedir.c
@@ -25,38 +25,23 @@
# include <pwd.h>
#endif
+#include <curl/mprintf.h>
+
#include "tool_homedir.h"
#include "memdebug.h" /* keep this as LAST include */
-static char *GetEnv(const char *variable, char do_expand)
+static char *GetEnv(const char *variable)
{
- char *env = NULL;
-#ifdef WIN32
- char buf1[1024], buf2[1024];
- DWORD rc;
+ char *dupe, *env;
- /* Don't use getenv(); it doesn't find variable added after program was
- * started. Don't accept truncated results (i.e. rc >= sizeof(buf1)). */
+ env = curl_getenv(variable);
+ if(!env)
+ return NULL;
- rc = GetEnvironmentVariableA(variable, buf1, sizeof(buf1));
- if(rc > 0 && rc < sizeof(buf1)) {
- env = buf1;
- variable = buf1;
- }
- if(do_expand && strchr(variable, '%')) {
- /* buf2 == variable if not expanded */
- rc = ExpandEnvironmentStringsA(variable, buf2, sizeof(buf2));
- if(rc > 0 && rc < sizeof(buf2) &&
- !strchr(buf2, '%')) /* no vars still unexpanded */
- env = buf2;
- }
-#else
- (void)do_expand;
- /* no length control */
- env = getenv(variable);
-#endif
- return (env && env[0]) ? strdup(env) : NULL;
+ dupe = strdup(env);
+ curl_free(env);
+ return dupe;
}
/* return the home directory of the current user as an allocated string */
@@ -64,11 +49,11 @@ char *homedir(void)
{
char *home;
- home = GetEnv("CURL_HOME", FALSE);
+ home = GetEnv("CURL_HOME");
if(home)
return home;
- home = GetEnv("HOME", FALSE);
+ home = GetEnv("HOME");
if(home)
return home;
@@ -86,10 +71,18 @@ char *homedir(void)
}
#endif /* PWD-stuff */
#ifdef WIN32
- home = GetEnv("APPDATA", TRUE);
- if(!home)
- home = GetEnv("%USERPROFILE%\\Application Data", TRUE); /* Normally only
- on Win-2K/XP */
+ home = GetEnv("APPDATA");
+ if(!home) {
+ char *env = GetEnv("USERPROFILE");
+ if(env) {
+ char *path = curl_maprintf("%s\\Application Data", env);
+ if(path) {
+ home = strdup(path);
+ curl_free(path);
+ }
+ free(env);
+ }
+ }
#endif /* WIN32 */
return home;
}
--
To stop receiving notification emails like this one, please contact
address@hidden.
- [gnurl] 106/282: altsvc: keep a copy of the file name to survive handle reset, (continued)
- [gnurl] 106/282: altsvc: keep a copy of the file name to survive handle reset, gnunet, 2020/04/01
- [gnurl] 112/282: multi: fix outdated comment, gnunet, 2020/04/01
- [gnurl] 113/282: strerror: Increase STRERROR_LEN 128 -> 256, gnunet, 2020/04/01
- [gnurl] 107/282: smtp: Simplify the MAIL command and avoid a duplication of send strings, gnunet, 2020/04/01
- [gnurl] 111/282: easy: remove dead code, gnunet, 2020/04/01
- [gnurl] 108/282: RELEASE-NOTES: synced, gnunet, 2020/04/01
- [gnurl] 120/282: CURLINFO_COOKIELIST.3: Fix example, gnunet, 2020/04/01
- [gnurl] 121/282: ftp: convert 'sock_accepted' to a plain boolean, gnunet, 2020/04/01
- [gnurl] 116/282: tool_home: Fix the copyright year being out of date, gnunet, 2020/04/01
- [gnurl] 114/282: strerror.h: Copyright year out of date, gnunet, 2020/04/01
- [gnurl] 115/282: tool_homedir: Change GetEnv() to use libcurl's curl_getenv(),
gnunet <=
- [gnurl] 118/282: TODO: CURLOPT_SSL_CTX_FUNCTION for LDAPS, gnunet, 2020/04/01
- [gnurl] 126/282: rename: a new file for Curl_rename(), gnunet, 2020/04/01
- [gnurl] 124/282: RELEASE-NOTES: synced, gnunet, 2020/04/01
- [gnurl] 110/282: create-dirs.d: mention the mode, gnunet, 2020/04/01
- [gnurl] 109/282: CURLOPT_ALTSVC_CTRL.3: fix the DEFAULT wording, gnunet, 2020/04/01
- [gnurl] 117/282: azure: disable brotli on the macos debug-builds, gnunet, 2020/04/01
- [gnurl] 125/282: cookies: make saving atomic with a rename, gnunet, 2020/04/01
- [gnurl] 122/282: multi: if Curl_readwrite sets 'comeback' use expire, not loop, gnunet, 2020/04/01
- [gnurl] 119/282: TODO: Paged searches on LDAP server, gnunet, 2020/04/01
- [gnurl] 133/282: HTTP-COOKIES: mention that a trailing newline is required, gnunet, 2020/04/01