]> git.ipfire.org Git - thirdparty/git.git/commitdiff
builtin/clone: fix segfault when using --revision with protocol v0
authorAdrian Friedli <adrian.friedli@mt.com>
Fri, 24 Jul 2026 12:41:38 +0000 (14:41 +0200)
committerJunio C Hamano <gitster@pobox.com>
Fri, 24 Jul 2026 16:38:06 +0000 (09:38 -0700)
Servers supporting protocol v2 do not advertise excess refs and honor
`transport_ls_refs_options.ref_prefixes` when

    $ git clone --revision=refs/heads/main $URL

contacts them, but when talking to a server that does not support
protocol v2 the client segfaults. This can also be observed when v0 is
enforced for example by

    $ git -c protocol.version=0 clone --revision=refs/heads/main $URL

In the protocol v2 case the server honors
`transport_ls_refs_options.ref_prefixes` and in `cmd_clone()` the linked
list `refs` returned by `transport_get_remote_refs()` only contains a
single item, which is the ref requested with the --revision argument.
Both `remote_head` returned by `find_ref_by_name()` and
`remote_head_points_at` returned by `guess_remote_head()` are NULL. The
guard in `update_remote_refs()` skips a the affected code because
`remote_head_points_at` is NULL.

In the protocol v0 case in `cmd_clone()` the linked list `refs` returned
by `transport_get_remote_refs()` contains many items, amongst others
"HEAD". `remote_head` returned by `find_ref_by_name()` is not NULL and
`remote_head_points_at` returned by `guess_remote_head()` is not NULL
but its field `peer_ref` is NULL. Because `remote_head_points_at` is not
NULL the guard in `update_remote_refs()` does not skip the affected code
and `remote_head_points_at->peer_ref->name` is accessed, which causes a
segfault later on.

Signed-off-by: Adrian Friedli <adrian.friedli@mt.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/clone.c
t/t5621-clone-revision.sh

index d60d1b60bc238cc9f6dd0a8418ba83107b7e944c..5cbebbf7c230582f9a1f9ab4993191aadc761303 100644 (file)
@@ -557,7 +557,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");
index db3b8cff558f0bdb1c43a8544966a06cf364e136..54789423f8cccc921122a313d645e6adcb66cba7 100755 (executable)
@@ -90,6 +90,14 @@ test_expect_success 'clone with --revision and --bare' '
        test_must_fail git -C dst config remote.origin.fetch
 '
 
+test_expect_success 'clone with --revision and protocol v0' '
+       test_when_finished "rm -rf dst" &&
+       git -c protocol.version=0 clone --no-local --revision=refs/heads/main . dst &&
+       git rev-parse refs/heads/main >expect &&
+       git -C dst rev-parse HEAD >actual &&
+       test_cmp expect actual
+'
+
 test_expect_success 'clone with --revision being a short raw commit hash' '
        test_when_finished "rm -rf dst" &&
        oid=$(git rev-parse --short refs/heads/feature) &&