range-diff: plug memory leak in common invocation
Create a public release_patch() version of the private free_patch() function added in 13b5af22f39 (apply: move libified code from builtin/apply.c to apply.{c,h}, 2016-04-22). Unlike the existing function this one doesn't free() the "struct patch" itself, so we can use it for variables on the stack. Use it in range-diff.c to fix a memory leak in common range-diff invocations, e.g.: git -P range-diff origin/master origin/next origin/seen Would emit several errors when compiled with SANITIZE=leak, but now runs cleanly. Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Ævar Arnfjörð Bjarmason committed
Mar 4, 2022 at 19:32 UTC
4998e93fa6e0da45440dd344419974e46d81a165
3 files changed
+9
-1
apply.c
+6
-1
@@ -219,13 +219,18 @@ static void free_fragment_list(struct fragment *list)
219
}
220
}
221
222
-static void free_patch(struct patch *patch)
222
+void release_patch(struct patch *patch)
223
{
224
free_fragment_list(patch->fragments);
225
free(patch->def_name);
226
free(patch->old_name);
227
free(patch->new_name);
228
free(patch->result);
229
+}
230
+
231
+static void free_patch(struct patch *patch)
232
+{
233
+ release_patch(patch);
234
free(patch);
235
}
236
apply.h
+2
@@ -173,6 +173,8 @@ int parse_git_diff_header(struct strbuf *root,
173
unsigned int size,
174
struct patch *patch);
175
176
+void release_patch(struct patch *patch);
177
+
178
/*
179
* Some aspects of the apply behavior are controlled by the following
180
* bits in the "options" parameter passed to apply_all_patches().
range-diff.c
+1
@@ -165,6 +165,7 @@ static int read_patches(const char *range, struct string_list *list,
165
patch.old_mode, patch.new_mode);
166
167
strbuf_addstr(&buf, " ##");
168
+ release_patch(&patch);
169
} else if (in_header) {
170
if (starts_with(line, "Author: ")) {
171
strbuf_addstr(&buf, " ## Metadata ##\n");