]> git.ipfire.org Git - thirdparty/git.git/commitdiff
ref-filter: add ref_format_clear() function
authorJeff King <peff@peff.net>
Mon, 9 Sep 2024 23:21:18 +0000 (19:21 -0400)
committerJunio C Hamano <gitster@pobox.com>
Mon, 9 Sep 2024 23:26:11 +0000 (16:26 -0700)
After using the ref-filter API, callers should use ref_filter_clear() to
free any used memory. However, there's not a matching function to clear
the ref_format struct.

Traditionally this did not need to be cleaned up, as it was just a way
for the caller to store and pass format options as a single unit. Even
though the parsing step of some placeholders may allocate data, that's
usually inside their "used_atom" structs, which are part of the
ref_filter itself.

But a few placeholders keep data outside of there. The %(ahead-behind)
and %(is-base) parsers both keep a master list of bases, because they
perform a single filtering pass outside of the use of any particular
atom. And since the format parser does not have access to the ref_filter
struct, they store their cross-atom data in the ref_format struct
itself.

And thus when they are finished, the ref_format also needs to be cleaned
up. So let's add a function to do so, and call it from all of the users
of the ref-filter API.

The %(is-base) case is found by running LSan on t6300. After this patch,
the script can now be marked leak-free.

Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/branch.c
builtin/for-each-ref.c
builtin/tag.c
builtin/verify-tag.c
ref-filter.c
ref-filter.h
t/t6300-for-each-ref.sh

index 3f870741bfd88a8da565b7571256056a393d3b4e..c98601c6fe01143a6f7e024da8b0a1269f48a498 100644 (file)
@@ -878,6 +878,7 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
                string_list_clear(&output, 0);
                ref_sorting_release(sorting);
                ref_filter_clear(&filter);
+               ref_format_clear(&format);
                return 0;
        } else if (edit_description) {
                const char *branch_name;
index 5517a4a1c084eef8982e1cb95daa444a9eb60606..c72fa05bcb1348876842543d50db62130403645f 100644 (file)
@@ -104,6 +104,7 @@ int cmd_for_each_ref(int argc, const char **argv, const char *prefix)
        filter_and_format_refs(&filter, flags, sorting, &format);
 
        ref_filter_clear(&filter);
+       ref_format_clear(&format);
        ref_sorting_release(sorting);
        strvec_clear(&vec);
        return 0;
index a1fb218512cc1a072682a33411a6da29313e2446..607e48e3110ee94fcae8fbe60ba9df156aded456 100644 (file)
@@ -702,6 +702,7 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
 cleanup:
        ref_sorting_release(sorting);
        ref_filter_clear(&filter);
+       ref_format_clear(&format);
        strbuf_release(&buf);
        strbuf_release(&ref);
        strbuf_release(&reflog_msg);
index c731e2f87b4ee35e4910e3a8c11e18b8e68e58c1..77becf7e75a1da6e1b37816cba80f4ce6d053884 100644 (file)
@@ -65,5 +65,6 @@ int cmd_verify_tag(int argc, const char **argv, const char *prefix)
                if (format.format)
                        pretty_print_ref(name, &oid, &format);
        }
+       ref_format_clear(&format);
        return had_error;
 }
index 0f51095bbd4391d5a39be376acbcae44025c69b7..ce1bcfad85783d8b24384761011d5077aa42a46b 100644 (file)
@@ -3621,3 +3621,16 @@ void ref_filter_clear(struct ref_filter *filter)
        free_commit_list(filter->unreachable_from);
        ref_filter_init(filter);
 }
+
+void ref_format_init(struct ref_format *format)
+{
+       struct ref_format blank = REF_FORMAT_INIT;
+       memcpy(format, &blank, sizeof(blank));
+}
+
+void ref_format_clear(struct ref_format *format)
+{
+       string_list_clear(&format->bases, 0);
+       string_list_clear(&format->is_base_tips, 0);
+       ref_format_init(format);
+}
index e794b8a676695edcab9c07c06d2a8df3ad9e2fb2..754038ab078669d82e4fff10514e46803cc265b7 100644 (file)
@@ -221,4 +221,7 @@ void filter_is_base(struct repository *r,
 void ref_filter_init(struct ref_filter *filter);
 void ref_filter_clear(struct ref_filter *filter);
 
+void ref_format_init(struct ref_format *format);
+void ref_format_clear(struct ref_format *format);
+
 #endif /*  REF_FILTER_H  */
index e8db612f956adbdfb526e12c63159c874d6e460c..b3163629c557934309af2e2e153a0c16f35ecc94 100755 (executable)
@@ -5,6 +5,7 @@
 
 test_description='for-each-ref test'
 
+TEST_PASSES_SANITIZE_LEAK=true
 . ./test-lib.sh
 GNUPGHOME_NOT_USED=$GNUPGHOME
 . "$TEST_DIRECTORY"/lib-gpg.sh