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@xxxxxxxxx> --- builtin/checkout.c | 2 ++ builtin/rebase.c | 3 +++ builtin/repack.c | 2 +- builtin/rev-parse.c | 1 + builtin/stash.c | 2 ++ ref-filter.c | 1 + t/helper/test-fake-ssh.c | 1 + t/t3428-rebase-signoff.sh | 1 + 8 files changed, 12 insertions(+), 1 deletion(-) diff --git a/builtin/checkout.c b/builtin/checkout.c index 2a132392fbe..659dd5c4309 100644 --- a/builtin/checkout.c +++ b/builtin/checkout.c @@ -1470,6 +1470,8 @@ static void die_if_some_operation_in_progress(void) "or \"git worktree add\".")); if (state.bisect_in_progress) warning(_("you are switching branch while bisecting")); + + wt_status_state_free_buffers(&state); } static int checkout_branch(struct checkout_opts *opts, diff --git a/builtin/rebase.c b/builtin/rebase.c index 5d855fd8f51..64aed11c85d 100644 --- a/builtin/rebase.c +++ b/builtin/rebase.c @@ -1825,10 +1825,13 @@ int cmd_rebase(int argc, const char **argv, const char *prefix) strbuf_release(&buf); strbuf_release(&revisions); free(options.head_name); + strvec_clear(&options.git_am_opts); free(options.gpg_sign_opt); free(options.cmd); free(options.strategy); strbuf_release(&options.git_format_patch_opt); free(squash_onto_name); + string_list_clear(&exec, 0); + string_list_clear(&strategy_options, 0); return !!ret; } diff --git a/builtin/repack.c b/builtin/repack.c index 10e23f9ee1f..fb3ac197426 100644 --- a/builtin/repack.c +++ b/builtin/repack.c @@ -956,6 +956,7 @@ int cmd_repack(int argc, const char **argv, const char *prefix) item = string_list_append(&names, line.buf); item->util = populate_pack_exts(item->string); } + strbuf_release(&line); fclose(out); ret = finish_command(&cmd); if (ret) @@ -1124,7 +1125,6 @@ int cmd_repack(int argc, const char **argv, const char *prefix) string_list_clear(&existing_nonkept_packs, 0); string_list_clear(&existing_kept_packs, 0); clear_pack_geometry(geometry); - strbuf_release(&line); return 0; } diff --git a/builtin/rev-parse.c b/builtin/rev-parse.c index 8f61050bde8..e0244d63de6 100644 --- a/builtin/rev-parse.c +++ b/builtin/rev-parse.c @@ -530,6 +530,7 @@ static int cmd_parseopt(int argc, const char **argv, const char *prefix) strbuf_addstr(&parsed, " --"); sq_quote_argv(&parsed, argv); puts(parsed.buf); + strbuf_release(&parsed); return 0; } diff --git a/builtin/stash.c b/builtin/stash.c index bb5485b4095..8a64d564a19 100644 --- a/builtin/stash.c +++ b/builtin/stash.c @@ -1686,8 +1686,10 @@ static int do_push_stash(const struct pathspec *ps, const char *stash_msg, int q } done: + strbuf_release(&patch); free_stash_info(&info); strbuf_release(&stash_msg_buf); + strbuf_release(&untracked_files); return ret; } diff --git a/ref-filter.c b/ref-filter.c index 914908fac52..b40b76c3806 100644 --- a/ref-filter.c +++ b/ref-filter.c @@ -1358,6 +1358,7 @@ static void find_subpos(const char *buf, /* parse signature first; we might not even have a subject line */ parse_signature(buf, end - buf, &payload, &signature); + strbuf_release(&payload); /* skip past header until we hit empty line */ while (*buf && *buf != '\n') { diff --git a/t/helper/test-fake-ssh.c b/t/helper/test-fake-ssh.c index 12beee99ad2..d5e511633ab 100644 --- a/t/helper/test-fake-ssh.c +++ b/t/helper/test-fake-ssh.c @@ -17,6 +17,7 @@ int cmd_main(int argc, const char **argv) f = fopen(buf.buf, "w"); if (!f) die("Could not write to %s", buf.buf); + strbuf_release(&buf); for (i = 0; i < argc; i++) fprintf(f, "%s%s", i > 0 ? " " : "", i > 0 ? argv[i] : "ssh:"); fprintf(f, "\n"); diff --git a/t/t3428-rebase-signoff.sh b/t/t3428-rebase-signoff.sh index f6993b7e14d..e1b1e947647 100755 --- a/t/t3428-rebase-signoff.sh +++ b/t/t3428-rebase-signoff.sh @@ -5,6 +5,7 @@ test_description='git rebase --signoff This test runs git rebase --signoff and make sure that it works. ' +TEST_PASSES_SANITIZE_LEAK=true . ./test-lib.sh # A simple file to commit -- 2.38.0.1451.g86b35f4140a