Browse Source

builtin/revert.c: don't dereference a NULL pointer

cherry-pick will segfault when transplanting a root commit if the --ff
option is used.  This happens because the "parent" pointer is set to NULL
when the commit being cherry-picked has no parents.  Later, when "parent"
is dereferenced, the cherry-pick segfaults.

Fix this by checking whether "parent" is NULL before dereferencing it and
add a test for this case of cherry-picking a root commit with --ff.

Reported-by: Zbyszek Szmek <zbyszek@in.waw.pl>
Signed-off-by: Brandon Casey <casey@nrlssc.navy.mil>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
maint
Brandon Casey 15 years ago committed by Junio C Hamano
parent
commit
6355e505ba
  1. 2
      builtin/revert.c
  2. 10
      t/t3506-cherry-pick-ff.sh

2
builtin/revert.c

@ -442,7 +442,7 @@ static int do_pick_commit(void) @@ -442,7 +442,7 @@ static int do_pick_commit(void)
else
parent = commit->parents->item;

if (allow_ff && !hashcmp(parent->object.sha1, head))
if (allow_ff && parent && !hashcmp(parent->object.sha1, head))
return fast_forward_to(commit->object.sha1, head);

if (parent && parse_commit(parent) < 0)

10
t/t3506-cherry-pick-ff.sh

@ -95,4 +95,14 @@ test_expect_success 'cherry pick a merge relative to nonexistent parent with --f @@ -95,4 +95,14 @@ test_expect_success 'cherry pick a merge relative to nonexistent parent with --f
test_must_fail git cherry-pick --ff -m 3 C
'

test_expect_success 'cherry pick a root commit with --ff' '
git reset --hard first -- &&
git rm file1 &&
echo first >file2 &&
git add file2 &&
git commit --amend -m "file2" &&
git cherry-pick --ff first &&
test "$(git rev-parse --verify HEAD)" = "1df192cd8bc58a2b275d842cede4d221ad9000d1"
'

test_done

Loading…
Cancel
Save