]> git.ipfire.org Git - thirdparty/git.git/commitdiff
ref-filter: simplify rstrip_ref_components() memory handling
authorJeff King <peff@peff.net>
Sun, 15 Feb 2026 09:05:34 +0000 (04:05 -0500)
committerJunio C Hamano <gitster@pobox.com>
Tue, 17 Feb 2026 17:45:29 +0000 (09:45 -0800)
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>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
ref-filter.c

index f5f0cb4ad6963274bf714d482029c78250c525af..9589418c256840a4a2aefdfa0bb410875ba6cd55 100644 (file)
@@ -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';