From: Jeff King <peff@peff.net>
To: Junio C Hamano <gitster@pobox.com>
Cc: Michael J Gruber <git@grubix.eu>, git@vger.kernel.org
Subject: [PATCH] config: store allocated string in non-const pointer
Date: Thu, 26 Mar 2026 15:23:20 -0400 [thread overview]
Message-ID: <20260326192320.GA418281@coredump.intra.peff.net> (raw)
In-Reply-To: <xmqqy0jepjqy.fsf@gitster.g>
On Thu, Mar 26, 2026 at 10:45:41AM -0700, Junio C Hamano wrote:
> > But we can untangle this for the compiler without having to cast by
> > using a non-const alias, like:
> [...]
> Yeah, this is much clearer.
Here it is as a patch with a commit message. I was eventually planning
to do a complete series that cleans up all the warnings, and this would
be part of it. But since other people are starting to work on it, too,
it may make sense to just send them out as we have them to avoid too
much duplication.
-- >8 --
Subject: [PATCH] config: store allocated string in non-const pointer
When git-config matches a url, we copy the variable section name and
store it in the "section" member of a urlmatch_config struct. That
member is const, since the url-matcher will not touch it (and other
callers really will have a const string).
But that means that we have only a const pointer to our allocated
string. We have to cast away the constness when we free it, and likewise
when we assign NUL to tie off the "." separating the subsection and key.
This latter happens implicitly via a strchr() call, but recent versions
of glibc have added annotations that let the compiler detect that and
complain.
Let's keep our own "section" pointer for the non-const string, and then
just point config.section at it. That avoids all of the casting, both
explicit and implicit.
Signed-off-by: Jeff King <peff@peff.net>
---
builtin/config.c | 7 ++++---
1 file changed, 4 insertions(+), 3 deletions(-)
diff --git a/builtin/config.c b/builtin/config.c
index 7c4857be62..cf4ba0f7cc 100644
--- a/builtin/config.c
+++ b/builtin/config.c
@@ -838,6 +838,7 @@ static int get_urlmatch(const struct config_location_options *opts,
const char *var, const char *url)
{
int ret;
+ char *section;
char *section_tail;
struct config_display_options display_opts = *_display_opts;
struct string_list_item *item;
@@ -851,8 +852,8 @@ static int get_urlmatch(const struct config_location_options *opts,
if (!url_normalize(url, &config.url))
die("%s", config.url.err);
- config.section = xstrdup_tolower(var);
- section_tail = strchr(config.section, '.');
+ config.section = section = xstrdup_tolower(var);
+ section_tail = strchr(section, '.');
if (section_tail) {
*section_tail = '\0';
config.key = section_tail + 1;
@@ -886,7 +887,7 @@ static int get_urlmatch(const struct config_location_options *opts,
string_list_clear(&values, 1);
free(config.url.url);
- free((void *)config.section);
+ free(section);
return ret;
}
--
2.53.0.1081.gf77a8b8145
next prev parent reply other threads:[~2026-03-26 19:23 UTC|newest]
Thread overview: 24+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-03-26 15:22 [PATCH 0/6] ISOC23: quell warnings on discarding const Michael J Gruber
2026-03-26 15:22 ` [PATCH 1/6] do not discard const: the simple cases Michael J Gruber
2026-03-26 17:34 ` Jeff King
2026-03-26 17:45 ` Junio C Hamano
2026-03-26 19:23 ` Jeff King [this message]
2026-03-26 15:22 ` [PATCH 2/6] do not discard const: make git-compat-util ISOC23-like Michael J Gruber
2026-03-26 15:22 ` [PATCH 3/6] do not discard const: adjust to non-const data types Michael J Gruber
2026-03-26 17:28 ` Junio C Hamano
2026-03-26 15:22 ` [PATCH 4/6] do not discard const: declare const where we stay const Michael J Gruber
2026-03-26 15:22 ` [PATCH 5/6] do not discard const: keep signature Michael J Gruber
2026-03-26 17:18 ` Junio C Hamano
2026-03-26 15:22 ` [PATCH 6/6] do not discard const: the ugly truth Michael J Gruber
2026-03-26 17:07 ` Junio C Hamano
2026-03-26 17:42 ` Jeff King
2026-03-26 19:02 ` [PATCH 0/4] fix const issues in revision parser Jeff King
2026-03-26 19:04 ` [PATCH 1/4] revision: make handle_dotdot() interface less confusing Jeff King
2026-03-26 19:28 ` Junio C Hamano
2026-03-26 23:14 ` Jeff King
2026-03-27 15:55 ` Junio C Hamano
2026-03-26 19:05 ` [PATCH 2/4] rev-parse: simplify dotdot parsing Jeff King
2026-03-26 19:13 ` [PATCH 3/4] revision: avoid writing to const string for parent marks Jeff King
2026-03-26 19:14 ` [PATCH 4/4] rev-parse: " Jeff King
2026-03-26 16:26 ` [PATCH 0/6] ISOC23: quell warnings on discarding const D. Ben Knoble
2026-03-27 17:45 ` Michael J Gruber
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=20260326192320.GA418281@coredump.intra.peff.net \
--to=peff@peff.net \
--cc=git@grubix.eu \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
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