Skip to content

Commit b84e65d

Browse files
committed
merge: fix NULL pointer dereference when merging nothing into void
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 <[email protected]>
1 parent a0feb1b commit b84e65d

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

builtin/merge.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1257,26 +1257,26 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
12571257
builtin_merge_options);
12581258

12591259
if (!head_commit) {
1260-
struct commit *remote_head;
12611260
/*
12621261
* If the merged head is a valid one there is no reason
12631262
* to forbid "git merge" into a branch yet to be born.
12641263
* We do the same for "git pull".
12651264
*/
1265+
unsigned char *remote_head_sha1;
12661266
if (squash)
12671267
die(_("Squash commit into empty head not supported yet"));
12681268
if (fast_forward == FF_NO)
12691269
die(_("Non-fast-forward commit does not make sense into "
12701270
"an empty head"));
12711271
remoteheads = collect_parents(head_commit, &head_subsumed,
12721272
argc, argv, NULL);
1273-
remote_head = remoteheads->item;
1274-
if (!remote_head)
1273+
if (!remoteheads)
12751274
die(_("%s - not something we can merge"), argv[0]);
12761275
if (remoteheads->next)
12771276
die(_("Can merge only exactly one commit into empty head"));
1278-
read_empty(remote_head->object.oid.hash, 0);
1279-
update_ref("initial pull", "HEAD", remote_head->object.oid.hash,
1277+
remote_head_sha1 = remoteheads->item->object.oid.hash;
1278+
read_empty(remote_head_sha1, 0);
1279+
update_ref("initial pull", "HEAD", remote_head_sha1,
12801280
NULL, 0, UPDATE_REFS_DIE_ON_ERR);
12811281
goto done;
12821282
}

t/t7600-merge.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -725,4 +725,14 @@ test_expect_success 'merge detects mod-256 conflicts (resolve)' '
725725
test_must_fail git merge -s resolve master
726726
'
727727

728+
test_expect_success 'merge nothing into void' '
729+
git init void &&
730+
(
731+
cd void &&
732+
git remote add up .. &&
733+
git fetch up &&
734+
test_must_fail git merge FETCH_HEAD
735+
)
736+
'
737+
728738
test_done

0 commit comments

Comments
 (0)