Skip to content

Commit db629c6

Browse files
peffgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent f046127 commit db629c6

File tree

7 files changed

+21
-0
lines changed

7 files changed

+21
-0
lines changed

builtin/branch.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -878,6 +878,7 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
878878
string_list_clear(&output, 0);
879879
ref_sorting_release(sorting);
880880
ref_filter_clear(&filter);
881+
ref_format_clear(&format);
881882
return 0;
882883
} else if (edit_description) {
883884
const char *branch_name;

builtin/for-each-ref.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ int cmd_for_each_ref(int argc, const char **argv, const char *prefix)
104104
filter_and_format_refs(&filter, flags, sorting, &format);
105105

106106
ref_filter_clear(&filter);
107+
ref_format_clear(&format);
107108
ref_sorting_release(sorting);
108109
strvec_clear(&vec);
109110
return 0;

builtin/tag.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -702,6 +702,7 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
702702
cleanup:
703703
ref_sorting_release(sorting);
704704
ref_filter_clear(&filter);
705+
ref_format_clear(&format);
705706
strbuf_release(&buf);
706707
strbuf_release(&ref);
707708
strbuf_release(&reflog_msg);

builtin/verify-tag.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,5 +65,6 @@ int cmd_verify_tag(int argc, const char **argv, const char *prefix)
6565
if (format.format)
6666
pretty_print_ref(name, &oid, &format);
6767
}
68+
ref_format_clear(&format);
6869
return had_error;
6970
}

ref-filter.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3621,3 +3621,16 @@ void ref_filter_clear(struct ref_filter *filter)
36213621
free_commit_list(filter->unreachable_from);
36223622
ref_filter_init(filter);
36233623
}
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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,4 +221,7 @@ void filter_is_base(struct repository *r,
221221
void ref_filter_init(struct ref_filter *filter);
222222
void ref_filter_clear(struct ref_filter *filter);
223223

224+
void ref_format_init(struct ref_format *format);
225+
void ref_format_clear(struct ref_format *format);
226+
224227
#endif /* REF_FILTER_H */

t/t6300-for-each-ref.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
test_description='for-each-ref test'
77

8+
TEST_PASSES_SANITIZE_LEAK=true
89
. ./test-lib.sh
910
GNUPGHOME_NOT_USED=$GNUPGHOME
1011
. "$TEST_DIRECTORY"/lib-gpg.sh

0 commit comments

Comments
 (0)