Skip to content

Commit 59fa5f5

Browse files
rscharfegitster
authored andcommitted
sha1-name: check for overflow of N in "foo^N" and "foo~N"
Reject values that don't fit into an int, as get_parent() and get_nth_ancestor() cannot handle them. That's better than potentially returning a random object. If this restriction turns out to be too tight then we can switch to a wider data type, but we'd still have to check for overflow. Signed-off-by: René Scharfe <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent a678df1 commit 59fa5f5

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

sha1-name.c

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1163,13 +1163,22 @@ static enum get_oid_result get_oid_1(struct repository *r,
11631163
}
11641164

11651165
if (has_suffix) {
1166-
int num = 0;
1166+
unsigned int num = 0;
11671167
int len1 = cp - name;
11681168
cp++;
1169-
while (cp < name + len)
1170-
num = num * 10 + *cp++ - '0';
1169+
while (cp < name + len) {
1170+
unsigned int digit = *cp++ - '0';
1171+
if (unsigned_mult_overflows(num, 10))
1172+
return MISSING_OBJECT;
1173+
num *= 10;
1174+
if (unsigned_add_overflows(num, digit))
1175+
return MISSING_OBJECT;
1176+
num += digit;
1177+
}
11711178
if (!num && len1 == len - 1)
11721179
num = 1;
1180+
else if (num > INT_MAX)
1181+
return MISSING_OBJECT;
11731182
if (has_suffix == '^')
11741183
return get_parent(r, name, len1, oid, num);
11751184
/* else if (has_suffix == '~') -- goes without saying */

t/t1506-rev-parse-diagnosis.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,11 +215,11 @@ test_expect_success 'arg before dashdash must be a revision (ambiguous)' '
215215
test_cmp expect actual
216216
'
217217

218-
test_expect_failure 'reject Nth parent if N is too high' '
218+
test_expect_success 'reject Nth parent if N is too high' '
219219
test_must_fail git rev-parse HEAD^100000000000000000000000000000000
220220
'
221221

222-
test_expect_failure 'reject Nth ancestor if N is too high' '
222+
test_expect_success 'reject Nth ancestor if N is too high' '
223223
test_must_fail git rev-parse HEAD~100000000000000000000000000000000
224224
'
225225

0 commit comments

Comments
 (0)