]> git.ipfire.org Git - thirdparty/git.git/commitdiff
http.c: introduce `set_long_from_env()` for convenience
authorTaylor Blau <me@ttaylorr.com>
Wed, 19 Mar 2025 22:23:50 +0000 (18:23 -0400)
committerJunio C Hamano <gitster@pobox.com>
Fri, 21 Mar 2025 08:38:41 +0000 (01:38 -0700)
In 7059cd99fc (http_init(): Fix config file parsing, 2009-03-09), http.c
gained a new "set_from_env()" function as a convenience function around
conditionally assigning an environment variable to some variable if and
only if the environment variable was set to begin with.

But prior to 7059cd99fc, there were two spots which need to first
strtol() whatever is set in the environment before assigning it to a
long pointer. Both instances stored the result of getenv() in a
temporary variable, and conditionally strtol() it depending on whether
or not getenv() returned NULL.

Replace those two instances with a new cousin of 'set_from_env()' called
'set_long_from_env()', which does what its name suggests. This allows us
to remove the temporary variables and clean up some minor code
duplication while also adding more robust error handling.

More importantly, however, it prepares us for a future commit which will
introduce more instances of assigning an environment variable to a long.

Signed-off-by: Taylor Blau <me@ttaylorr.com>
Acked-by: Elijah Newren <newren@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
http.c

diff --git a/http.c b/http.c
index 0cbcb079b29542e237be481391ebb8b23320ae29..17b676a1d5a0e098f409cc489d29ee9e6ff1f589 100644 (file)
--- a/http.c
+++ b/http.c
@@ -1256,10 +1256,30 @@ static void set_from_env(char **var, const char *envname)
        }
 }
 
+static void set_long_from_env(long *var, const char *envname)
+{
+       const char *val = getenv(envname);
+       if (val) {
+               long tmp;
+               char *endp;
+               int saved_errno = errno;
+
+               errno = 0;
+               tmp = strtol(val, &endp, 10);
+
+               if (errno)
+                       warning_errno(_("failed to parse %s"), envname);
+               else if (*endp || endp == val)
+                       warning(_("failed to parse %s"), envname);
+               else
+                       *var = tmp;
+
+               errno = saved_errno;
+       }
+}
+
 void http_init(struct remote *remote, const char *url, int proactive_auth)
 {
-       char *low_speed_limit;
-       char *low_speed_time;
        char *normalized_url;
        struct urlmatch_config config = URLMATCH_CONFIG_INIT;
 
@@ -1338,12 +1358,8 @@ void http_init(struct remote *remote, const char *url, int proactive_auth)
 
        set_from_env(&user_agent, "GIT_HTTP_USER_AGENT");
 
-       low_speed_limit = getenv("GIT_HTTP_LOW_SPEED_LIMIT");
-       if (low_speed_limit)
-               curl_low_speed_limit = strtol(low_speed_limit, NULL, 10);
-       low_speed_time = getenv("GIT_HTTP_LOW_SPEED_TIME");
-       if (low_speed_time)
-               curl_low_speed_time = strtol(low_speed_time, NULL, 10);
+       set_long_from_env(&curl_low_speed_limit, "GIT_HTTP_LOW_SPEED_LIMIT");
+       set_long_from_env(&curl_low_speed_time, "GIT_HTTP_LOW_SPEED_TIME");
 
        if (curl_ssl_verify == -1)
                curl_ssl_verify = 1;