Skip to content

Commit

Permalink
merge: fix NULL pointer dereference when merging nothing into void
Browse files Browse the repository at this point in the history
When we are on an unborn branch and merging only one foreign parent,
we allow "git merge" to fast-forward to that foreign parent commit.

This codepath incorrectly attempted to dereference the list of
parents that the merge is going to record even when the list is
empty.  It must refuse to operate instead when there is no parent.

All other codepaths make sure the list is not empty before they
dereference it, and are safe.

Reported-by: Jose Ivan B. Vilarouca Filho
Signed-off-by: Junio C Hamano <gitster@pobox.com>
  • Loading branch information
Junio C Hamano committed Mar 23, 2016
1 parent a0feb1b commit b84e65d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
10 changes: 5 additions & 5 deletions builtin/merge.c
Original file line number Diff line number Diff line change
Expand Up @@ -1257,26 +1257,26 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
builtin_merge_options);

if (!head_commit) {
struct commit *remote_head;
/*
* If the merged head is a valid one there is no reason
* to forbid "git merge" into a branch yet to be born.
* We do the same for "git pull".
*/
unsigned char *remote_head_sha1;
if (squash)
die(_("Squash commit into empty head not supported yet"));
if (fast_forward == FF_NO)
die(_("Non-fast-forward commit does not make sense into "
"an empty head"));
remoteheads = collect_parents(head_commit, &head_subsumed,
argc, argv, NULL);
remote_head = remoteheads->item;
if (!remote_head)
if (!remoteheads)
die(_("%s - not something we can merge"), argv[0]);
if (remoteheads->next)
die(_("Can merge only exactly one commit into empty head"));
read_empty(remote_head->object.oid.hash, 0);
update_ref("initial pull", "HEAD", remote_head->object.oid.hash,
remote_head_sha1 = remoteheads->item->object.oid.hash;
read_empty(remote_head_sha1, 0);
update_ref("initial pull", "HEAD", remote_head_sha1,
NULL, 0, UPDATE_REFS_DIE_ON_ERR);
goto done;
}
Expand Down
10 changes: 10 additions & 0 deletions t/t7600-merge.sh
Original file line number Diff line number Diff line change
Expand Up @@ -725,4 +725,14 @@ test_expect_success 'merge detects mod-256 conflicts (resolve)' '
test_must_fail git merge -s resolve master
'

test_expect_success 'merge nothing into void' '
git init void &&
(
cd void &&
git remote add up .. &&
git fetch up &&
test_must_fail git merge FETCH_HEAD
)
'

test_done

0 comments on commit b84e65d

Please sign in to comment.