Skip to content

Commit d385845

Browse files
peffgitster
authored andcommitted
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> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 213b213 commit d385845

1 file changed

Lines changed: 4 additions & 3 deletions

File tree

builtin/config.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -838,6 +838,7 @@ static int get_urlmatch(const struct config_location_options *opts,
838838
const char *var, const char *url)
839839
{
840840
int ret;
841+
char *section;
841842
char *section_tail;
842843
struct config_display_options display_opts = *_display_opts;
843844
struct string_list_item *item;
@@ -851,8 +852,8 @@ static int get_urlmatch(const struct config_location_options *opts,
851852
if (!url_normalize(url, &config.url))
852853
die("%s", config.url.err);
853854

854-
config.section = xstrdup_tolower(var);
855-
section_tail = strchr(config.section, '.');
855+
config.section = section = xstrdup_tolower(var);
856+
section_tail = strchr(section, '.');
856857
if (section_tail) {
857858
*section_tail = '\0';
858859
config.key = section_tail + 1;
@@ -886,7 +887,7 @@ static int get_urlmatch(const struct config_location_options *opts,
886887
string_list_clear(&values, 1);
887888
free(config.url.url);
888889

889-
free((void *)config.section);
890+
free(section);
890891
return ret;
891892
}
892893

0 commit comments

Comments
 (0)