Skip to content

Commit 9ea1378

Browse files
committed
Merge branch 'ab/various-leak-fixes'
Various leak fixes. * ab/various-leak-fixes: built-ins: use free() not UNLEAK() if trivial, rm dead code revert: fix parse_options_concat() leak cherry-pick: free "struct replay_opts" members rebase: don't leak on "--abort" connected.c: free the "struct packed_git" sequencer.c: fix "opts->strategy" leak in read_strategy_opts() ls-files: fix a --with-tree memory leak revision API: call graph_clear() in release_revisions() unpack-file: fix ancient leak in create_temp_file() built-ins & libs & helpers: add/move destructors, fix leaks dir.c: free "ident" and "exclude_per_dir" in "struct untracked_cache" read-cache.c: clear and free "sparse_checkout_patterns" commit: discard partial cache before (re-)reading it {reset,merge}: call discard_index() before returning tests: mark tests as passing with SANITIZE=leak
2 parents 7576e51 + ac95f5d commit 9ea1378

File tree

77 files changed

+142
-48
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

77 files changed

+142
-48
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/checkout.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1471,6 +1471,8 @@ static void die_if_some_operation_in_progress(void)
14711471
"or \"git worktree add\"."));
14721472
if (state.bisect_in_progress)
14731473
warning(_("you are switching branch while bisecting"));
1474+
1475+
wt_status_state_free_buffers(&state);
14741476
}
14751477

14761478
static int checkout_branch(struct checkout_opts *opts,

builtin/commit.c

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -991,8 +991,11 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
991991
struct object_id oid;
992992
const char *parent = "HEAD";
993993

994-
if (!active_nr && read_cache() < 0)
995-
die(_("Cannot read index"));
994+
if (!active_nr) {
995+
discard_cache();
996+
if (read_cache() < 0)
997+
die(_("Cannot read index"));
998+
}
996999

9971000
if (amend)
9981001
parent = "HEAD^1";
@@ -1875,8 +1878,8 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
18751878
apply_autostash(git_path_merge_autostash(the_repository));
18761879

18771880
cleanup:
1878-
UNLEAK(author_ident);
1879-
UNLEAK(err);
1880-
UNLEAK(sb);
1881+
strbuf_release(&author_ident);
1882+
strbuf_release(&err);
1883+
strbuf_release(&sb);
18811884
return ret;
18821885
}

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
@@ -612,7 +612,7 @@ int cmd_diff(int argc, const char **argv, const char *prefix)
612612
if (1 < rev.diffopt.skip_stat_unmatch)
613613
refresh_index_quietly();
614614
release_revisions(&rev);
615-
UNLEAK(ent);
615+
object_array_clear(&ent);
616616
UNLEAK(blob);
617617
return result;
618618
}

builtin/ls-files.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -613,6 +613,7 @@ void overlay_tree_on_index(struct index_state *istate,
613613
if (!fn)
614614
fn = read_one_entry_quick;
615615
err = read_tree(the_repository, tree, &pathspec, fn, istate);
616+
clear_pathspec(&pathspec);
616617
if (err)
617618
die("unable to read tree entries %s", tree_name);
618619

builtin/merge.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1789,5 +1789,6 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
17891789
}
17901790
strbuf_release(&buf);
17911791
free(branch_to_free);
1792+
discard_index(&the_index);
17921793
return ret;
17931794
}

builtin/rebase.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1322,6 +1322,7 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
13221322
if (reset_head(the_repository, &ropts) < 0)
13231323
die(_("could not move back to %s"),
13241324
oid_to_hex(&options.orig_head->object.oid));
1325+
strbuf_release(&head_msg);
13251326
remove_branch_state(the_repository, 0);
13261327
ret = finish_rebase(&options);
13271328
goto cleanup;
@@ -1828,10 +1829,13 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
18281829
strbuf_release(&revisions);
18291830
free(options.reflog_action);
18301831
free(options.head_name);
1832+
strvec_clear(&options.git_am_opts);
18311833
free(options.gpg_sign_opt);
18321834
free(options.cmd);
18331835
free(options.strategy);
18341836
strbuf_release(&options.git_format_patch_opt);
18351837
free(squash_onto_name);
1838+
string_list_clear(&exec, 0);
1839+
string_list_clear(&strategy_options, 0);
18361840
return !!ret;
18371841
}

builtin/repack.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -973,6 +973,7 @@ int cmd_repack(int argc, const char **argv, const char *prefix)
973973
item = string_list_append(&names, line.buf);
974974
item->util = populate_pack_exts(item->string);
975975
}
976+
strbuf_release(&line);
976977
fclose(out);
977978
ret = finish_command(&cmd);
978979
if (ret)
@@ -1175,7 +1176,6 @@ int cmd_repack(int argc, const char **argv, const char *prefix)
11751176
string_list_clear(&existing_nonkept_packs, 0);
11761177
string_list_clear(&existing_kept_packs, 0);
11771178
clear_pack_geometry(geometry);
1178-
strbuf_release(&line);
11791179

11801180
return 0;
11811181
}

0 commit comments

Comments
 (0)