git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH] pull: avoid crash of invalid merge head

From
Junio C Hamano <gitster@pobox.com>
Date
Sep 15, 2026, 22:13 UTC
Message-ID
<xmqqld92xixa.fsf@gitster.g>
In-Reply-To
<pull.2223.git.1789252459520.gitgitgadget@gmail.com>
"Jiri Kuncar via GitGitGadget" <gitgitgadget@gmail.com> writes:
Show 14 quoted lines
> From: Jiri Kuncar <jiri.kuncar@gmail.com>
>
> Adds NULL guards for lookup_commit_reference() to avoid segfaults.
>
> Those invalid references are possibly caused by parallel fetches or
> gc racing on the same repository.
>
> This effectively treats failed lookup as "not up to date" so caller
> falls to a normal merge, which reports the broken object instead of
> crashing.
>
> Signed-off-by: Jiri Kuncar <jiri.kuncar@gmail.com>
> ---
>     pull: avoid crash of invalid merge head

The log message sounds a bit unusual from our norm (see Documentation/SubmittingPatches).

It is of course good to deal with a corrupt state more gracefully rather than crashing. From a cursory look, the particular solution chosen, to drive the caller to perform a merge and have it fail, may smell a bit like cheating, in that we could diagnose the breakage better by reporting what was broken at each place, but it probably is a good choice.

If we really want to improve the situation for 'orig_head', for example, we would probably want to turn it into a commit object instance a lot earlier and pass the commit object instance around in the call chain. Passing around many struct object_id instances instead of object instances is an unnatural consequence of how this program evolved. It was originally written as a shell script, and of course passing hexadecimal object names was the only way the script could drive 'git merge-base' and other programs to see if the commit recorded as the current 'HEAD' will fast-forward to the commit that is fetched from the remote to be merged in, for example. Once we go that route to resolve object names early to object instances, we will not have multiple lookup_commit_reference() calls on the same object name (which require us to watch out for failures) to begin with.

The above is a long-winded way to say that it is a good improvement that does not do more than it needs to do and we will not have to spend too much effort to undo when we revamp the internals to do "the right thing" later.

Show 33 quoted lines
> diff --git a/t/t5520-pull.sh b/t/t5520-pull.sh
> index 27f38ab3c8..7a3eadddd3 100755
> --- a/t/t5520-pull.sh
> +++ b/t/t5520-pull.sh
> @@ -888,4 +888,30 @@ test_expect_success 'git pull --rebase against local branch' '
>  	test_cmp expect file2
>  '
>  
> +test_expect_success 'pull does not crash when a merge head does not resolve' '
> +	test_when_finished "rm -rf up dn" &&
> +	git init up &&
> +	(
> +		cd up &&
> +		test_commit base &&
> +		git switch -c sideA &&
> +		test_commit a &&
> +		git switch -c sideB base &&
> +		test_commit b
> +	) &&
> +	git clone up dn &&
> +	(
> +		cd dn &&
> +		git -c fetch.unpackLimit=1000 fetch origin \
> +			"+refs/heads/*:refs/remotes/origin/*" &&
> +		git commit-graph write --reachable &&
> +		oid=$(git rev-parse refs/remotes/origin/sideA) &&
> +		obj=.git/objects/$(test_oid_to_path "$oid") &&
> +		test -f "$obj" &&
> +		chmod u+w "$obj" &&
> +		>"$obj" &&
> +		test_must_fail git pull --no-rebase origin sideA sideB
> +	)
> +'

The "test -f" there smells more like a debugging aid for this test than making sure the fixed program works as expected. I wonder if it is simpler (and more portable to non-POSIX environments) if we replace the "corrupt $obj" step with 'rm -f "$obj"'.

Thanks.
Previous: Jiri Kuncar via GitGitGadgetNext: Jiri Kuncar via GitGitGadget
Message 2 of 3 in “pull: avoid crash of invalid merge head”
  1. pull: avoid crash of invalid merge headJiri Kuncar via GitGitGadget, Sep 12, 2026
  2. Junio C HamanoSep 15, 2026
  3. pull: avoid segfault when commit lookup failsJiri Kuncar via GitGitGadget, Sep 21, 2026

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.