]> git.ipfire.org Git - thirdparty/git.git/commitdiff
prepare_repo_settings(): plug leak of config values
authorJeff King <peff@peff.net>
Thu, 8 Sep 2022 05:02:50 +0000 (01:02 -0400)
committerJunio C Hamano <gitster@pobox.com>
Thu, 8 Sep 2022 18:11:28 +0000 (11:11 -0700)
We call repo_config_get_string() for fetch.negotiationAlgorithm, which
allocates a copy of the string, but we never free it.

We could add a call to free(), but there's an even simpler solution: we
don't need the string to persist beyond a few strcasecmp() calls, so we
can instead use the "_tmp" variant which gives us a const pointer to the
cached value.

We need to switch the type of "strval" to "const char *" for this to
work, which affects a similar call that checks core.untrackedCache. But
it's in the same boat! It doesn't actually need the value to persist
beyond a maybe_bool() check (though it does remember to correctly free
the string afterwards). So we can simplify it at the same time.

Note that this core.untrackedCache check arguably should be using
repo_config_get_maybe_bool(), but there are some subtle behavior
changes. E.g., it doesn't currently allow a value-less "true". Arguably
it should, but let's avoid lumping further changes in what should be a
simple leak cleanup.

Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
repo-settings.c

index 2dfcb2b6542f2cbe75d4575e8b707cf1676679a3..f220c16f841639de5d938c06d034e399b1c86fe2 100644 (file)
@@ -15,7 +15,7 @@ void prepare_repo_settings(struct repository *r)
 {
        int experimental;
        int value;
-       char *strval;
+       const char *strval;
        int manyfiles;
 
        if (!r->gitdir)
@@ -67,7 +67,7 @@ void prepare_repo_settings(struct repository *r)
        if (!repo_config_get_int(r, "index.version", &value))
                r->settings.index_version = value;
 
-       if (!repo_config_get_string(r, "core.untrackedcache", &strval)) {
+       if (!repo_config_get_string_tmp(r, "core.untrackedcache", &strval)) {
                int v = git_parse_maybe_bool(strval);
 
                /*
@@ -78,10 +78,9 @@ void prepare_repo_settings(struct repository *r)
                if (v >= 0)
                        r->settings.core_untracked_cache = v ?
                                UNTRACKED_CACHE_WRITE : UNTRACKED_CACHE_REMOVE;
-               free(strval);
        }
 
-       if (!repo_config_get_string(r, "fetch.negotiationalgorithm", &strval)) {
+       if (!repo_config_get_string_tmp(r, "fetch.negotiationalgorithm", &strval)) {
                int fetch_default = r->settings.fetch_negotiation_algorithm;
                if (!strcasecmp(strval, "skipping"))
                        r->settings.fetch_negotiation_algorithm = FETCH_NEGOTIATION_SKIPPING;