builtin/branch: fix leaking sorting options

The sorting options are leaking, but given that they are marked with `UNLEAK()` the leak sanitizer doesn't complain. Fix the leak by creating a common exit path and clearing the vector such that we can get rid of the `UNLEAK()` annotation entirely. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt committed Nov 20, 2024 at 14:39 UTC b97301c13c758f168019129fc888e6492c046752
1 file changed +22 -11
builtin/branch.c
+22 -11
@@ -722,6 +722,7 @@ int cmd_branch(int argc,
722 static struct ref_sorting *sorting;
723 struct string_list sorting_options = STRING_LIST_INIT_DUP;
724 struct ref_format format = REF_FORMAT_INIT;
725 + int ret;
726
727 struct option options[] = {
728 OPT_GROUP(N_("Generic options")),
@@ -851,15 +852,15 @@ int cmd_branch(int argc,
852 if (list)
853 setup_auto_pager("branch", 1);
854
854 - UNLEAK(sorting_options);
855 -
855 if (delete) {
856 if (!argc)
857 die(_("branch name required"));
859 - return delete_branches(argc, argv, delete > 1, filter.kind, quiet);
858 + ret = delete_branches(argc, argv, delete > 1, filter.kind, quiet);
859 + goto out;
860 } else if (show_current) {
861 print_current_branch_name();
862 - return 0;
862 + ret = 0;
863 + goto out;
864 } else if (list) {
865 /* git branch --list also shows HEAD when it is detached */
866 if ((filter.kind & FILTER_REFS_BRANCHES) && filter.detached)
@@ -882,12 +883,13 @@ int cmd_branch(int argc,
883 ref_sorting_release(sorting);
884 ref_filter_clear(&filter);
885 ref_format_clear(&format);
885 - return 0;
886 +
887 + ret = 0;
888 + goto out;
889 } else if (edit_description) {
890 const char *branch_name;
891 struct strbuf branch_ref = STRBUF_INIT;
892 struct strbuf buf = STRBUF_INIT;
890 - int ret = 1; /* assume failure */
893
894 if (!argc) {
895 if (filter.detached)
@@ -901,18 +903,22 @@ int cmd_branch(int argc,
903 }
904
905 strbuf_addf(&branch_ref, "refs/heads/%s", branch_name);
904 - if (!refs_ref_exists(get_main_ref_store(the_repository), branch_ref.buf))
906 + if (!refs_ref_exists(get_main_ref_store(the_repository), branch_ref.buf)) {
907 error((!argc || branch_checked_out(branch_ref.buf))
908 ? _("no commit on branch '%s' yet")
909 : _("no branch named '%s'"),
910 branch_name);
909 - else if (!edit_branch_description(branch_name))
911 + ret = 1;
912 + } else if (!edit_branch_description(branch_name)) {
913 ret = 0; /* happy */
914 + } else {
915 + ret = 1;
916 + }
917
918 strbuf_release(&branch_ref);
919 strbuf_release(&buf);
920
915 - return ret;
921 + goto out;
922 } else if (copy || rename) {
923 if (!argc)
924 die(_("branch name required"));
@@ -1000,12 +1006,17 @@ int cmd_branch(int argc,
1006 create_branches_recursively(the_repository, branch_name,
1007 start_name, NULL, force,
1008 reflog, quiet, track, 0);
1003 - return 0;
1009 + ret = 0;
1010 + goto out;
1011 }
1012 create_branch(the_repository, branch_name, start_name, force, 0,
1013 reflog, quiet, track, 0);
1014 } else
1015 usage_with_options(builtin_branch_usage, options);
1016
1010 - return 0;
1017 + ret = 0;
1018 +
1019 +out:
1020 + string_list_clear(&sorting_options, 0);
1021 + return ret;
1022 }