Subject: Re: remote-curl: segfault parsing remote.<name>.fetch outside a repository

From: Junio C Hamano <[email-masked]>
Date: Sat, 21 Mar 2026 18:20:16 -0700

Jeff King <[email-masked]> writes:

> It's a curious case, though. The crashing code is parse_refspec() does
> this:
>
>   if (llen == the_hash_algo->hexsz && !get_oid_hex(item->src, &unused))
>         item->exact_sha1 = 1; /* ok */
>
> But what is the correct hash algo to use here when we are outside a
> repository?

Hmph, who is calling into the transport outside a repository in the
first place?  Even "git clone" should create the receiving
repository before it calls into the transport, no?  Is this "git
ls-remote" or something?

> In this particular case, the origin refspecs are not even going to be
> used, but you can construct a similar one where they are:
>
>   git -C / \
>       -c remote.foo.url=https://github.com/git/git \
>       -c remote.foo.fetch=whatever \
>       ls-remote foo

OK.

> We could do this:
>
> diff --git a/refspec.c b/refspec.c
> index 0775358d96..e6c29b7dd0 100644
> --- a/refspec.c
> +++ b/refspec.c
> @@ -101,7 +101,7 @@ static int parse_refspec(struct refspec_item *item, const char *refspec, int fet
>  		/* LHS */
>  		if (!*item->src)
>  			; /* empty is ok; it means "HEAD" */
> -		else if (llen == the_hash_algo->hexsz && !get_oid_hex(item->src, &unused))
> +		else if (the_hash_algo && llen == the_hash_algo->hexsz && !get_oid_hex(item->src, &unused))
>  			item->exact_sha1 = 1; /* ok */
>  		else if (!check_refname_format(item->src, flags))
>  			; /* valid looking ref is ok */
>
> to make the segfault go away, but it is mostly papering over the
> problem. I'm not sure if the exact_sha1 flag would matter when we are
> not actually fetching (and we cannot fetch when we are not in a local
> repo). Grepping around, it looks like it does influence the ref prefixes
> we send to the other side (yet another chicken-and-egg!).

Yup, I agree with your assessment that exact_sha1 should mostly be
garbage if we do not have a repository in the first place.