Skip to content

Commit 3fb72c7

Browse files
rscharfegitster
authored andcommitted
config: use unsigned_mult_overflows to check for overflows
parse_unit_factor() checks if a K, M or G is present after a number and multiplies it by 2^10, 2^20 or 2^30, respectively. One of its callers checks if the result is smaller than the number alone to detect overflows. The other one passes 1 as the number and does multiplication and overflow check itself in a similar manner. This works, but is inconsistent, and it would break if we added support for a bigger unit factor. E.g. 16777217T is 2^64 + 2^40, i.e. too big for a 64-bit number. Modulo 2^64 we get 2^40 == 1TB, which is bigger than the raw number 16777217 == 2^24 + 1, so the overflow would go undetected by that method. Let both callers pass 1 and handle overflow check and multiplication themselves. Do the check before the multiplication, using unsigned_mult_overflows, which is simpler and can deal with larger unit factors. Signed-off-by: Rene Scharfe <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 9dae4fe commit 3fb72c7

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

config.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -870,8 +870,8 @@ static int git_parse_signed(const char *value, intmax_t *ret, intmax_t max)
870870
return 0;
871871
}
872872
uval = val < 0 ? -val : val;
873-
uval *= factor;
874-
if (uval > max || (val < 0 ? -val : val) > uval) {
873+
if (unsigned_mult_overflows(factor, uval) ||
874+
factor * uval > max) {
875875
errno = ERANGE;
876876
return 0;
877877
}
@@ -888,21 +888,22 @@ static int git_parse_unsigned(const char *value, uintmax_t *ret, uintmax_t max)
888888
if (value && *value) {
889889
char *end;
890890
uintmax_t val;
891-
uintmax_t oldval;
891+
uintmax_t factor = 1;
892892

893893
errno = 0;
894894
val = strtoumax(value, &end, 0);
895895
if (errno == ERANGE)
896896
return 0;
897-
oldval = val;
898-
if (!parse_unit_factor(end, &val)) {
897+
if (!parse_unit_factor(end, &factor)) {
899898
errno = EINVAL;
900899
return 0;
901900
}
902-
if (val > max || oldval > val) {
901+
if (unsigned_mult_overflows(factor, val) ||
902+
factor * val > max) {
903903
errno = ERANGE;
904904
return 0;
905905
}
906+
val *= factor;
906907
*ret = val;
907908
return 1;
908909
}

0 commit comments

Comments
 (0)