Skip to content

Commit 0ed556d

Browse files
newrengitster
authored andcommitted
rev-parse: check lookup'ed commit references for NULL
Commits 2122f8b ("rev-parse: Add support for the ^! and ^@ syntax", 2008-07-26) and 3dd4e73 ("Teach rev-parse the ... syntax.", 2006-07-04) taught rev-parse new syntax, and used lookup_commit_reference() as part of their logic. Neither usage checked the returned commit to see if it was non-NULL before using it. Check for NULL and ensure an appropriate error is reported to the user. Reported by Florian Weimer and Todd Zullinger. Helped-by: Jeff King <[email protected]> Signed-off-by: Elijah Newren <[email protected]> Reviewed-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent d32eb83 commit 0ed556d

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(&oid);
284284
b = lookup_commit_reference(&end);
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)