summaryrefslogtreecommitdiff
path: root/config.c
diff options
context:
space:
mode:
authorJunio C Hamano <gitster@pobox.com>2022-11-28 03:13:43 (GMT)
committerJunio C Hamano <gitster@pobox.com>2022-11-28 03:13:43 (GMT)
commit6accbe3ce7dadd5570bf61154a63d3bef9effb5d (patch)
tree5de61dc9fe7d5bd93a93e96cad8c93dbc62de548 /config.c
parentba88f8c81dcd5968901cdc11b3ade927acc61f7e (diff)
parent14770cf0de218cc373e7d286b864f526e5ea2840 (diff)
downloadgit-6accbe3ce7dadd5570bf61154a63d3bef9effb5d.zip
git-6accbe3ce7dadd5570bf61154a63d3bef9effb5d.tar.gz
git-6accbe3ce7dadd5570bf61154a63d3bef9effb5d.tar.bz2
Merge branch 'pw/config-int-parse-fixes'
Assorted fixes of parsing end-user input as integers. * pw/config-int-parse-fixes: git_parse_signed(): avoid integer overflow config: require at least one digit when parsing numbers git_parse_unsigned: reject negative values
Diffstat (limited to 'config.c')
-rw-r--r--config.c24
1 files changed, 19 insertions, 5 deletions
diff --git a/config.c b/config.c
index c058b2c..27f3828 100644
--- a/config.c
+++ b/config.c
@@ -1160,21 +1160,26 @@ static int git_parse_signed(const char *value, intmax_t *ret, intmax_t max)
if (value && *value) {
char *end;
intmax_t val;
- uintmax_t uval;
- uintmax_t factor;
+ intmax_t factor;
+
+ if (max < 0)
+ BUG("max must be a positive integer");
errno = 0;
val = strtoimax(value, &end, 0);
if (errno == ERANGE)
return 0;
+ if (end == value) {
+ errno = EINVAL;
+ return 0;
+ }
factor = get_unit_factor(end);
if (!factor) {
errno = EINVAL;
return 0;
}
- uval = val < 0 ? -val : val;
- if (unsigned_mult_overflows(factor, uval) ||
- factor * uval > max) {
+ if ((val < 0 && -max / factor > val) ||
+ (val > 0 && max / factor < val)) {
errno = ERANGE;
return 0;
}
@@ -1193,10 +1198,19 @@ static int git_parse_unsigned(const char *value, uintmax_t *ret, uintmax_t max)
uintmax_t val;
uintmax_t factor;
+ /* negative values would be accepted by strtoumax */
+ if (strchr(value, '-')) {
+ errno = EINVAL;
+ return 0;
+ }
errno = 0;
val = strtoumax(value, &end, 0);
if (errno == ERANGE)
return 0;
+ if (end == value) {
+ errno = EINVAL;
+ return 0;
+ }
factor = get_unit_factor(end);
if (!factor) {
errno = EINVAL;