Skip to content

Commit 7595c0e

Browse files
phillipwoodttaylorr
authored andcommitted
config: require at least one digit when parsing numbers
If the input to strtoimax() or strtoumax() does not contain any digits then they return zero and set `end` to point to the start of the input string. git_parse_[un]signed() do not check `end` and so fail to return an error and instead return a value of zero if the input string is a valid units factor without any digits (e.g "k"). Tests are added to check that 'git config --int' and OPT_MAGNITUDE() reject a units specifier without a leading digit. Helped-by: Jeff King <[email protected]> Signed-off-by: Phillip Wood <[email protected]> Signed-off-by: Taylor Blau <[email protected]>
1 parent 84356ff commit 7595c0e

File tree

3 files changed

+21
-0
lines changed

3 files changed

+21
-0
lines changed

config.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1167,6 +1167,10 @@ static int git_parse_signed(const char *value, intmax_t *ret, intmax_t max)
11671167
val = strtoimax(value, &end, 0);
11681168
if (errno == ERANGE)
11691169
return 0;
1170+
if (end == value) {
1171+
errno = EINVAL;
1172+
return 0;
1173+
}
11701174
factor = get_unit_factor(end);
11711175
if (!factor) {
11721176
errno = EINVAL;
@@ -1202,6 +1206,10 @@ static int git_parse_unsigned(const char *value, uintmax_t *ret, uintmax_t max)
12021206
val = strtoumax(value, &end, 0);
12031207
if (errno == ERANGE)
12041208
return 0;
1209+
if (end == value) {
1210+
errno = EINVAL;
1211+
return 0;
1212+
}
12051213
factor = get_unit_factor(end);
12061214
if (!factor) {
12071215
errno = EINVAL;

t/t0040-parse-options.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -714,4 +714,11 @@ test_expect_success 'negative magnitude' '
714714
grep "non-negative integer" err &&
715715
test_must_be_empty out
716716
'
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+
717724
test_done

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)