Skip to content

Commit 7cb4a97

Browse files
committed
Merge branch 'en/rev-parse-invalid-range'
"git rev-parse Y..." etc. misbehaved when given endpoints were not committishes. * en/rev-parse-invalid-range: rev-parse: check lookup'ed commit references for NULL
2 parents caf0c98 + 0ed556d commit 7cb4a97

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

builtin/rev-parse.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,10 @@ static int try_difference(const char *arg)
282282
struct commit *a, *b;
283283
a = lookup_commit_reference(&start_oid);
284284
b = lookup_commit_reference(&end_oid);
285+
if (!a || !b) {
286+
*dotdot = '.';
287+
return 0;
288+
}
285289
exclude = get_merge_bases(a, b);
286290
while (exclude) {
287291
struct commit *commit = pop_commit(&exclude);
@@ -328,12 +332,12 @@ static int try_parent_shorthands(const char *arg)
328332
return 0;
329333

330334
*dotdot = 0;
331-
if (get_oid_committish(arg, &oid)) {
335+
if (get_oid_committish(arg, &oid) ||
336+
!(commit = lookup_commit_reference(&oid))) {
332337
*dotdot = '^';
333338
return 0;
334339
}
335340

336-
commit = lookup_commit_reference(&oid);
337341
if (exclude_parent &&
338342
exclude_parent > commit_list_count(commit->parents)) {
339343
*dotdot = '^';

t/t6101-rev-parse-parents.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,4 +214,12 @@ test_expect_success 'rev-list merge^-1x (garbage after ^-1)' '
214214
test_must_fail git rev-list merge^-1x
215215
'
216216

217+
test_expect_success 'rev-parse $garbage^@ does not segfault' '
218+
test_must_fail git rev-parse $EMPTY_TREE^@
219+
'
220+
221+
test_expect_success 'rev-parse $garbage...$garbage does not segfault' '
222+
test_must_fail git rev-parse $EMPTY_TREE...$EMPTY_BLOB
223+
'
224+
217225
test_done

0 commit comments

Comments
 (0)