public inbox for git@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] fix(clone): segment fault when using --revision and protocol v0/v1
@ 2026-02-01  9:23 NitroCao via GitGitGadget
  2026-02-03 11:56 ` [PATCH v2] clone: fix segfault when using --revision and v0/v1 protocol NitroCao via GitGitGadget
  0 siblings, 1 reply; 5+ messages in thread
From: NitroCao via GitGitGadget @ 2026-02-01  9:23 UTC (permalink / raw)
  To: git; +Cc: NitroCao, Nitro Cao

From: Nitro Cao <jaycecao520@gmail.com>

git clone command would segment fault when satisfying the following
conditions at the same time:
  - Use HTTP protocol v0 or v1 to interact with remote servers.
  - The value of `--revision` doesn't specify the peer reference, like
    `--revision master` instead of `--revision refs/heads/master:master`

When using protocol v2, git client can use `ref-prefix` param of
`ls-refs` command to fetch wanted references based on `--revision`.
But for protocol v0/v1, git client just fetch all references and
doesn't filter them.
In this case, the value of `remote_head` variable is not NULL,
which leads to the value of `remote_head_points_at` not NULL too.
But we don't specify the peer reference in `--revsion`,
`remote_head_points_at->peer_ref` would be NULL. So git client would
boom when `update_remote_refs`.

Signed-off-by: Nitro Cao <jaycecao520@gmail.com>
---
    fix(clone): segment fault when using --revision and protocol v0/v1
    
    git clone command would segment fault when satisfying the following
    conditions at the same time:
    
     * Use HTTP protocol v0 or v1 to interact with remote servers.
     * The value of --revision doesn't specify the peer reference, like
       --revision master instead of --revision refs/heads/master:master
    
    When using protocol v2, git client can use ref-prefix param of ls-refs
    command to fetch wanted references based on --revision. But for protocol
    v0/v1, git client just fetch all references and doesn't filter them. In
    this case, the value of remote_head variable is not NULL, which leads to
    the value of remote_head_points_at not NULL too. But we don't specify
    the peer reference in --revsion, remote_head_points_at->peer_ref would
    be NULL. So git client would boom when update_remote_refs.

Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-2185%2FNitroCao%2Ffix%2Fsegment-fault-with-revision-param-v1
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-2185/NitroCao/fix/segment-fault-with-revision-param-v1
Pull-Request: https://github.com/git/git/pull/2185

 builtin/clone.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/builtin/clone.c b/builtin/clone.c
index b40cee5968..ba8de92563 100644
--- a/builtin/clone.c
+++ b/builtin/clone.c
@@ -558,7 +558,7 @@ static void update_remote_refs(const struct ref *refs,
 			write_followtags(refs, msg);
 	}
 
-	if (remote_head_points_at && !option_bare) {
+	if (remote_head_points_at && remote_head_points_at->peer_ref && !option_bare) {
 		struct strbuf head_ref = STRBUF_INIT;
 		strbuf_addstr(&head_ref, branch_top);
 		strbuf_addstr(&head_ref, "HEAD");

base-commit: 22584464849815268419fd9d2eba307362360db1
-- 
gitgitgadget

^ permalink raw reply related	[flat|nested] 5+ messages in thread
* Re: [PATCH v2] clone: fix segfault when using --revision and v0/v1 protocol
@ 2026-02-08 14:09 Jayce Cao
  0 siblings, 0 replies; 5+ messages in thread
From: Jayce Cao @ 2026-02-08 14:09 UTC (permalink / raw)
  To: gitster; +Cc: git, gitgitgadget, Jayce Cao

Hi Junio and thanks for your time!

> While your change may skip the code that segfaults, wouldn't it also
> stop noticing a broken case where .peer_ref should have been set but
> didn't, even when --revision=<rev> parameter is not used in the
> command invocation?  IOW, it is better to segfault and draw attention
> by Git developers when a valid input is given by the end user and our
> code misbehaves (e.g., and fails to to set .peer_ref as it should).

I totally agree with you.


> Wouldn't the correct fix be more like the following?
>
> - split out parts from update_remote_refs() that are needed even in
>   option_rev mode into a separate helper function, and call that
>   from cmd_clone().
>
> - make the call to update_remote_refs() conditional---specifically,
>   we shouldn't be calling it when option_rev is in effect.

Do we have another fix to make a conditional call to `find_ref_by_name()`
when `option_rev` is in effect? Because from the doc of `--revision`,
"... and detach `HEAD` to_<rev>_. ...", that means we don't need to
know where the real HEAD points to?

> Also, isn't this something we can specify the expected behaviour in
> tests?  Not only we want to ensure that nothing segfaults, we would
> want to make sure that the resulting repository has no refs and HEAD
> is detached at the specified revision.

I'll add several tests to cover the bug after we make sure how to fix it.

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2026-02-08 15:25 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-02-01  9:23 [PATCH] fix(clone): segment fault when using --revision and protocol v0/v1 NitroCao via GitGitGadget
2026-02-03 11:56 ` [PATCH v2] clone: fix segfault when using --revision and v0/v1 protocol NitroCao via GitGitGadget
2026-02-03 19:26   ` Junio C Hamano
2026-02-08 15:25     ` Nitro Cao
  -- strict thread matches above, loose matches on Subject: below --
2026-02-08 14:09 [PATCH v2] clone: fix segfault when using --revision and v0/v1 protocol Jayce Cao

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox