Git development
 help / color / mirror / Atom feed
From: Adrian Friedli <adrian.friedli@mt.com>
To: adrian.friedli@mt.com
Cc: git@vger.kernel.org, gitster@pobox.com
Subject: Re: [PATCH] builtin/clone: fix segfault when using --revision on some servers
Date: Thu,  9 Apr 2026 15:09:04 +0200	[thread overview]
Message-ID: <20260409130904.1811-1-adrian.friedli@mt.com> (raw)
In-Reply-To: <DB8SPR01MB0009F61C462C819CDFF80ED7EA44A@DB8SPR01MB0009.eurprd03.prod.outlook.com>

> > > In the bad case the server ignores
> > > `transport_ls_refs_options.ref_prefixes` and 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.
> >
> > The description makes it sound more like this code is perfectly fine, and the
> > problem is in guess_remote_head() that reads the refs list and includes such a
> > bogus thing with no peer_ref in the result of its guessing.  There are 4 direct
> > callers to guess_remote_head() including this one---wouldn't they also obtain
> > a list with such a ref entry?
> 
> I traced the 3 other callers to guess_remote_head() and none of them has a
> problem if peer_ref is NULL. In get_expanded_map() there is even a condition
> `if (cpy->peer_ref)`, which indicates peer_ref is allowed to be NULL.

The details of the 4 callers to guess_remote_head()

builtin/fetch.c:set_head()
builtin/remote.c:get_head_names()

  refs from guess_remote_head() are only used with ref->name

builtin/clone.c:wanted_peer_refs()

  Does not use REMOTE_GUESS_HEAD_ALL flag in guess_remote_head() call. So only one ref is returned.

  calls get_fetch_map() with refs from guess_remote_head().
    may call get_expanded_map()
      safe if peer_ref is NULL, has a guard on the copy of ref `if (cpy->peer_ref)`.
    may call get_remote_ref()
      calls get_remote_ref()
        calls find_ref_by_name_abbrev()
	  only looks at name and next

builtin/clone.c:cmd_clone()

  Does not use REMOTE_GUESS_HEAD_ALL flag in guess_remote_head() call. So only one ref is returned.

  may call write_refspec_config() with refs from guess_remote_head()
    only uses name from refs

  calls update_remote_refs() with refs from guess_remote_head() 
  -> thats the failing one, no other uses

  may call update_head() with refs from guess_remote_head()
    only uses name and old_oid from refs

Kind regards
Adrian

      reply	other threads:[~2026-04-09 13:09 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-03-06 11:10 [PATCH] builtin/clone: fix segfault when using --revision on some servers Adrian Friedli
2026-03-06 19:51 ` Junio C Hamano
2026-03-12 12:34   ` Friedli Adrian LCPF-CH
2026-04-09 13:09     ` Adrian Friedli [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260409130904.1811-1-adrian.friedli@mt.com \
    --to=adrian.friedli@mt.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox