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

[PATCH v2] refspec: safely parse refspecs outside a repository

From
K Jayatheerth <jayatheerthkulkarni2005@gmail.com>
Date
Mar 22, 2026, 02:35 UTC
Message-ID
<20260322023557.15907-1-jayatheerthkulkarni2005@gmail.com>
In-Reply-To
<20260321194653.24513-1-jayatheerthkulkarni2005@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), `the_hash_algo` is left uninitialized (NULL).

If a user has a global fetch refspec configured, `parse_refspec()` attempts to check if 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. This crash occurs for both standard and negative refspecs.

Fix this by ensuring `the_hash_algo` is non-NULL before checking `the_hash_algo->hexsz` for both standard and negative refspecs. When operating outside a repository, fetching is impossible, so bypassing the exact OID check is the cleanest approach.

Additionally, while looking into the remote-curl execution path, take the opportunity to remove an unused `#include "git-curl-compat.h"` from `remote-curl.c`.

Reported-by: Jo Liss <joliss@gmail.com>
Helped-by: Jeff King <peff@peff.net>
Signed-off-by: K Jayatheerth <jayatheerthkulkarni2005@gmail.com>
---
Changes in v2:
Instead of adding a fix by giving a default hash like we did in cmd_apply()
I understood that it is impossible to fetch here.
Therefore I picked up whatever Peff suggested here.
Since I got no feedback on the test, I am assuming it is correct and leaving as is.
 refspec.c                   |  4 ++--
 remote-curl.c               |  1 -
 t/t5551-http-fetch-smart.sh | 15 +++++++++++++++
 3 files changed, 17 insertions(+), 3 deletions(-)
diff --git a/refspec.c b/refspec.c
index 0775358d96..a864a0bac2 100644
--- a/refspec.c
+++ b/refspec.c
@@ -84,7 +84,7 @@ static int parse_refspec(struct refspec_item *item, const char *refspec, int fet
 		 */
 		if (!*item->src)
 			return 0; /* negative refspecs must not be empty */
-		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))
 			return 0; /* negative refpsecs cannot be exact sha1 */
 		else if (!check_refname_format(item->src, flags))
 			; /* valid looking ref is ok */
@@ -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 */
diff --git a/remote-curl.c b/remote-curl.c
index 92e40bb682..280880e54e 100644
--- a/remote-curl.c
+++ b/remote-curl.c
@@ -2,7 +2,6 @@
 #define DISABLE_SIGN_COMPARE_WARNINGS
 
 #include "git-compat-util.h"
-#include "git-curl-compat.h"
 #include "config.h"
 #include "environment.h"
 #include "gettext.h"
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: brian m. carlsonNext: Junio C Hamano
Message 4 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.