Subject: Re: [PATCH v2 1/3] set errno=0 before strtoX calls

From: Junio C Hamano <[email-masked]>
Date: Fri, 02 Aug 2024 14:18:31 -0700

"Kyle Lippincott via GitGitGadget" <[email-masked]> writes:

> From: Kyle Lippincott <[email-masked]>
>
> To detect conversion failure after calls to functions like `strtod`, one
> can check `errno == ERANGE`. These functions are not guaranteed to set
> `errno` to `0` on successful conversion, however. Manual manipulation of
> `errno` can likely be avoided by checking that the output pointer
> differs from the input pointer, but that's not how other locations, such
> as parse.c:139, handle this issue; they set errno to 0 prior to
> executing the function.
>
> For every place I could find a strtoX function with an ERANGE check
> following it, set `errno = 0;` prior to executing the conversion
> function.
>
> Signed-off-by: Kyle Lippincott <[email-masked]>
> ---
>  builtin/get-tar-commit-id.c | 1 +
>  ref-filter.c                | 1 +
>  t/helper/test-json-writer.c | 2 ++
>  t/helper/test-trace2.c      | 1 +
>  4 files changed, 5 insertions(+)

Clearilng before strtoX() call like these changes make perfect sense
(within the constraint of strtoX() API, which is horrible as pointed
out by others many times in the past ;-)

Thanks, will queue.

> diff --git a/builtin/get-tar-commit-id.c b/builtin/get-tar-commit-id.c
> index 66a7389f9f4..7195a072edc 100644
> --- a/builtin/get-tar-commit-id.c
> +++ b/builtin/get-tar-commit-id.c
> @@ -35,6 +35,7 @@ int cmd_get_tar_commit_id(int argc, const char **argv UNUSED, const char *prefix
>  	if (header->typeflag[0] != TYPEFLAG_GLOBAL_HEADER)
>  		return 1;
>  
> +	errno = 0;
>  	len = strtol(content, &end, 10);
>  	if (errno == ERANGE || end == content || len < 0)
>  		return 1;
> diff --git a/ref-filter.c b/ref-filter.c
> index 8c5e673fc0a..54880a2497a 100644
> --- a/ref-filter.c
> +++ b/ref-filter.c
> @@ -1628,6 +1628,7 @@ static void grab_date(const char *buf, struct atom_value *v, const char *atomnam
>  	timestamp = parse_timestamp(eoemail + 2, &zone, 10);
>  	if (timestamp == TIME_MAX)
>  		goto bad;
> +	errno = 0;
>  	tz = strtol(zone, NULL, 10);
>  	if ((tz == LONG_MIN || tz == LONG_MAX) && errno == ERANGE)
>  		goto bad;
> diff --git a/t/helper/test-json-writer.c b/t/helper/test-json-writer.c
> index ed52eb76bfc..a288069b04c 100644
> --- a/t/helper/test-json-writer.c
> +++ b/t/helper/test-json-writer.c
> @@ -415,6 +415,7 @@ static void get_i(struct line *line, intmax_t *s_in)
>  
>  	get_s(line, &s);
>  
> +	errno = 0;
>  	*s_in = strtol(s, &endptr, 10);
>  	if (*endptr || errno == ERANGE)
>  		die("line[%d]: invalid integer value", line->nr);
> @@ -427,6 +428,7 @@ static void get_d(struct line *line, double *s_in)
>  
>  	get_s(line, &s);
>  
> +	errno = 0;
>  	*s_in = strtod(s, &endptr);
>  	if (*endptr || errno == ERANGE)
>  		die("line[%d]: invalid float value", line->nr);
> diff --git a/t/helper/test-trace2.c b/t/helper/test-trace2.c
> index cd955ec63e9..c588c273ce7 100644
> --- a/t/helper/test-trace2.c
> +++ b/t/helper/test-trace2.c
> @@ -26,6 +26,7 @@ static int get_i(int *p_value, const char *data)
>  	if (!data || !*data)
>  		return MyError;
>  
> +	errno = 0;
>  	*p_value = strtol(data, &endptr, 10);
>  	if (*endptr || errno == ERANGE)
>  		return MyError;