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

[PATCH] remote: plug memory leaks

From
Junio C Hamano <gitster@pobox.com>
Date
Jul 25, 2026, 00:43 UTC
Message-ID
<xmqqv7a33nm9.fsf@gitster.g>

The in-core data structure used to keep track of 'url.<real>.{insteadOf,pushInsteadOf} = <alias>' settings is not properly cleaned up when the process is done with it.

Fix the rewrites_release() function to free not just the 'struct rewrites' instance itself, but also allocated structures that are pointed at by the 'struct rewrites' instance. One of the embedded structures holds a 'const char *' to point at a borrowed constant string from a configuration callback. Since the code does not modify this string, stop copying the value (alias URL) before registering it in 'struct rewrite', as nobody is freeing this member, to avoid leaking the extra copy.

Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 * These are not recently introduced leaks as far as I can tell, but
   the new tests in en/submodule-insteadof-remote-match expose them.
 remote.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
diff --git a/remote.c b/remote.c
index 368a43c1b2..e3d4b25040 100644
--- a/remote.c
+++ b/remote.c
@@ -309,8 +309,11 @@ static struct rewrite *make_rewrite(struct rewrites *r,
 
 static void rewrites_release(struct rewrites *r)
 {
-	for (int i = 0; i < r->rewrite_nr; i++)
+	for (int i = 0; i < r->rewrite_nr; i++) {
 		free((char *)r->rewrite[i]->base);
+		free(r->rewrite[i]->instead_of);
+		free(r->rewrite[i]);
+	}
 	free(r->rewrite);
 	memset(r, 0, sizeof(*r));
 }
@@ -469,13 +472,13 @@ static int handle_config(const char *key, const char *value,
 				return config_error_nonbool(key);
 			rewrite = make_rewrite(&remote_state->rewrites, name,
 					       namelen);
-			add_instead_of(rewrite, xstrdup(value));
+			add_instead_of(rewrite, value);
 		} else if (!strcmp(subkey, "pushinsteadof")) {
 			if (!value)
 				return config_error_nonbool(key);
 			rewrite = make_rewrite(&remote_state->rewrites_push,
 					       name, namelen);
-			add_instead_of(rewrite, xstrdup(value));
+			add_instead_of(rewrite, value);
 		}
 	}
 
-- 
2.55.0-576-g1c3ad6b142
Next: Junio C Hamano
Message 1 of 5 in “remote: plug memory leaks”
  1. remote: plug memory leaksJunio C Hamano, Jul 25, 2026
  2. Junio C HamanoJul 25, 2026
  3. remote: plug memory leaksJunio C Hamano, Jul 25, 2026
  4. Jeff KingJul 25, 2026
  5. Junio C HamanoJul 25, 2026

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.