Skip to content

Commit 33fdd77

Browse files
peffgitster
authored andcommitted
config: set errno in numeric git_parse_* functions
When we are parsing an integer or unsigned long, we use the strto*max functions, which properly set errno to ERANGE if we get a large value. However, we also do further range checks after applying our multiplication factor, but do not set ERANGE. This means that a caller cannot tell if an error was caused by ERANGE or if the input was simply not a valid number. This patch teaches git_parse_signed and git_parse_unsigned to set ERANGE for range errors, and EINVAL for other errors, so that the caller can reliably tell these cases apart. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 42d194e commit 33fdd77

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

config.c

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -480,16 +480,21 @@ static int git_parse_signed(const char *value, intmax_t *ret, intmax_t max)
480480
val = strtoimax(value, &end, 0);
481481
if (errno == ERANGE)
482482
return 0;
483-
if (!parse_unit_factor(end, &factor))
483+
if (!parse_unit_factor(end, &factor)) {
484+
errno = EINVAL;
484485
return 0;
486+
}
485487
uval = abs(val);
486488
uval *= factor;
487-
if (uval > max || abs(val) > uval)
489+
if (uval > max || abs(val) > uval) {
490+
errno = ERANGE;
488491
return 0;
492+
}
489493
val *= factor;
490494
*ret = val;
491495
return 1;
492496
}
497+
errno = EINVAL;
493498
return 0;
494499
}
495500

@@ -505,13 +510,18 @@ int git_parse_unsigned(const char *value, uintmax_t *ret, uintmax_t max)
505510
if (errno == ERANGE)
506511
return 0;
507512
oldval = val;
508-
if (!parse_unit_factor(end, &val))
513+
if (!parse_unit_factor(end, &val)) {
514+
errno = EINVAL;
509515
return 0;
510-
if (val > max || oldval > val)
516+
}
517+
if (val > max || oldval > val) {
518+
errno = ERANGE;
511519
return 0;
520+
}
512521
*ret = val;
513522
return 1;
514523
}
524+
errno = EINVAL;
515525
return 0;
516526
}
517527

0 commit comments

Comments
 (0)