Skip to content

Commit 773521d

Browse files
committed
Merge branch 'rs/nth-parent-parse'
The object name parser for "Nth parent" syntax has been made more robust against integer overflows. * rs/nth-parent-parse: sha1-name: check for overflow of N in "foo^N" and "foo~N" rev-parse: demonstrate overflow of N for "foo^N" and "foo~N"
2 parents 7f17913 + 59fa5f5 commit 773521d

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

sha1-name.c

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

11621162
if (has_suffix) {
1163-
int num = 0;
1163+
unsigned int num = 0;
11641164
int len1 = cp - name;
11651165
cp++;
1166-
while (cp < name + len)
1167-
num = num * 10 + *cp++ - '0';
1166+
while (cp < name + len) {
1167+
unsigned int digit = *cp++ - '0';
1168+
if (unsigned_mult_overflows(num, 10))
1169+
return MISSING_OBJECT;
1170+
num *= 10;
1171+
if (unsigned_add_overflows(num, digit))
1172+
return MISSING_OBJECT;
1173+
num += digit;
1174+
}
11681175
if (!num && len1 == len - 1)
11691176
num = 1;
1177+
else if (num > INT_MAX)
1178+
return MISSING_OBJECT;
11701179
if (has_suffix == '^')
11711180
return get_parent(r, name, len1, oid, num);
11721181
/* else if (has_suffix == '~') -- goes without saying */

t/t1506-rev-parse-diagnosis.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,4 +214,12 @@ test_expect_success 'arg before dashdash must be a revision (ambiguous)' '
214214
test_cmp expect actual
215215
'
216216

217+
test_expect_success 'reject Nth parent if N is too high' '
218+
test_must_fail git rev-parse HEAD^100000000000000000000000000000000
219+
'
220+
221+
test_expect_success 'reject Nth ancestor if N is too high' '
222+
test_must_fail git rev-parse HEAD~100000000000000000000000000000000
223+
'
224+
217225
test_done

0 commit comments

Comments
 (0)