Skip to content

Commit 649c768

Browse files
jayatheerthkulkarnigitster
authored andcommitted
remote-curl: fall back to default hash outside repo
When a remote helper like git-remote-http is invoked outside of a repository (for example, by running git ls-remote in a non-git directory), setup_git_directory_gently() leaves the_hash_algo uninitialized as NULL. If the user has a globally configured fetch refspec, remote-curl attempts to parse it during initialization. Inside parse_refspec(), it checks whether the LHS of the refspec is an exact OID by evaluating llen == the_hash_algo->hexsz. Because the_hash_algo is NULL, this results in a segmentation fault. In 9e89dcb (builtin/ls-remote: fall back to SHA1 outside of a repo, 2024-08-02), we added a workaround to ls-remote to fall back to the default hash algorithm to prevent exactly this type of crash when parsing refspec capabilities. However, because remote-curl runs as a separate process, it does not inherit that fallback and crashes anyway. Instead of pushing a NULL-guard workaround down into parse_refspec(), fix this by mirroring the ls-remote workaround directly in remote-curl.c. If we are operating outside a repository, initialize the_hash_algo to GIT_HASH_DEFAULT. This keeps the HTTP transport consistent with non-HTTP transports that execute in-process, preventing crashes without altering the generic refspec parsing logic. Reported-by: Jo Liss <joliss@gmail.com> Helped-by: Jeff King <peff@peff.net> Signed-off-by: K Jayatheerth <jayatheerthkulkarni2005@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 67ad421 commit 649c768

2 files changed

Lines changed: 14 additions & 0 deletions

File tree

remote-curl.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1551,6 +1551,13 @@ int cmd_main(int argc, const char **argv)
15511551
goto cleanup;
15521552
}
15531553

1554+
/*
1555+
* yuck, see 9e89dcb66a (builtin/ls-remote: fall back to SHA1 outside
1556+
* of a repo, 2024-08-02)
1557+
*/
1558+
if (nongit)
1559+
repo_set_hash_algo(the_repository, GIT_HASH_DEFAULT);
1560+
15541561
options.verbosity = 1;
15551562
options.progress = !!isatty(2);
15561563
options.thin = 1;

t/t5551-http-fetch-smart.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -782,4 +782,11 @@ test_expect_success 'tag following always works over v0 http' '
782782
test_cmp expect actual
783783
'
784784

785+
test_expect_success 'ls-remote outside repo does not segfault with fetch refspec' '
786+
nongit git \
787+
-c remote.origin.url="$HTTPD_URL/smart/repo.git" \
788+
-c remote.origin.fetch=anything \
789+
ls-remote origin
790+
'
791+
785792
test_done

0 commit comments

Comments
 (0)