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

[PATCH 2/4] ref-filter: simplify lstrip_ref_components() memory handling

From
Jeff King <peff@peff.net>
Date
Feb 15, 2026, 09:02 UTC
Message-ID
<20260215090223.GB695631@coredump.intra.peff.net>
In-Reply-To
<20260215085755.GA86262@coredump.intra.peff.net>

We're walking forward in the string, skipping path components from left-to-right. So when we've stripped as much as we want, the pointer we have is a complete NUL-terminated string and we can just return it (after duplicating it, of course). So there is no need for a temporary allocated string.

But we do make an extra temporary copy due to f0062d3b74 (ref-filter: free item->value and item->value->s, 2018-10-18). This is probably from cargo-culting the technique used in rstrip_ref_components(), which _does_ need a separate string (since it is stripping from the end and ties off the temporary string with a NUL).

Let's drop the extra allocation. This is slightly more efficient, but more importantly makes the code much simpler.

Signed-off-by: Jeff King <peff@peff.net>
---
 ref-filter.c | 9 ++-------
 1 file changed, 2 insertions(+), 7 deletions(-)
diff --git a/ref-filter.c b/ref-filter.c
index 9153331f42..eb09fda21b 100644
--- a/ref-filter.c
+++ b/ref-filter.c
@@ -2196,23 +2196,18 @@ static int normalize_component_count(const char *refname, int len)
 static const char *lstrip_ref_components(const char *refname, int len)
 {
 	int remaining = normalize_component_count(refname, len);
-	const char *start = xstrdup(refname);
-	const char *to_free = start;
 
 	while (remaining > 0) {
-		switch (*start++) {
+		switch (*refname++) {
 		case '\0':
-			free((char *)to_free);
 			return xstrdup("");
 		case '/':
 			remaining--;
 			break;
 		}
 	}
 
-	start = xstrdup(start);
-	free((char *)to_free);
-	return start;
+	return xstrdup(refname);
 }
 
 static const char *rstrip_ref_components(const char *refname, int len)
-- 
2.53.0.438.gad17e1cd28
Previous: Karthik NayakNext: Jeff King
Message 9 of 14 in “ref-filter: don't declare a strdup'd variable const before writing to it”
  1. ref-filter: don't declare a strdup'd variable const before writing to itCollin Funk, Feb 14, 2026
  2. 0/4 cleaning up ref-filter lstrip/rstrip codeJeff King, Feb 15, 2026
  3. 1/4 ref-filter: factor out refname component countingJeff King, Feb 15, 2026
  4. Junio C HamanoFeb 17, 2026
  5. Jeff KingFeb 19, 2026
  6. Junio C HamanoFeb 19, 2026
  7. ref-filter: clarify lstrip/rstrip component countingJeff King, Feb 20, 2026
  8. Karthik NayakFeb 22, 2026
  9. 2/4 ref-filter: simplify lstrip_ref_components() memory handlingJeff King, Feb 15, 2026
  10. 3/4 ref-filter: simplify rstrip_ref_components() memory handlingJeff King, Feb 15, 2026
  11. 4/4 ref-filter: avoid strrchr() in rstrip_ref_components()Jeff King, Feb 15, 2026
  12. Patrick SteinhardtFeb 16, 2026
  13. Jeff KingFeb 15, 2026
  14. Collin FunkFeb 15, 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.