set errno=0 before strtoX calls

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 <spectral@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Kyle Lippincott committed Aug 5, 2024 at 17:10 UTC b928d57ca9aa7457ec0dee022c1664e8cd606b22
4 files changed +5
builtin/get-tar-commit-id.c
+1
@@ -35,6 +35,7 @@ int cmd_get_tar_commit_id(int argc, const char **argv UNUSED, const char *prefix
35 if (header->typeflag[0] != TYPEFLAG_GLOBAL_HEADER)
36 return 1;
37
38 + errno = 0;
39 len = strtol(content, &end, 10);
40 if (errno == ERANGE || end == content || len < 0)
41 return 1;
ref-filter.c
+1
@@ -1628,6 +1628,7 @@ static void grab_date(const char *buf, struct atom_value *v, const char *atomnam
1628 timestamp = parse_timestamp(eoemail + 2, &zone, 10);
1629 if (timestamp == TIME_MAX)
1630 goto bad;
1631 + errno = 0;
1632 tz = strtol(zone, NULL, 10);
1633 if ((tz == LONG_MIN || tz == LONG_MAX) && errno == ERANGE)
1634 goto bad;
t/helper/test-json-writer.c
+2
@@ -415,6 +415,7 @@ static void get_i(struct line *line, intmax_t *s_in)
415
416 get_s(line, &s);
417
418 + errno = 0;
419 *s_in = strtol(s, &endptr, 10);
420 if (*endptr || errno == ERANGE)
421 die("line[%d]: invalid integer value", line->nr);
@@ -427,6 +428,7 @@ static void get_d(struct line *line, double *s_in)
428
429 get_s(line, &s);
430
431 + errno = 0;
432 *s_in = strtod(s, &endptr);
433 if (*endptr || errno == ERANGE)
434 die("line[%d]: invalid float value", line->nr);
t/helper/test-trace2.c
+1
@@ -26,6 +26,7 @@ static int get_i(int *p_value, const char *data)
26 if (!data || !*data)
27 return MyError;
28
29 + errno = 0;
30 *p_value = strtol(data, &endptr, 10);
31 if (*endptr || errno == ERANGE)
32 return MyError;