Skip to content

Commit 19a305b

Browse files
committed
remote: plug memory leaks
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>
1 parent 94f0577 commit 19a305b

1 file changed

Lines changed: 6 additions & 3 deletions

File tree

remote.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -304,8 +304,11 @@ static struct rewrite *make_rewrite(struct rewrites *r,
304304

305305
static void rewrites_release(struct rewrites *r)
306306
{
307-
for (int i = 0; i < r->rewrite_nr; i++)
307+
for (int i = 0; i < r->rewrite_nr; i++) {
308308
free((char *)r->rewrite[i]->base);
309+
free(r->rewrite[i]->instead_of);
310+
free(r->rewrite[i]);
311+
}
309312
free(r->rewrite);
310313
memset(r, 0, sizeof(*r));
311314
}
@@ -464,13 +467,13 @@ static int handle_config(const char *key, const char *value,
464467
return config_error_nonbool(key);
465468
rewrite = make_rewrite(&remote_state->rewrites, name,
466469
namelen);
467-
add_instead_of(rewrite, xstrdup(value));
470+
add_instead_of(rewrite, value);
468471
} else if (!strcmp(subkey, "pushinsteadof")) {
469472
if (!value)
470473
return config_error_nonbool(key);
471474
rewrite = make_rewrite(&remote_state->rewrites_push,
472475
name, namelen);
473-
add_instead_of(rewrite, xstrdup(value));
476+
add_instead_of(rewrite, value);
474477
}
475478
}
476479

0 commit comments

Comments
 (0)