From 19a305bd22e37cb256d18d1d4277f9bdc005ec26 Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Fri, 24 Jul 2026 17:43:26 -0700 Subject: [PATCH] remote: plug memory leaks The in-core data structure used to keep track of 'url..{insteadOf,pushInsteadOf} = ' 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 --- remote.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/remote.c b/remote.c index a664cd166a..dc849956ff 100644 --- a/remote.c +++ b/remote.c @@ -304,8 +304,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)); } @@ -464,13 +467,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); } }