ref-filter: add ref_format_clear() function

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>

Jeff King committed Sep 9, 2024 at 19:21 UTC db629c61f0be3665a36750fe2353b9ee958b0376
7 files changed +21
builtin/branch.c
+1
@@ -878,6 +878,7 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
878 string_list_clear(&output, 0);
879 ref_sorting_release(sorting);
880 ref_filter_clear(&filter);
881 + ref_format_clear(&format);
882 return 0;
883 } else if (edit_description) {
884 const char *branch_name;
builtin/for-each-ref.c
+1
@@ -104,6 +104,7 @@ int cmd_for_each_ref(int argc, const char **argv, const char *prefix)
104 filter_and_format_refs(&filter, flags, sorting, &format);
105
106 ref_filter_clear(&filter);
107 + ref_format_clear(&format);
108 ref_sorting_release(sorting);
109 strvec_clear(&vec);
110 return 0;
builtin/tag.c
+1
@@ -702,6 +702,7 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
702 cleanup:
703 ref_sorting_release(sorting);
704 ref_filter_clear(&filter);
705 + ref_format_clear(&format);
706 strbuf_release(&buf);
707 strbuf_release(&ref);
708 strbuf_release(&reflog_msg);
builtin/verify-tag.c
+1
@@ -65,5 +65,6 @@ int cmd_verify_tag(int argc, const char **argv, const char *prefix)
65 if (format.format)
66 pretty_print_ref(name, &oid, &format);
67 }
68 + ref_format_clear(&format);
69 return had_error;
70 }
ref-filter.c
+13
@@ -3621,3 +3621,16 @@ void ref_filter_clear(struct ref_filter *filter)
3621 free_commit_list(filter->unreachable_from);
3622 ref_filter_init(filter);
3623 }
3624 +
3625 +void ref_format_init(struct ref_format *format)
3626 +{
3627 + struct ref_format blank = REF_FORMAT_INIT;
3628 + memcpy(format, &blank, sizeof(blank));
3629 +}
3630 +
3631 +void ref_format_clear(struct ref_format *format)
3632 +{
3633 + string_list_clear(&format->bases, 0);
3634 + string_list_clear(&format->is_base_tips, 0);
3635 + ref_format_init(format);
3636 +}
ref-filter.h
+3
@@ -221,4 +221,7 @@ void filter_is_base(struct repository *r,
221 void ref_filter_init(struct ref_filter *filter);
222 void ref_filter_clear(struct ref_filter *filter);
223
224 +void ref_format_init(struct ref_format *format);
225 +void ref_format_clear(struct ref_format *format);
226 +
227 #endif /* REF_FILTER_H */
t/t6300-for-each-ref.sh
+1
@@ -5,6 +5,7 @@
5
6 test_description='for-each-ref test'
7
8 +TEST_PASSES_SANITIZE_LEAK=true
9 . ./test-lib.sh
10 GNUPGHOME_NOT_USED=$GNUPGHOME
11 . "$TEST_DIRECTORY"/lib-gpg.sh