Skip to content

Commit a5953f6

Browse files
committed
Merge branch 'jv/merge-nothing-into-void' into maint
"git merge FETCH_HEAD" dereferenced NULL pointer when merging nothing into an unborn history (which is arguably unusual usage, which perhaps was the reason why nobody noticed it). * jv/merge-nothing-into-void: merge: fix NULL pointer dereference when merging nothing into void
2 parents ea7fefb + b84e65d commit a5953f6

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
@@ -753,4 +753,14 @@ test_expect_success 'merge detects mod-256 conflicts (resolve)' '
753753
test_must_fail git merge -s resolve master
754754
'
755755

756+
test_expect_success 'merge nothing into void' '
757+
git init void &&
758+
(
759+
cd void &&
760+
git remote add up .. &&
761+
git fetch up &&
762+
test_must_fail git merge FETCH_HEAD
763+
)
764+
'
765+
756766
test_done

0 commit comments

Comments
 (0)