]> git.ipfire.org Git - thirdparty/git.git/commitdiff
range-diff: plug memory leak in common invocation
authorÆvar Arnfjörð Bjarmason <avarab@gmail.com>
Fri, 4 Mar 2022 18:32:15 +0000 (19:32 +0100)
committerJunio C Hamano <gitster@pobox.com>
Fri, 4 Mar 2022 21:24:19 +0000 (13:24 -0800)
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>
apply.c
apply.h
range-diff.c

diff --git a/apply.c b/apply.c
index 0912307bd91a5005d9a4b57e2a4324541e247edc..01f9181642889824bd641dd6949e1f3fd3852600 100644 (file)
--- a/apply.c
+++ b/apply.c
@@ -219,13 +219,18 @@ static void free_fragment_list(struct fragment *list)
        }
 }
 
-static void free_patch(struct patch *patch)
+void release_patch(struct patch *patch)
 {
        free_fragment_list(patch->fragments);
        free(patch->def_name);
        free(patch->old_name);
        free(patch->new_name);
        free(patch->result);
+}
+
+static void free_patch(struct patch *patch)
+{
+       release_patch(patch);
        free(patch);
 }
 
diff --git a/apply.h b/apply.h
index 4052da50c0658cf6434b262bba6f557459e7950c..b9f18ce87d1e0374a04aaaf785592c752dba6058 100644 (file)
--- a/apply.h
+++ b/apply.h
@@ -173,6 +173,8 @@ int parse_git_diff_header(struct strbuf *root,
                          unsigned int size,
                          struct patch *patch);
 
+void release_patch(struct patch *patch);
+
 /*
  * Some aspects of the apply behavior are controlled by the following
  * bits in the "options" parameter passed to apply_all_patches().
index 30a4de5c2d8a1447dc23d6b5d61278df0f796bbb..b2a2961f52159636c7b7b5199ce8e83e7b806d5c 100644 (file)
@@ -165,6 +165,7 @@ static int read_patches(const char *range, struct string_list *list,
                                            patch.old_mode, patch.new_mode);
 
                        strbuf_addstr(&buf, " ##");
+                       release_patch(&patch);
                } else if (in_header) {
                        if (starts_with(line, "Author: ")) {
                                strbuf_addstr(&buf, " ## Metadata ##\n");