built-ins: use free() not UNLEAK() if trivial, rm dead code

For a lot of uses of UNLEAK() it would be quite tricky to release the memory involved, or we're missing the relevant *_(release|clear)() functions. But in these cases we have them already, and can just invoke them on the variable(s) involved, instead of UNLEAK(). For "builtin/worktree.c" the UNLEAK() was also added in [1], but the struct member it's unleaking was removed in [2]. The only non-"int" member of that structure is "const char *keep_locked", which comes to us via "argv" or a string literal[3]. We have good visibility via the compiler and tooling (e.g. SANITIZE=address) on bad free()-ing, but none on UNLEAK() we don't need anymore. So let's prefer releasing the memory when it's easy. For "bugreport", "worktree" and "config" we need to start using a "ret = ..." return pattern. For "builtin/bugreport.c" these UNLEAK() were added in [4], and for "builtin/config.c" in [1]. For "config" the code seen here was the only user of the "value" variable. For "ACTION_{RENAME,REMOVE}_SECTION" we need to be sure to return the right exit code in the cases where we were relying on falling through to the top-level. I think there's still a use-case for UNLEAK(), but hat it's changed since then. Using it so that "we can see the real leaks" is counter-productive in these cases. It's more useful to have UNLEAK() be a marker of the remaining odd cases where it's hard to free() the memory for whatever reason. With this change less than 20 of them remain in-tree. 1. 0e5bba53af7 (add UNLEAK annotation for reducing leak false positives, 2017-09-08) 2. d861d34a6ed (worktree: remove extra members from struct add_opts, 2018-04-24) 3. 0db4961c49b (worktree: teach `add` to accept --reason <string> with --lock, 2021-07-15) 4. 0e5bba53af7 and 00d8c311050 (commit: fix "author_ident" leak, 2022-05-12). Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com> Signed-off-by: Taylor Blau <me@ttaylorr.com>

Ævar Arnfjörð Bjarmason committed Nov 8, 2022 at 19:17 UTC ac95f5d36adb42980064587fd7910fa275ff853e
6 files changed +35 -33
builtin/add.c
+1 -1
@@ -695,6 +695,6 @@ finish:
695 die(_("Unable to write new index file"));
696
697 dir_clear(&dir);
698 - UNLEAK(pathspec);
698 + clear_pathspec(&pathspec);
699 return exit_status;
700 }
builtin/bugreport.c
+6 -3
@@ -106,6 +106,7 @@ int cmd_bugreport(int argc, const char **argv, const char *prefix)
106 const char *user_relative_path = NULL;
107 char *prefixed_filename;
108 size_t output_path_len;
109 + int ret;
110
111 const struct option bugreport_options[] = {
112 OPT_CALLBACK_F(0, "diagnose", &diagnose, N_("mode"),
@@ -182,7 +183,9 @@ int cmd_bugreport(int argc, const char **argv, const char *prefix)
183 user_relative_path);
184
185 free(prefixed_filename);
185 - UNLEAK(buffer);
186 - UNLEAK(report_path);
187 - return !!launch_editor(report_path.buf, NULL, NULL);
186 + strbuf_release(&buffer);
187 +
188 + ret = !!launch_editor(report_path.buf, NULL, NULL);
189 + strbuf_release(&report_path);
190 + return ret;
191 }
builtin/commit.c
+3 -3
@@ -1874,8 +1874,8 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
1874 apply_autostash(git_path_merge_autostash(the_repository));
1875
1876 cleanup:
1877 - UNLEAK(author_ident);
1878 - UNLEAK(err);
1879 - UNLEAK(sb);
1877 + strbuf_release(&author_ident);
1878 + strbuf_release(&err);
1879 + strbuf_release(&sb);
1880 return ret;
1881 }
builtin/config.c
+20 -22
@@ -639,8 +639,9 @@ static char *default_user_config(void)
639 int cmd_config(int argc, const char **argv, const char *prefix)
640 {
641 int nongit = !startup_info->have_repository;
642 - char *value;
642 + char *value = NULL;
643 int flags = 0;
644 + int ret = 0;
645
646 given_config_source.file = xstrdup_or_null(getenv(CONFIG_ENVIRONMENT));
647
@@ -856,44 +857,38 @@ int cmd_config(int argc, const char **argv, const char *prefix)
857 free(config_file);
858 }
859 else if (actions == ACTION_SET) {
859 - int ret;
860 check_write();
861 check_argc(argc, 2, 2);
862 value = normalize_value(argv[0], argv[1]);
863 - UNLEAK(value);
863 ret = git_config_set_in_file_gently(given_config_source.file, argv[0], value);
864 if (ret == CONFIG_NOTHING_SET)
865 error(_("cannot overwrite multiple values with a single value\n"
866 " Use a regexp, --add or --replace-all to change %s."), argv[0]);
868 - return ret;
867 }
868 else if (actions == ACTION_SET_ALL) {
869 check_write();
870 check_argc(argc, 2, 3);
871 value = normalize_value(argv[0], argv[1]);
874 - UNLEAK(value);
875 - return git_config_set_multivar_in_file_gently(given_config_source.file,
876 - argv[0], value, argv[2],
877 - flags);
872 + ret = git_config_set_multivar_in_file_gently(given_config_source.file,
873 + argv[0], value, argv[2],
874 + flags);
875 }
876 else if (actions == ACTION_ADD) {
877 check_write();
878 check_argc(argc, 2, 2);
879 value = normalize_value(argv[0], argv[1]);
883 - UNLEAK(value);
884 - return git_config_set_multivar_in_file_gently(given_config_source.file,
885 - argv[0], value,
886 - CONFIG_REGEX_NONE,
887 - flags);
880 + ret = git_config_set_multivar_in_file_gently(given_config_source.file,
881 + argv[0], value,
882 + CONFIG_REGEX_NONE,
883 + flags);
884 }
885 else if (actions == ACTION_REPLACE_ALL) {
886 check_write();
887 check_argc(argc, 2, 3);
888 value = normalize_value(argv[0], argv[1]);
893 - UNLEAK(value);
894 - return git_config_set_multivar_in_file_gently(given_config_source.file,
895 - argv[0], value, argv[2],
896 - flags | CONFIG_FLAGS_MULTI_REPLACE);
889 + ret = git_config_set_multivar_in_file_gently(given_config_source.file,
890 + argv[0], value, argv[2],
891 + flags | CONFIG_FLAGS_MULTI_REPLACE);
892 }
893 else if (actions == ACTION_GET) {
894 check_argc(argc, 1, 2);
@@ -934,26 +929,28 @@ int cmd_config(int argc, const char **argv, const char *prefix)
929 flags | CONFIG_FLAGS_MULTI_REPLACE);
930 }
931 else if (actions == ACTION_RENAME_SECTION) {
937 - int ret;
932 check_write();
933 check_argc(argc, 2, 2);
934 ret = git_config_rename_section_in_file(given_config_source.file,
935 argv[0], argv[1]);
936 if (ret < 0)
937 return ret;
944 - if (ret == 0)
938 + else if (!ret)
939 die(_("no such section: %s"), argv[0]);
940 + else
941 + ret = 0;
942 }
943 else if (actions == ACTION_REMOVE_SECTION) {
948 - int ret;
944 check_write();
945 check_argc(argc, 1, 1);
946 ret = git_config_rename_section_in_file(given_config_source.file,
947 argv[0], NULL);
948 if (ret < 0)
949 return ret;
955 - if (ret == 0)
950 + else if (!ret)
951 die(_("no such section: %s"), argv[0]);
952 + else
953 + ret = 0;
954 }
955 else if (actions == ACTION_GET_COLOR) {
956 check_argc(argc, 1, 2);
@@ -966,5 +963,6 @@ int cmd_config(int argc, const char **argv, const char *prefix)
963 return get_colorbool(argv[0], argc == 2);
964 }
965
969 - return 0;
966 + free(value);
967 + return ret;
968 }
builtin/diff.c
+1 -1
@@ -609,7 +609,7 @@ int cmd_diff(int argc, const char **argv, const char *prefix)
609 if (1 < rev.diffopt.skip_stat_unmatch)
610 refresh_index_quietly();
611 release_revisions(&rev);
612 - UNLEAK(ent);
612 + object_array_clear(&ent);
613 UNLEAK(blob);
614 return result;
615 }
builtin/worktree.c
+4 -3
@@ -629,6 +629,7 @@ static int add(int ac, const char **av, const char *prefix)
629 N_("try to match the new branch name with a remote-tracking branch")),
630 OPT_END()
631 };
632 + int ret;
633
634 memset(&opts, 0, sizeof(opts));
635 opts.checkout = 1;
@@ -705,9 +706,9 @@ static int add(int ac, const char **av, const char *prefix)
706 die(_("--[no-]track can only be used if a new branch is created"));
707 }
708
708 - UNLEAK(path);
709 - UNLEAK(opts);
710 - return add_worktree(path, branch, &opts);
709 + ret = add_worktree(path, branch, &opts);
710 + free(path);
711 + return ret;
712 }
713
714 static void show_worktree_porcelain(struct worktree *wt, int line_terminator)