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

[PATCH v2 15/20] remote: fix leaking config strings

From
Patrick Steinhardt <ps@pks.im>
Date
Aug 22, 2024, 09:17 UTC
Message-ID
<532328b78140620549385c7e2dff3ecc98036593.1724315484.git.ps@pks.im>
In-Reply-To
<cover.1724315484.git.ps@pks.im>

We're leaking several config strings when assembling remotes, either because we do not free preceding values in case a config was set multiple times, or because we do not free them when releasing the remote state. This includes config strings for "branch" sections, "insteadOf", "pushInsteadOf", and "pushDefault".

Plug those leaks.
Signed-off-by: Patrick Steinhardt <ps@pks.im>
---
 remote.c | 40 ++++++++++++++++++++++++++++++++++++++--
 1 file changed, 38 insertions(+), 2 deletions(-)
diff --git a/remote.c b/remote.c
index 7d5b8f750d8..2c52119bbb2 100644
--- a/remote.c
+++ b/remote.c
@@ -243,6 +243,17 @@ static struct branch *make_branch(struct remote_state *remote_state,
 	return ret;
 }
 
+static void branch_release(struct branch *branch)
+{
+	free((char *)branch->name);
+	free((char *)branch->refname);
+	free(branch->remote_name);
+	free(branch->pushremote_name);
+	for (int i = 0; i < branch->merge_nr; i++)
+		refspec_item_clear(branch->merge[i]);
+	free(branch->merge);
+}
+
 static struct rewrite *make_rewrite(struct rewrites *r,
 				    const char *base, size_t len)
 {
@@ -263,6 +274,14 @@ static struct rewrite *make_rewrite(struct rewrites *r,
 	return ret;
 }
 
+static void rewrites_release(struct rewrites *r)
+{
+	for (int i = 0; i < r->rewrite_nr; i++)
+		free((char *)r->rewrite[i]->base);
+	free(r->rewrite);
+	memset(r, 0, sizeof(*r));
+}
+
 static void add_instead_of(struct rewrite *rewrite, const char *instead_of)
 {
 	ALLOC_GROW(rewrite->instead_of, rewrite->instead_of_nr + 1, rewrite->instead_of_alloc);
@@ -373,8 +392,10 @@ static int handle_config(const char *key, const char *value,
 			return -1;
 		branch = make_branch(remote_state, name, namelen);
 		if (!strcmp(subkey, "remote")) {
+			FREE_AND_NULL(branch->remote_name);
 			return git_config_string(&branch->remote_name, key, value);
 		} else if (!strcmp(subkey, "pushremote")) {
+			FREE_AND_NULL(branch->pushremote_name);
 			return git_config_string(&branch->pushremote_name, key, value);
 		} else if (!strcmp(subkey, "merge")) {
 			if (!value)
@@ -406,9 +427,11 @@ static int handle_config(const char *key, const char *value,
 		return 0;
 
 	/* Handle remote.* variables */
-	if (!name && !strcmp(subkey, "pushdefault"))
+	if (!name && !strcmp(subkey, "pushdefault")) {
+		FREE_AND_NULL(remote_state->pushremote_name);
 		return git_config_string(&remote_state->pushremote_name, key,
 					 value);
+	}
 
 	if (!name)
 		return 0;
@@ -475,12 +498,15 @@ static int handle_config(const char *key, const char *value,
 		else if (!strcmp(value, "--tags"))
 			remote->fetch_tags = 2;
 	} else if (!strcmp(subkey, "proxy")) {
+		FREE_AND_NULL(remote->http_proxy);
 		return git_config_string(&remote->http_proxy,
 					 key, value);
 	} else if (!strcmp(subkey, "proxyauthmethod")) {
+		FREE_AND_NULL(remote->http_proxy_authmethod);
 		return git_config_string(&remote->http_proxy_authmethod,
 					 key, value);
 	} else if (!strcmp(subkey, "vcs")) {
+		FREE_AND_NULL(remote->foreign_vcs);
 		return git_config_string(&remote->foreign_vcs, key, value);
 	}
 	return 0;
@@ -2797,16 +2823,26 @@ struct remote_state *remote_state_new(void)
 
 void remote_state_clear(struct remote_state *remote_state)
 {
+	struct hashmap_iter iter;
+	struct branch *b;
 	int i;
 
 	for (i = 0; i < remote_state->remotes_nr; i++)
 		remote_clear(remote_state->remotes[i]);
 	FREE_AND_NULL(remote_state->remotes);
+	FREE_AND_NULL(remote_state->pushremote_name);
 	remote_state->remotes_alloc = 0;
 	remote_state->remotes_nr = 0;
 
+	rewrites_release(&remote_state->rewrites);
+	rewrites_release(&remote_state->rewrites_push);
+
 	hashmap_clear_and_free(&remote_state->remotes_hash, struct remote, ent);
-	hashmap_clear_and_free(&remote_state->branches_hash, struct remote, ent);
+	hashmap_for_each_entry(&remote_state->branches_hash, &iter, b, ent) {
+		branch_release(b);
+		free(b);
+	}
+	hashmap_clear(&remote_state->branches_hash);
 }
 
 /*
-- 
2.46.0.164.g477ce5ccd6.dirty
Previous: Patrick SteinhardtNext: Patrick Steinhardt
Message 53 of 60 in “Memory leak fixes (pt.5)”
  1. 00/20 Memory leak fixes (pt.5)Patrick Steinhardt, Aug 20, 2024
  2. 01/20 mailinfo: fix leaking header dataPatrick Steinhardt, Aug 20, 2024
  3. Junio C HamanoAug 20, 2024
  4. 02/20 convert: fix leaks when resetting attributesPatrick Steinhardt, Aug 20, 2024
  5. Junio C HamanoAug 20, 2024
  6. 03/20 pretty: fix memory leaks when parsing pretty formatsPatrick Steinhardt, Aug 20, 2024
  7. Junio C HamanoAug 20, 2024
  8. Patrick SteinhardtAug 22, 2024
  9. 04/20 pretty: fix leaking key/value separator bufferPatrick Steinhardt, Aug 20, 2024
  10. 05/20 builtin/merge-tree: fix leaking `-X` strategy optionsPatrick Steinhardt, Aug 20, 2024
  11. Junio C HamanoAug 20, 2024
  12. 06/20 builtin/upload-archive: fix leaking args passed to `write_archive()`Patrick Steinhardt, Aug 20, 2024
  13. 07/20 builtin/archive: fix leaking `OPT_FILENAME()` valuePatrick Steinhardt, Aug 20, 2024
  14. 08/20 midx-write: fix leaking hashfile on error casesPatrick Steinhardt, Aug 20, 2024
  15. Junio C HamanoAug 20, 2024
  16. Patrick SteinhardtAug 22, 2024
  17. 09/20 builtin/repack: fix leaks when computing packs to repackPatrick Steinhardt, Aug 20, 2024
  18. 10/20 t/helper: fix leaking multi-pack-indices in "read-midx"Patrick Steinhardt, Aug 20, 2024
  19. 11/20 transport: fix leaking OID arrays in git:// transport dataPatrick Steinhardt, Aug 20, 2024
  20. 12/20 builtin/send-pack: fix leaking refspecsPatrick Steinhardt, Aug 20, 2024
  21. 13/20 sideband: fix leaks when configuring sideband colorsPatrick Steinhardt, Aug 20, 2024
  22. Junio C HamanoAug 20, 2024
  23. Patrick SteinhardtAug 22, 2024
  24. 14/20 builtin/fetch-pack: fix leaking refsPatrick Steinhardt, Aug 20, 2024
  25. Junio C HamanoAug 21, 2024
  26. 15/20 remote: fix leaking config stringsPatrick Steinhardt, Aug 20, 2024
  27. Junio C HamanoAug 21, 2024
  28. Patrick SteinhardtAug 22, 2024
  29. Junio C HamanoAug 22, 2024
  30. 16/20 remote: fix leaks when matching refspecsPatrick Steinhardt, Aug 20, 2024
  31. 17/20 remote: fix leaking peer ref when expanding refmapPatrick Steinhardt, Aug 20, 2024
  32. 18/20 builtin/fetch: fix leaking transaction with `--atomic`Patrick Steinhardt, Aug 20, 2024
  33. 19/20 transport: fix leaking arguments when fetching from bundlePatrick Steinhardt, Aug 20, 2024
  34. Junio C HamanoAug 21, 2024
  35. Patrick SteinhardtAug 22, 2024
  36. Junio C HamanoAug 22, 2024
  37. 20/20 transport: fix leaking negotiation tipsPatrick Steinhardt, Aug 20, 2024
  38. 00/20 Memory leak fixes (pt.5)Patrick Steinhardt, Aug 22, 2024
  39. 01/20 mailinfo: fix leaking header dataPatrick Steinhardt, Aug 22, 2024
  40. 02/20 convert: fix leaks when resetting attributesPatrick Steinhardt, Aug 22, 2024
  41. 03/20 pretty: fix memory leaks when parsing pretty formatsPatrick Steinhardt, Aug 22, 2024
  42. 04/20 pretty: fix leaking key/value separator bufferPatrick Steinhardt, Aug 22, 2024
  43. 05/20 builtin/merge-tree: fix leaking `-X` strategy optionsPatrick Steinhardt, Aug 22, 2024
  44. 06/20 builtin/upload-archive: fix leaking args passed to `write_archive()`Patrick Steinhardt, Aug 22, 2024
  45. 07/20 builtin/archive: fix leaking `OPT_FILENAME()` valuePatrick Steinhardt, Aug 22, 2024
  46. 08/20 midx-write: fix leaking hashfile on error casesPatrick Steinhardt, Aug 22, 2024
  47. 09/20 builtin/repack: fix leaks when computing packs to repackPatrick Steinhardt, Aug 22, 2024
  48. 10/20 t/helper: fix leaking multi-pack-indices in "read-midx"Patrick Steinhardt, Aug 22, 2024
  49. 11/20 transport: fix leaking OID arrays in git:// transport dataPatrick Steinhardt, Aug 22, 2024
  50. 12/20 builtin/send-pack: fix leaking refspecsPatrick Steinhardt, Aug 22, 2024
  51. 13/20 sideband: fix leaks when configuring sideband colorsPatrick Steinhardt, Aug 22, 2024
  52. 14/20 builtin/fetch-pack: fix leaking refsPatrick Steinhardt, Aug 22, 2024
  53. 15/20 remote: fix leaking config stringsPatrick Steinhardt, Aug 22, 2024
  54. 16/20 remote: fix leaks when matching refspecsPatrick Steinhardt, Aug 22, 2024
  55. 17/20 remote: fix leaking peer ref when expanding refmapPatrick Steinhardt, Aug 22, 2024
  56. 18/20 builtin/fetch: fix leaking transaction with `--atomic`Patrick Steinhardt, Aug 22, 2024
  57. 19/20 transport: fix leaking arguments when fetching from bundlePatrick Steinhardt, Aug 22, 2024
  58. Junio C HamanoAug 22, 2024
  59. 20/20 transport: fix leaking negotiation tipsPatrick Steinhardt, Aug 22, 2024
  60. Junio C HamanoAug 22, 2024

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.