]> git.ipfire.org Git - thirdparty/git.git/commitdiff
config: store allocated string in non-const pointer
authorJeff King <peff@peff.net>
Thu, 26 Mar 2026 19:23:20 +0000 (15:23 -0400)
committerJunio C Hamano <gitster@pobox.com>
Thu, 26 Mar 2026 19:47:17 +0000 (12:47 -0700)
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>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/config.c

index 7c4857be6229047cf0f4b8499de9a955077a18cd..cf4ba0f7cc6f228b16c1f131619837f7a8d441c0 100644 (file)
@@ -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;
 }