Skip to content

Commit 324efcf

Browse files
phillipwoodgitster
authored andcommitted
add -p: fix memory leak
asan reports that the C version of `add -p` is not freeing all the memory it allocates. Fix this by introducing a function to clear `struct add_p_state` and use it instead of freeing individual members. Signed-off-by: Phillip Wood <[email protected]> Acked-by: Johannes Schindelin <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 47ae905 commit 324efcf

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

add-patch.c

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,20 @@ struct add_p_state {
266266
const char *revision;
267267
};
268268

269+
static void add_p_state_clear(struct add_p_state *s)
270+
{
271+
size_t i;
272+
273+
strbuf_release(&s->answer);
274+
strbuf_release(&s->buf);
275+
strbuf_release(&s->plain);
276+
strbuf_release(&s->colored);
277+
for (i = 0; i < s->file_diff_nr; i++)
278+
free(s->file_diff[i].hunk);
279+
free(s->file_diff);
280+
clear_add_i_state(&s->s);
281+
}
282+
269283
static void err(struct add_p_state *s, const char *fmt, ...)
270284
{
271285
va_list args;
@@ -1673,9 +1687,7 @@ int run_add_p(struct repository *r, enum add_p_mode mode,
16731687
repo_refresh_and_write_index(r, REFRESH_QUIET, 0, 1,
16741688
NULL, NULL, NULL) < 0) ||
16751689
parse_diff(&s, ps) < 0) {
1676-
strbuf_release(&s.plain);
1677-
strbuf_release(&s.colored);
1678-
clear_add_i_state(&s.s);
1690+
add_p_state_clear(&s);
16791691
return -1;
16801692
}
16811693

@@ -1690,10 +1702,6 @@ int run_add_p(struct repository *r, enum add_p_mode mode,
16901702
else if (binary_count == s.file_diff_nr)
16911703
fprintf(stderr, _("Only binary files changed.\n"));
16921704

1693-
strbuf_release(&s.answer);
1694-
strbuf_release(&s.buf);
1695-
strbuf_release(&s.plain);
1696-
strbuf_release(&s.colored);
1697-
clear_add_i_state(&s.s);
1705+
add_p_state_clear(&s);
16981706
return 0;
16991707
}

0 commit comments

Comments
 (0)