built-ins & libs & helpers: add/move destructors, fix leaks

Fix various leaks in built-ins, libraries and a test helper here we were missing a call to strbuf_release(), string_list_clear() etc, or were calling them after a potential "return". Comments on individual changes: - builtin/checkout.c: Fix a memory leak that was introduced in [1]. A sibling leak introduced in [2] was recently fixed in [3]. As with [3] we should be using the wt_status_state_free_buffers() API introduced in [4]. - builtin/repack.c: Fix a leak that's been here since this use of "strbuf_release()" was added in a1bbc6c0176 (repack: rewrite the shell script in C, 2013-09-15). We don't use the variable for anything except this loop, so we can instead free it right afterwards. - builtin/rev-parse: Fix a leak that's been here since this code was added in 21d47835386 (Add a parseopt mode to git-rev-parse to bring parse-options to shell scripts., 2007-11-04). - builtin/stash.c: Fix a couple of leaks that have been here since this code was added in d4788af875c (stash: convert create to builtin, 2019-02-25), we strbuf_release()'d only some of the "struct strbuf" we allocated earlier in the function, let's release all of them. - ref-filter.c: Fix a leak in 482c1191869 (gpg-interface: improve interface for parsing tags, 2021-02-11), we don't use the "payload" variable that we ask parse_signature() to populate for us, so let's free it. - t/helper/test-fake-ssh.c: Fix a leak that's been here since this code was added in 3064d5a38c7 (mingw: fix t5601-clone.sh, 2016-01-27). Let's free the "struct strbuf" as soon as we don't need it anymore. 1. c45f0f525de (switch: reject if some operation is in progress, 2019-03-29) 2. 2708ce62d21 (branch: sort detached HEAD based on a flag, 2021-01-07) 3. abcac2e19fa (ref-filter.c: fix a leak in get_head_description, 2022-09-25) 4. 962dd7ebc3e (wt-status: introduce wt_status_state_free_buffers(), 2020-09-27). 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 b6046abc0c27134d52c56699a88d1fadfe7e66ce
10 files changed +14 -1
builtin/checkout.c
+2
@@ -1470,6 +1470,8 @@ static void die_if_some_operation_in_progress(void)
1470 "or \"git worktree add\"."));
1471 if (state.bisect_in_progress)
1472 warning(_("you are switching branch while bisecting"));
1473 +
1474 + wt_status_state_free_buffers(&state);
1475 }
1476
1477 static int checkout_branch(struct checkout_opts *opts,
builtin/rebase.c
+3
@@ -1828,10 +1828,13 @@ cleanup:
1828 strbuf_release(&revisions);
1829 free(options.reflog_action);
1830 free(options.head_name);
1831 + strvec_clear(&options.git_am_opts);
1832 free(options.gpg_sign_opt);
1833 free(options.cmd);
1834 free(options.strategy);
1835 strbuf_release(&options.git_format_patch_opt);
1836 free(squash_onto_name);
1837 + string_list_clear(&exec, 0);
1838 + string_list_clear(&strategy_options, 0);
1839 return !!ret;
1840 }
builtin/repack.c
+1 -1
@@ -956,6 +956,7 @@ int cmd_repack(int argc, const char **argv, const char *prefix)
956 item = string_list_append(&names, line.buf);
957 item->util = populate_pack_exts(item->string);
958 }
959 + strbuf_release(&line);
960 fclose(out);
961 ret = finish_command(&cmd);
962 if (ret)
@@ -1124,7 +1125,6 @@ int cmd_repack(int argc, const char **argv, const char *prefix)
1125 string_list_clear(&existing_nonkept_packs, 0);
1126 string_list_clear(&existing_kept_packs, 0);
1127 clear_pack_geometry(geometry);
1127 - strbuf_release(&line);
1128
1129 return 0;
1130 }
builtin/rev-parse.c
+1
@@ -530,6 +530,7 @@ static int cmd_parseopt(int argc, const char **argv, const char *prefix)
530 strbuf_addstr(&parsed, " --");
531 sq_quote_argv(&parsed, argv);
532 puts(parsed.buf);
533 + strbuf_release(&parsed);
534 return 0;
535 }
536
builtin/stash.c
+2
@@ -1686,8 +1686,10 @@ static int do_push_stash(const struct pathspec *ps, const char *stash_msg, int q
1686 }
1687
1688 done:
1689 + strbuf_release(&patch);
1690 free_stash_info(&info);
1691 strbuf_release(&stash_msg_buf);
1692 + strbuf_release(&untracked_files);
1693 return ret;
1694 }
1695
ref-filter.c
+1
@@ -1358,6 +1358,7 @@ static void find_subpos(const char *buf,
1358
1359 /* parse signature first; we might not even have a subject line */
1360 parse_signature(buf, end - buf, &payload, &signature);
1361 + strbuf_release(&payload);
1362
1363 /* skip past header until we hit empty line */
1364 while (*buf && *buf != '\n') {
t/helper/test-fake-ssh.c
+1
@@ -17,6 +17,7 @@ int cmd_main(int argc, const char **argv)
17 f = fopen(buf.buf, "w");
18 if (!f)
19 die("Could not write to %s", buf.buf);
20 + strbuf_release(&buf);
21 for (i = 0; i < argc; i++)
22 fprintf(f, "%s%s", i > 0 ? " " : "", i > 0 ? argv[i] : "ssh:");
23 fprintf(f, "\n");
t/t3409-rebase-environ.sh
+1
@@ -2,6 +2,7 @@
2
3 test_description='git rebase interactive environment'
4
5 +TEST_PASSES_SANITIZE_LEAK=true
6 . ./test-lib.sh
7
8 test_expect_success 'setup' '
t/t3428-rebase-signoff.sh
+1
@@ -5,6 +5,7 @@ test_description='git rebase --signoff
5 This test runs git rebase --signoff and make sure that it works.
6 '
7
8 +TEST_PASSES_SANITIZE_LEAK=true
9 . ./test-lib.sh
10
11 # A simple file to commit
t/t3433-rebase-across-mode-change.sh
+1
@@ -2,6 +2,7 @@
2
3 test_description='git rebase across mode change'
4
5 +TEST_PASSES_SANITIZE_LEAK=true
6 . ./test-lib.sh
7
8 test_expect_success 'setup' '