git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH 1/3] http.c: introduce `set_long_from_env()` for convenience

From
Taylor Blau <me@ttaylorr.com>
Date
Mar 19, 2025, 18:02 UTC
Message-ID
<Z9sGzWNU5Gn5c/+8@nand.local>
In-Reply-To
<Z9rqK8QSs2dA7t6S@pks.im>
On Wed, Mar 19, 2025 at 05:00:43PM +0100, Patrick Steinhardt wrote:
Show 20 quoted lines
> > diff --git a/http.c b/http.c
> > index 0c9a872809..be564fd520 100644
> > --- a/http.c
> > +++ b/http.c
> > @@ -1256,10 +1256,15 @@ 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)
> > +		*var = strtol(val, NULL, 10);
> > +}
>
> Hm. We don't perform any error checking at all for whether or not the
> value of the environment variable is a valid integer. This isn't a new
> issue introduced by your patch, but now that we have a central place
> where it's being parsed I wonder whether we should be checking for
> errors?

Yeah, I guess it's technically not "new" in the sense that we were already doing:

    xyz = getenv("XYZ");
    if (xyz)
        *var = strtol(xyz, NULL, 10);
I suppose we could do something like:
--- 8< ---
diff --git a/http.c b/http.c
index c13c7da530..6b01ad7a53 100644
--- a/http.c
+++ b/http.c
@@ -1280,8 +1280,20 @@ 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)
-		*var = strtol(val, NULL, 10);
+	if (val) {
+		long tmp;
+		char *endp;
+		errno = 0;
+		tmp = strtol(val, &endp, 10);
+		if (errno)
+			warning_errno(_("failed to parse '%s' (%s) as long"),
+				      envname, val);
+		else if (endp == val)
+			warning(_("failed to parse '%s' (%s) as long"), envname,
+				val);
+		else
+			*var = tmp;
+	}
 }

 void http_init(struct remote *remote, const char *url, int proactive_auth)
--- >8 ---

On top, but TBH I'm not sure how much value it adds. This is only used
for reading GIT_XYZ variables out of the environment, and we're already
pretty lax about strtol() errors in other places. Since this isn't the
interface we expect users to use, I'm OK to punt on it for now unless
you feel strongly otherwise.

Thanks,
Taylor
Previous: Patrick SteinhardtNext: Taylor Blau
Message 5 of 21 in “http: support fine-tuning curl's keepalive behavior”
  1. 0/3 http: support fine-tuning curl's keepalive behaviorTaylor Blau, Mar 18, 2025
  2. 1/3 http.c: introduce `set_long_from_env()` for convenienceTaylor Blau, Mar 18, 2025
  3. Elijah NewrenMar 19, 2025
  4. Patrick SteinhardtMar 19, 2025
  5. Taylor BlauMar 19, 2025
  6. 2/3 http.c: inline `set_curl_keepalive()`Taylor Blau, Mar 18, 2025
  7. Elijah NewrenMar 19, 2025
  8. 3/3 http.c: allow custom TCP keepalive behavior via configTaylor Blau, Mar 18, 2025
  9. Patrick SteinhardtMar 19, 2025
  10. Elijah NewrenMar 19, 2025
  11. Taylor BlauMar 19, 2025
  12. 0/4 http: support fine-tuning curl's keepalive behaviorTaylor Blau, Mar 19, 2025
  13. 1/4 http.c: remove unnecessary casts to longTaylor Blau, Mar 19, 2025
  14. 2/4 http.c: introduce `set_long_from_env()` for convenienceTaylor Blau, Mar 19, 2025
  15. Patrick SteinhardtMar 20, 2025
  16. Jeff KingApr 1, 2025
  17. 3/4 http.c: inline `set_curl_keepalive()`Taylor Blau, Mar 19, 2025
  18. Jeff KingApr 1, 2025
  19. 4/4 http.c: allow custom TCP keepalive behavior via configTaylor Blau, Mar 19, 2025
  20. Jeff KingApr 1, 2025
  21. Elijah NewrenMar 19, 2025

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.