Skip to content

Commit ac95f5d

Browse files
avargitster
authored andcommitted
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. 0e5bba5 (add UNLEAK annotation for reducing leak false positives, 2017-09-08) 2. d861d34 (worktree: remove extra members from struct add_opts, 2018-04-24) 3. 0db4961 (worktree: teach `add` to accept --reason <string> with --lock, 2021-07-15) 4. 0e5bba5 and 00d8c31 (commit: fix "author_ident" leak, 2022-05-12). Signed-off-by: Ævar Arnfjörð Bjarmason <[email protected]> Signed-off-by: Taylor Blau <[email protected]>
1 parent 603f2f5 commit ac95f5d

File tree

6 files changed

+35
-33
lines changed

6 files changed

+35
-33
lines changed

builtin/add.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -695,6 +695,6 @@ int cmd_add(int argc, const char **argv, const char *prefix)
695695
die(_("Unable to write new index file"));
696696

697697
dir_clear(&dir);
698-
UNLEAK(pathspec);
698+
clear_pathspec(&pathspec);
699699
return exit_status;
700700
}

builtin/bugreport.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ int cmd_bugreport(int argc, const char **argv, const char *prefix)
106106
const char *user_relative_path = NULL;
107107
char *prefixed_filename;
108108
size_t output_path_len;
109+
int ret;
109110

110111
const struct option bugreport_options[] = {
111112
OPT_CALLBACK_F(0, "diagnose", &diagnose, N_("mode"),
@@ -182,7 +183,9 @@ int cmd_bugreport(int argc, const char **argv, const char *prefix)
182183
user_relative_path);
183184

184185
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;
188191
}

builtin/commit.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1874,8 +1874,8 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
18741874
apply_autostash(git_path_merge_autostash(the_repository));
18751875

18761876
cleanup:
1877-
UNLEAK(author_ident);
1878-
UNLEAK(err);
1879-
UNLEAK(sb);
1877+
strbuf_release(&author_ident);
1878+
strbuf_release(&err);
1879+
strbuf_release(&sb);
18801880
return ret;
18811881
}

builtin/config.c

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -639,8 +639,9 @@ static char *default_user_config(void)
639639
int cmd_config(int argc, const char **argv, const char *prefix)
640640
{
641641
int nongit = !startup_info->have_repository;
642-
char *value;
642+
char *value = NULL;
643643
int flags = 0;
644+
int ret = 0;
644645

645646
given_config_source.file = xstrdup_or_null(getenv(CONFIG_ENVIRONMENT));
646647

@@ -856,44 +857,38 @@ int cmd_config(int argc, const char **argv, const char *prefix)
856857
free(config_file);
857858
}
858859
else if (actions == ACTION_SET) {
859-
int ret;
860860
check_write();
861861
check_argc(argc, 2, 2);
862862
value = normalize_value(argv[0], argv[1]);
863-
UNLEAK(value);
864863
ret = git_config_set_in_file_gently(given_config_source.file, argv[0], value);
865864
if (ret == CONFIG_NOTHING_SET)
866865
error(_("cannot overwrite multiple values with a single value\n"
867866
" Use a regexp, --add or --replace-all to change %s."), argv[0]);
868-
return ret;
869867
}
870868
else if (actions == ACTION_SET_ALL) {
871869
check_write();
872870
check_argc(argc, 2, 3);
873871
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);
878875
}
879876
else if (actions == ACTION_ADD) {
880877
check_write();
881878
check_argc(argc, 2, 2);
882879
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);
888884
}
889885
else if (actions == ACTION_REPLACE_ALL) {
890886
check_write();
891887
check_argc(argc, 2, 3);
892888
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);
897892
}
898893
else if (actions == ACTION_GET) {
899894
check_argc(argc, 1, 2);
@@ -934,26 +929,28 @@ int cmd_config(int argc, const char **argv, const char *prefix)
934929
flags | CONFIG_FLAGS_MULTI_REPLACE);
935930
}
936931
else if (actions == ACTION_RENAME_SECTION) {
937-
int ret;
938932
check_write();
939933
check_argc(argc, 2, 2);
940934
ret = git_config_rename_section_in_file(given_config_source.file,
941935
argv[0], argv[1]);
942936
if (ret < 0)
943937
return ret;
944-
if (ret == 0)
938+
else if (!ret)
945939
die(_("no such section: %s"), argv[0]);
940+
else
941+
ret = 0;
946942
}
947943
else if (actions == ACTION_REMOVE_SECTION) {
948-
int ret;
949944
check_write();
950945
check_argc(argc, 1, 1);
951946
ret = git_config_rename_section_in_file(given_config_source.file,
952947
argv[0], NULL);
953948
if (ret < 0)
954949
return ret;
955-
if (ret == 0)
950+
else if (!ret)
956951
die(_("no such section: %s"), argv[0]);
952+
else
953+
ret = 0;
957954
}
958955
else if (actions == ACTION_GET_COLOR) {
959956
check_argc(argc, 1, 2);
@@ -966,5 +963,6 @@ int cmd_config(int argc, const char **argv, const char *prefix)
966963
return get_colorbool(argv[0], argc == 2);
967964
}
968965

969-
return 0;
966+
free(value);
967+
return ret;
970968
}

builtin/diff.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -609,7 +609,7 @@ int cmd_diff(int argc, const char **argv, const char *prefix)
609609
if (1 < rev.diffopt.skip_stat_unmatch)
610610
refresh_index_quietly();
611611
release_revisions(&rev);
612-
UNLEAK(ent);
612+
object_array_clear(&ent);
613613
UNLEAK(blob);
614614
return result;
615615
}

builtin/worktree.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -629,6 +629,7 @@ static int add(int ac, const char **av, const char *prefix)
629629
N_("try to match the new branch name with a remote-tracking branch")),
630630
OPT_END()
631631
};
632+
int ret;
632633

633634
memset(&opts, 0, sizeof(opts));
634635
opts.checkout = 1;
@@ -705,9 +706,9 @@ static int add(int ac, const char **av, const char *prefix)
705706
die(_("--[no-]track can only be used if a new branch is created"));
706707
}
707708

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;
711712
}
712713

713714
static void show_worktree_porcelain(struct worktree *wt, int line_terminator)

0 commit comments

Comments
 (0)