git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH] remote-curl: set fallback hash algorithm outside repo

From
K Jayatheerth <jayatheerthkulkarni2005@gmail.com>
Date
Mar 21, 2026, 19:46 UTC
Message-ID
<20260321194653.24513-1-jayatheerthkulkarni2005@gmail.com>
In-Reply-To
<CAN=xy38zCRdOAnMtBXtRyUHE=+gtS8J6mwUWFQqxDAaBLAm7dA@mail.gmail.com>

When a remote helper like git-remote-http is invoked outside of a repository (for example, by running `git ls-remote -h <url>` in a non-git directory), setup_git_directory_gently() leaves the_repository->hash_algo as NULL.

If the user has a global fetch refspec configured, remote-curl attempts to parse this refspec 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.

Fix this by mirroring the behavior of Git's main built-ins. If remote-curl is operating outside a repository, initialize a fallback hash algorithm (SHA-1) so that refspec parsing can safely check hexadecimal lengths.

Also add a test in t5551 to ensure this regression does not happen again. The test uses GIT_CEILING_DIRECTORIES to ensure the command genuinely runs in a nongit environment without falling back to the test suite's trash directory repository.

Reported-by: Jo Liss <joliss@gmail.com>
Signed-off-by: K Jayatheerth <jayatheerthkulkarni2005@gmail.com>
---
While the fix was tricky to find
I believe it is a small one.
The debug Jo did here helped me find it faster than I would've.
I hope the test I added is in the right file, I had multiple options, but
looking at other test files this seemed a fair option.
 remote-curl.c               |  4 ++++
 t/t5551-http-fetch-smart.sh | 15 +++++++++++++++
 2 files changed, 19 insertions(+)
diff --git a/remote-curl.c b/remote-curl.c
index 92e40bb682..4c85e6b079 100644
--- a/remote-curl.c
+++ b/remote-curl.c
@@ -1547,6 +1547,10 @@ int cmd_main(int argc, const char **argv)
 	int ret = 1;
 
 	setup_git_directory_gently(&nongit);
+
+	if (nongit && !the_repository->hash_algo)
+		repo_set_hash_algo(the_repository, GIT_HASH_SHA1);
+
 	if (argc < 2) {
 		error(_("remote-curl: usage: git remote-curl <remote> [<url>]"));
 		goto cleanup;
diff --git a/t/t5551-http-fetch-smart.sh b/t/t5551-http-fetch-smart.sh
index 73cf531580..ed81e6b49b 100755
--- a/t/t5551-http-fetch-smart.sh
+++ b/t/t5551-http-fetch-smart.sh
@@ -782,4 +782,19 @@ test_expect_success 'tag following always works over v0 http' '
 	test_cmp expect actual
 '
 
+test_expect_success 'ls-remote outside repo does not segfault with fetch refspec' '
+	GIT_CEILING_DIRECTORIES=$(pwd) &&
+	export GIT_CEILING_DIRECTORIES &&
+	mkdir nongit &&
+	(
+		cd nongit &&
+		env GIT_CONFIG_NOSYSTEM=1 \
+			GIT_CONFIG_GLOBAL=/dev/null \
+			GIT_CONFIG_COUNT=1 \
+			GIT_CONFIG_KEY_0=remote.origin.fetch \
+			GIT_CONFIG_VALUE_0="+refs/tags/*:refs/tags/*" \
+			git ls-remote "$HTTPD_URL/smart/repo.git"
+	)
+'
+
 test_done
-- 
2.53.0
Previous: Jo LissNext: brian m. carlson
Message 2 of 17 in “remote-curl: segfault parsing remote.<name>.fetch outside a repository”
  1. Jo LissMar 21, 2026
  2. remote-curl: set fallback hash algorithm outside repoK Jayatheerth, Mar 21, 2026
  3. brian m. carlsonMar 21, 2026
  4. refspec: safely parse refspecs outside a repositoryK Jayatheerth, Mar 22, 2026
  5. Junio C HamanoMar 22, 2026
  6. Jeff KingMar 22, 2026
  7. 1/2 refspec: safely parse refspecs outside a repositoryK Jayatheerth, Mar 22, 2026
  8. 2/2 refspec: fix typo in commentK Jayatheerth, Mar 22, 2026
  9. Junio C HamanoMar 23, 2026
  10. Jeff KingMar 23, 2026
  11. Junio C HamanoMar 23, 2026
  12. 1/2 remote-curl: fall back to default hash outside repoK Jayatheerth, Mar 24, 2026
  13. 2/2 refspec: fix typo in commentK Jayatheerth, Mar 24, 2026
  14. Junio C HamanoMar 24, 2026
  15. Jeff KingMar 21, 2026
  16. Junio C HamanoMar 22, 2026
  17. Jeff KingMar 22, 2026

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.