Skip to content

Commit 6accbe3

Browse files
committed
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
2 parents ba88f8c + 14770cf commit 6accbe3

File tree

4 files changed

+43
-5
lines changed

4 files changed

+43
-5
lines changed

config.c

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1160,21 +1160,26 @@ static int git_parse_signed(const char *value, intmax_t *ret, intmax_t max)
11601160
if (value && *value) {
11611161
char *end;
11621162
intmax_t val;
1163-
uintmax_t uval;
1164-
uintmax_t factor;
1163+
intmax_t factor;
1164+
1165+
if (max < 0)
1166+
BUG("max must be a positive integer");
11651167

11661168
errno = 0;
11671169
val = strtoimax(value, &end, 0);
11681170
if (errno == ERANGE)
11691171
return 0;
1172+
if (end == value) {
1173+
errno = EINVAL;
1174+
return 0;
1175+
}
11701176
factor = get_unit_factor(end);
11711177
if (!factor) {
11721178
errno = EINVAL;
11731179
return 0;
11741180
}
1175-
uval = val < 0 ? -val : val;
1176-
if (unsigned_mult_overflows(factor, uval) ||
1177-
factor * uval > max) {
1181+
if ((val < 0 && -max / factor > val) ||
1182+
(val > 0 && max / factor < val)) {
11781183
errno = ERANGE;
11791184
return 0;
11801185
}
@@ -1193,10 +1198,19 @@ static int git_parse_unsigned(const char *value, uintmax_t *ret, uintmax_t max)
11931198
uintmax_t val;
11941199
uintmax_t factor;
11951200

1201+
/* negative values would be accepted by strtoumax */
1202+
if (strchr(value, '-')) {
1203+
errno = EINVAL;
1204+
return 0;
1205+
}
11961206
errno = 0;
11971207
val = strtoumax(value, &end, 0);
11981208
if (errno == ERANGE)
11991209
return 0;
1210+
if (end == value) {
1211+
errno = EINVAL;
1212+
return 0;
1213+
}
12001214
factor = get_unit_factor(end);
12011215
if (!factor) {
12021216
errno = EINVAL;

t/t0040-parse-options.sh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -709,4 +709,16 @@ test_expect_success 'subcommands are incompatible with KEEP_DASHDASH unless in c
709709
grep ^BUG err
710710
'
711711

712+
test_expect_success 'negative magnitude' '
713+
test_must_fail test-tool parse-options --magnitude -1 >out 2>err &&
714+
grep "non-negative integer" err &&
715+
test_must_be_empty out
716+
'
717+
718+
test_expect_success 'magnitude with units but no numbers' '
719+
test_must_fail test-tool parse-options --magnitude m >out 2>err &&
720+
grep "non-negative integer" err &&
721+
test_must_be_empty out
722+
'
723+
712724
test_done

t/t1050-large.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ test_description='adding and checking out large blobs'
55

66
. ./test-lib.sh
77

8+
test_expect_success 'core.bigFileThreshold must be non-negative' '
9+
test_must_fail git -c core.bigFileThreshold=-1 rev-parse >out 2>err &&
10+
grep "bad numeric config value" err &&
11+
test_must_be_empty out
12+
'
13+
814
test_expect_success setup '
915
# clone does not allow us to pass core.bigfilethreshold to
1016
# new repos, so set core.bigfilethreshold globally

t/t1300-config.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2228,6 +2228,12 @@ test_expect_success '--type rejects unknown specifiers' '
22282228
test_i18ngrep "unrecognized --type argument" error
22292229
'
22302230

2231+
test_expect_success '--type=int requires at least one digit' '
2232+
test_must_fail git config --type int --default m some.key >out 2>error &&
2233+
grep "bad numeric config value" error &&
2234+
test_must_be_empty out
2235+
'
2236+
22312237
test_expect_success '--replace-all does not invent newlines' '
22322238
q_to_tab >.git/config <<-\EOF &&
22332239
[abc]key

0 commit comments

Comments
 (0)