]> git.ipfire.org Git - thirdparty/git.git/commitdiff
built-ins & libs & helpers: add/move destructors, fix leaks
authorÆvar Arnfjörð Bjarmason <avarab@gmail.com>
Tue, 8 Nov 2022 18:17:42 +0000 (19:17 +0100)
committerJunio C Hamano <gitster@pobox.com>
Mon, 21 Nov 2022 03:32:48 +0000 (12:32 +0900)
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>
builtin/checkout.c
builtin/rebase.c
builtin/repack.c
builtin/rev-parse.c
builtin/stash.c
ref-filter.c
t/helper/test-fake-ssh.c
t/t3409-rebase-environ.sh
t/t3428-rebase-signoff.sh
t/t3433-rebase-across-mode-change.sh

index 2a132392fbe7478c808b01d06039112fc321a55e..659dd5c43096944a20c71d83bbd67b38eeabb07e 100644 (file)
@@ -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,
index 4d6839a578587f813752f09f991cb5a71dce0eeb..6a831320313f90d648c104b725cfb74c654ede70 100644 (file)
@@ -1828,10 +1828,13 @@ cleanup:
        strbuf_release(&revisions);
        free(options.reflog_action);
        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;
 }
index 10e23f9ee1fc4baea34152c23ea8ab5459986ae5..fb3ac197426bd74e574e41ca2caca37314dce942 100644 (file)
@@ -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;
 }
index 8f61050bde884303fe1ac0fcb8162a99224bf5fd..e0244d63de67ad5b9316b07617ec5ac6c3c129e9 100644 (file)
@@ -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;
 }
 
index bb5485b40950032a8230c063a078012db28aeab6..8a64d564a195dca1eea01d9e5b8e34d4173b9dc4 100644 (file)
@@ -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;
 }
 
index 914908fac524e788cb362511deb0592e4948f62e..b40b76c380610130e6346015a5f71360e092f5aa 100644 (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(buf, end - buf, &payload, &signature);
+       strbuf_release(&payload);
 
        /* skip past header until we hit empty line */
        while (*buf && *buf != '\n') {
index 12beee99ad2f4e70e804b21895522d6d362929d2..d5e511633ab9a7fbb1b06fc38b2e2127da8f40bd 100644 (file)
@@ -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");
index 83ffb39d9ffdfd869ecb24e5adbbe11944a73c53..acaf5558dbe2e445efe2989e137b2f9ef5140aaf 100755 (executable)
@@ -2,6 +2,7 @@
 
 test_description='git rebase interactive environment'
 
+TEST_PASSES_SANITIZE_LEAK=true
 . ./test-lib.sh
 
 test_expect_success 'setup' '
index f6993b7e14d91617b337bfaefa717ec8591fb28f..e1b1e947647462f7cbc9fb2492b2cfe127056f1e 100755 (executable)
@@ -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
index 05df964670f49d011bf35b414177a35f812a4c4e..c8172b0852233b6f370eab47c6cc4877db54d8f7 100755 (executable)
@@ -2,6 +2,7 @@
 
 test_description='git rebase across mode change'
 
+TEST_PASSES_SANITIZE_LEAK=true
 . ./test-lib.sh
 
 test_expect_success 'setup' '