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

[PATCH 3/4] ref-filter: simplify rstrip_ref_components() memory handling

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

We're stripping path components from the end of a string, which we do by assigning a NUL as we parse each component, shortening the string. This requires an extra temporary buffer to avoid munging our input string.

But the way that we allocate the buffer is unusual. We have an extra "to_free" variable. Usually this is used when the access variable is conceptually const, like:

   const char *foo;
   char *to_free = NULL;
   if (...)
           foo = to_free = xstrdup(...);
   else
           foo = some_const_string;
   ...
   free(to_free);

But that's not what's happening here. Our "start" variable always points to the allocated buffer, and to_free is redundant. Worse, it is marked as const itself, requiring a cast when we free it.

Let's drop to_free entirely, and mark "start" as non-const, making the memory handling more clear. As a bonus, this also silences a warning from glibc-2.43 that our call to strrchr() implicitly strips away the const-ness of "start".

Signed-off-by: Jeff King <peff@peff.net>
---
 ref-filter.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
diff --git a/ref-filter.c b/ref-filter.c
index eb09fda21b..1008b2fd5a 100644
--- a/ref-filter.c
+++ b/ref-filter.c
@@ -2213,13 +2213,12 @@ static const char *lstrip_ref_components(const char *refname, int len)
 static const char *rstrip_ref_components(const char *refname, int len)
 {
 	int remaining = normalize_component_count(refname, len);
-	const char *start = xstrdup(refname);
-	const char *to_free = start;
+	char *start = xstrdup(refname);
 
 	while (remaining-- > 0) {
 		char *p = strrchr(start, '/');
 		if (!p) {
-			free((char *)to_free);
+			free(start);
 			return xstrdup("");
 		} else
 			p[0] = '\0';
-- 
2.53.0.438.gad17e1cd28
Previous: Jeff KingNext: Jeff King
Message 10 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.