Skip to content

Commit 6355e50

Browse files
drafnelgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Brandon Casey <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 9027fa9 commit 6355e50

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

builtin/revert.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,7 @@ static int do_pick_commit(void)
442442
else
443443
parent = commit->parents->item;
444444

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

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

t/t3506-cherry-pick-ff.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,4 +95,14 @@ test_expect_success 'cherry pick a merge relative to nonexistent parent with --f
9595
test_must_fail git cherry-pick --ff -m 3 C
9696
'
9797

98+
test_expect_success 'cherry pick a root commit with --ff' '
99+
git reset --hard first -- &&
100+
git rm file1 &&
101+
echo first >file2 &&
102+
git add file2 &&
103+
git commit --amend -m "file2" &&
104+
git cherry-pick --ff first &&
105+
test "$(git rev-parse --verify HEAD)" = "1df192cd8bc58a2b275d842cede4d221ad9000d1"
106+
'
107+
98108
test_done

0 commit comments

Comments
 (0)