summaryrefslogtreecommitdiff
path: root/add-patch.c
diff options
context:
space:
mode:
authorPhillip Wood <phillip.wood@dunelm.org.uk>2020-09-07 15:04:00 (GMT)
committerJunio C Hamano <gitster@pobox.com>2020-09-08 21:51:38 (GMT)
commit324efcf6b6d30d43b98e76c7beac90ecfb40d637 (patch)
treee060ddbd6c8e39b9c067971af70c08c74b18fda5 /add-patch.c
parent47ae905ffb98cc4d4fd90083da6bc8dab55d9ecc (diff)
downloadgit-324efcf6b6d30d43b98e76c7beac90ecfb40d637.zip
git-324efcf6b6d30d43b98e76c7beac90ecfb40d637.tar.gz
git-324efcf6b6d30d43b98e76c7beac90ecfb40d637.tar.bz2
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 <phillip.wood@dunelm.org.uk> Acked-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'add-patch.c')
-rw-r--r--add-patch.c24
1 files changed, 16 insertions, 8 deletions
diff --git a/add-patch.c b/add-patch.c
index f899389..87db599 100644
--- a/add-patch.c
+++ b/add-patch.c
@@ -266,6 +266,20 @@ struct add_p_state {
const char *revision;
};
+static void add_p_state_clear(struct add_p_state *s)
+{
+ size_t i;
+
+ strbuf_release(&s->answer);
+ strbuf_release(&s->buf);
+ strbuf_release(&s->plain);
+ strbuf_release(&s->colored);
+ for (i = 0; i < s->file_diff_nr; i++)
+ free(s->file_diff[i].hunk);
+ free(s->file_diff);
+ clear_add_i_state(&s->s);
+}
+
static void err(struct add_p_state *s, const char *fmt, ...)
{
va_list args;
@@ -1673,9 +1687,7 @@ int run_add_p(struct repository *r, enum add_p_mode mode,
repo_refresh_and_write_index(r, REFRESH_QUIET, 0, 1,
NULL, NULL, NULL) < 0) ||
parse_diff(&s, ps) < 0) {
- strbuf_release(&s.plain);
- strbuf_release(&s.colored);
- clear_add_i_state(&s.s);
+ add_p_state_clear(&s);
return -1;
}
@@ -1690,10 +1702,6 @@ int run_add_p(struct repository *r, enum add_p_mode mode,
else if (binary_count == s.file_diff_nr)
fprintf(stderr, _("Only binary files changed.\n"));
- strbuf_release(&s.answer);
- strbuf_release(&s.buf);
- strbuf_release(&s.plain);
- strbuf_release(&s.colored);
- clear_add_i_state(&s.s);
+ add_p_state_clear(&s);
return 0;
}