Skip to content

Commit b928d57

Browse files
spectral54gitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 39bf06a commit b928d57

File tree

4 files changed

+5
-0
lines changed

4 files changed

+5
-0
lines changed

builtin/get-tar-commit-id.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ int cmd_get_tar_commit_id(int argc, const char **argv UNUSED, const char *prefix
3535
if (header->typeflag[0] != TYPEFLAG_GLOBAL_HEADER)
3636
return 1;
3737

38+
errno = 0;
3839
len = strtol(content, &end, 10);
3940
if (errno == ERANGE || end == content || len < 0)
4041
return 1;

ref-filter.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1628,6 +1628,7 @@ static void grab_date(const char *buf, struct atom_value *v, const char *atomnam
16281628
timestamp = parse_timestamp(eoemail + 2, &zone, 10);
16291629
if (timestamp == TIME_MAX)
16301630
goto bad;
1631+
errno = 0;
16311632
tz = strtol(zone, NULL, 10);
16321633
if ((tz == LONG_MIN || tz == LONG_MAX) && errno == ERANGE)
16331634
goto bad;

t/helper/test-json-writer.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,7 @@ static void get_i(struct line *line, intmax_t *s_in)
415415

416416
get_s(line, &s);
417417

418+
errno = 0;
418419
*s_in = strtol(s, &endptr, 10);
419420
if (*endptr || errno == ERANGE)
420421
die("line[%d]: invalid integer value", line->nr);
@@ -427,6 +428,7 @@ static void get_d(struct line *line, double *s_in)
427428

428429
get_s(line, &s);
429430

431+
errno = 0;
430432
*s_in = strtod(s, &endptr);
431433
if (*endptr || errno == ERANGE)
432434
die("line[%d]: invalid float value", line->nr);

t/helper/test-trace2.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ static int get_i(int *p_value, const char *data)
2626
if (!data || !*data)
2727
return MyError;
2828

29+
errno = 0;
2930
*p_value = strtol(data, &endptr, 10);
3031
if (*endptr || errno == ERANGE)
3132
return MyError;

0 commit comments

Comments
 (0)