Skip to content

Commit 8f282bd

Browse files
pks-tgitster
authored andcommitted
parse: fix off-by-one for minimum signed values
We accept a maximum value in `git_parse_signed()` that restricts the range of accepted integers. As the intent is to pass `INT*_MAX` values here, this maximum doesn't only act as the upper bound, but also as the implicit lower bound of the accepted range. This lower bound is calculated by negating the maximum. But given that the maximum value of a signed integer with N bits is `2^(N-1)-1` whereas the minimum value is `-2^(N-1)` we have an off-by-one error in the lower bound. Fix this off-by-one error by using `-max - 1` as lower bound instead. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 5b97a56 commit 8f282bd

File tree

1 file changed

+1
-1
lines changed

1 file changed

+1
-1
lines changed

parse.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ int git_parse_signed(const char *value, intmax_t *ret, intmax_t max)
3838
errno = EINVAL;
3939
return 0;
4040
}
41-
if ((val < 0 && -max / factor > val) ||
41+
if ((val < 0 && (-max - 1) / factor > val) ||
4242
(val > 0 && max / factor < val)) {
4343
errno = ERANGE;
4444
return 0;

0 commit comments

Comments
 (0)