git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 5/5] prepare_repo_settings(): plug leak of config values

From
Jeff King <peff@peff.net>
Date
Sep 8, 2022, 05:02 UTC
Message-ID
<Yxl3elDV2kpAfAvU@coredump.intra.peff.net>
In-Reply-To
<Yxl1BNQoy6Drf0Oe@coredump.intra.peff.net>

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>
---
 repo-settings.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
diff --git a/repo-settings.c b/repo-settings.c
index 43bc881bfc..e8b58151bc 100644
--- a/repo-settings.c
+++ b/repo-settings.c
@@ -22,7 +22,7 @@ void prepare_repo_settings(struct repository *r)
 {
 	int experimental;
 	int value;
-	char *strval;
+	const char *strval;
 	int manyfiles;
 
 	if (!r->gitdir)
@@ -77,7 +77,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);
 
 		/*
@@ -88,10 +88,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;
-- 
2.37.3.1139.g47294c03c7
Previous: Jeff KingNext: Derrick Stolee
Message 6 of 8 in “plugging some list-objects-filter leaks”
  1. 0/5 plugging some list-objects-filter leaksJeff King, Sep 8, 2022
  2. 1/5 list_objects_filter_copy(): deep-copy sparse_oid_name fieldJeff King, Sep 8, 2022
  3. 2/5 transport: deep-copy object-filter struct for fetch-packJeff King, Sep 8, 2022
  4. 3/5 transport: free filter options in disconnect_git()Jeff King, Sep 8, 2022
  5. 4/5 list_objects_filter_options: plug leak of filter_spec stringsJeff King, Sep 8, 2022
  6. 5/5 prepare_repo_settings(): plug leak of config valuesJeff King, Sep 8, 2022
  7. Derrick StoleeSep 9, 2022
  8. Jeff KingSep 11, 2022

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.