From: Jeff King <peff@peff•net>
To: Collin Funk <collin.funk1@gmail•com>
Cc: git@vger•kernel.org, Junio C Hamano <gitster@pobox•com>
Subject: [PATCH 3/4] ref-filter: simplify rstrip_ref_components() memory handling
Date: Sun, 15 Feb 2026 04:05:34 -0500 [thread overview]
Message-ID: <20260215090534.GC695631@coredump.intra.peff.net> (raw)
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
next prev parent reply other threads:[~2026-02-15 9:05 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-02-14 5:15 [PATCH] ref-filter: don't declare a strdup'd variable const before writing to it Collin Funk
2026-02-15 8:57 ` [PATCH 0/4] cleaning up ref-filter lstrip/rstrip code Jeff King
2026-02-15 9:00 ` [PATCH 1/4] ref-filter: factor out refname component counting Jeff King
2026-02-17 18:07 ` Junio C Hamano
2026-02-19 11:21 ` Jeff King
2026-02-19 18:56 ` Junio C Hamano
2026-02-20 6:00 ` [PATCH] ref-filter: clarify lstrip/rstrip " Jeff King
2026-02-22 17:04 ` [PATCH 1/4] ref-filter: factor out refname " Karthik Nayak
2026-02-15 9:02 ` [PATCH 2/4] ref-filter: simplify lstrip_ref_components() memory handling Jeff King
2026-02-15 9:05 ` Jeff King [this message]
2026-02-15 9:07 ` [PATCH 4/4] ref-filter: avoid strrchr() in rstrip_ref_components() Jeff King
2026-02-16 7:23 ` Patrick Steinhardt
2026-02-15 9:11 ` [PATCH 0/4] cleaning up ref-filter lstrip/rstrip code Jeff King
2026-02-15 22:23 ` Collin Funk
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260215090534.GC695631@coredump.intra.peff.net \
--to=peff@peff$(echo .)net \
--cc=collin.funk1@gmail$(echo .)com \
--cc=git@vger$(echo .)kernel.org \
--cc=gitster@pobox$(echo .)com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox