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>
Brandon Casey committed
Sep 27, 2010 at 12:29 UTC
6355e505bacf0a12a5de77bcd0c1198e74ad340b
2 files changed
+11
-1
builtin/revert.c
+1
-1
@@ -442,7 +442,7 @@ static int do_pick_commit(void)
442
else
443
parent = commit->parents->item;
444
445
- if (allow_ff && !hashcmp(parent->object.sha1, head))
445
+ if (allow_ff && parent && !hashcmp(parent->object.sha1, head))
446
return fast_forward_to(commit->object.sha1, head);
447
448
if (parent && parse_commit(parent) < 0)
t/t3506-cherry-pick-ff.sh
+10
@@ -95,4 +95,14 @@ test_expect_success 'cherry pick a merge relative to nonexistent parent with --f
95
test_must_fail git cherry-pick --ff -m 3 C
96
'
97
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
+
108
test_done