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 a1bbc6c017 (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 21d4783538 (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 d4788af875 (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 482c119186 (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 3064d5a38c (mingw: fix t5601-clone.sh,
  2016-01-27). Let's free the "struct strbuf" as soon as we don't need
  it anymore.

1. c45f0f525d (switch: reject if some operation is in progress,
   2019-03-29)
2. 2708ce62d2 (branch: sort detached HEAD based on a flag,
   2021-01-07)
3. abcac2e19f (ref-filter.c: fix a leak in get_head_description,
   2022-09-25)
4. 962dd7ebc3 (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>
This commit is contained in:
Ævar Arnfjörð Bjarmason 2022-11-08 19:17:42 +01:00 committed by Junio C Hamano
parent 083fd1a264
commit b6046abc0c
10 changed files with 14 additions and 1 deletions

View File

@ -1470,6 +1470,8 @@ static void die_if_some_operation_in_progress(void)
"or \"git worktree add\".")); "or \"git worktree add\"."));
if (state.bisect_in_progress) if (state.bisect_in_progress)
warning(_("you are switching branch while bisecting")); warning(_("you are switching branch while bisecting"));
wt_status_state_free_buffers(&state);
} }
static int checkout_branch(struct checkout_opts *opts, static int checkout_branch(struct checkout_opts *opts,

View File

@ -1828,10 +1828,13 @@ cleanup:
strbuf_release(&revisions); strbuf_release(&revisions);
free(options.reflog_action); free(options.reflog_action);
free(options.head_name); free(options.head_name);
strvec_clear(&options.git_am_opts);
free(options.gpg_sign_opt); free(options.gpg_sign_opt);
free(options.cmd); free(options.cmd);
free(options.strategy); free(options.strategy);
strbuf_release(&options.git_format_patch_opt); strbuf_release(&options.git_format_patch_opt);
free(squash_onto_name); free(squash_onto_name);
string_list_clear(&exec, 0);
string_list_clear(&strategy_options, 0);
return !!ret; return !!ret;
} }

View File

@ -956,6 +956,7 @@ int cmd_repack(int argc, const char **argv, const char *prefix)
item = string_list_append(&names, line.buf); item = string_list_append(&names, line.buf);
item->util = populate_pack_exts(item->string); item->util = populate_pack_exts(item->string);
} }
strbuf_release(&line);
fclose(out); fclose(out);
ret = finish_command(&cmd); ret = finish_command(&cmd);
if (ret) 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_nonkept_packs, 0);
string_list_clear(&existing_kept_packs, 0); string_list_clear(&existing_kept_packs, 0);
clear_pack_geometry(geometry); clear_pack_geometry(geometry);
strbuf_release(&line);
return 0; return 0;
} }

View File

@ -530,6 +530,7 @@ static int cmd_parseopt(int argc, const char **argv, const char *prefix)
strbuf_addstr(&parsed, " --"); strbuf_addstr(&parsed, " --");
sq_quote_argv(&parsed, argv); sq_quote_argv(&parsed, argv);
puts(parsed.buf); puts(parsed.buf);
strbuf_release(&parsed);
return 0; return 0;
} }

View File

@ -1686,8 +1686,10 @@ static int do_push_stash(const struct pathspec *ps, const char *stash_msg, int q
} }
done: done:
strbuf_release(&patch);
free_stash_info(&info); free_stash_info(&info);
strbuf_release(&stash_msg_buf); strbuf_release(&stash_msg_buf);
strbuf_release(&untracked_files);
return ret; return ret;
} }

View File

@ -1358,6 +1358,7 @@ static void find_subpos(const char *buf,
/* parse signature first; we might not even have a subject line */ /* parse signature first; we might not even have a subject line */
parse_signature(buf, end - buf, &payload, &signature); parse_signature(buf, end - buf, &payload, &signature);
strbuf_release(&payload);
/* skip past header until we hit empty line */ /* skip past header until we hit empty line */
while (*buf && *buf != '\n') { while (*buf && *buf != '\n') {

View File

@ -17,6 +17,7 @@ int cmd_main(int argc, const char **argv)
f = fopen(buf.buf, "w"); f = fopen(buf.buf, "w");
if (!f) if (!f)
die("Could not write to %s", buf.buf); die("Could not write to %s", buf.buf);
strbuf_release(&buf);
for (i = 0; i < argc; i++) for (i = 0; i < argc; i++)
fprintf(f, "%s%s", i > 0 ? " " : "", i > 0 ? argv[i] : "ssh:"); fprintf(f, "%s%s", i > 0 ? " " : "", i > 0 ? argv[i] : "ssh:");
fprintf(f, "\n"); fprintf(f, "\n");

View File

@ -2,6 +2,7 @@
test_description='git rebase interactive environment' test_description='git rebase interactive environment'
TEST_PASSES_SANITIZE_LEAK=true
. ./test-lib.sh . ./test-lib.sh
test_expect_success 'setup' ' test_expect_success 'setup' '

View File

@ -5,6 +5,7 @@ test_description='git rebase --signoff
This test runs git rebase --signoff and make sure that it works. This test runs git rebase --signoff and make sure that it works.
' '
TEST_PASSES_SANITIZE_LEAK=true
. ./test-lib.sh . ./test-lib.sh
# A simple file to commit # A simple file to commit

View File

@ -2,6 +2,7 @@
test_description='git rebase across mode change' test_description='git rebase across mode change'
TEST_PASSES_SANITIZE_LEAK=true
. ./test-lib.sh . ./test-lib.sh
test_expect_success 'setup' ' test_expect_success 'setup' '