Skip to content

Commit b6046ab

Browse files
avargitster
authored andcommitted
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 a1bbc6c (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 21d4783 (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 d4788af (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 482c119 (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 3064d5a (mingw: fix t5601-clone.sh, 2016-01-27). Let's free the "struct strbuf" as soon as we don't need it anymore. 1. c45f0f5 (switch: reject if some operation is in progress, 2019-03-29) 2. 2708ce6 (branch: sort detached HEAD based on a flag, 2021-01-07) 3. abcac2e (ref-filter.c: fix a leak in get_head_description, 2022-09-25) 4. 962dd7e (wt-status: introduce wt_status_state_free_buffers(), 2020-09-27). Signed-off-by: Ævar Arnfjörð Bjarmason <[email protected]> Signed-off-by: Taylor Blau <[email protected]>
1 parent 083fd1a commit b6046ab

File tree

10 files changed

+14
-1
lines changed

10 files changed

+14
-1
lines changed

builtin/checkout.c

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

14751477
static int checkout_branch(struct checkout_opts *opts,

builtin/rebase.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1828,10 +1828,13 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
18281828
strbuf_release(&revisions);
18291829
free(options.reflog_action);
18301830
free(options.head_name);
1831+
strvec_clear(&options.git_am_opts);
18311832
free(options.gpg_sign_opt);
18321833
free(options.cmd);
18331834
free(options.strategy);
18341835
strbuf_release(&options.git_format_patch_opt);
18351836
free(squash_onto_name);
1837+
string_list_clear(&exec, 0);
1838+
string_list_clear(&strategy_options, 0);
18361839
return !!ret;
18371840
}

builtin/repack.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -956,6 +956,7 @@ int cmd_repack(int argc, const char **argv, const char *prefix)
956956
item = string_list_append(&names, line.buf);
957957
item->util = populate_pack_exts(item->string);
958958
}
959+
strbuf_release(&line);
959960
fclose(out);
960961
ret = finish_command(&cmd);
961962
if (ret)
@@ -1124,7 +1125,6 @@ int cmd_repack(int argc, const char **argv, const char *prefix)
11241125
string_list_clear(&existing_nonkept_packs, 0);
11251126
string_list_clear(&existing_kept_packs, 0);
11261127
clear_pack_geometry(geometry);
1127-
strbuf_release(&line);
11281128

11291129
return 0;
11301130
}

builtin/rev-parse.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -530,6 +530,7 @@ static int cmd_parseopt(int argc, const char **argv, const char *prefix)
530530
strbuf_addstr(&parsed, " --");
531531
sq_quote_argv(&parsed, argv);
532532
puts(parsed.buf);
533+
strbuf_release(&parsed);
533534
return 0;
534535
}
535536

builtin/stash.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1686,8 +1686,10 @@ static int do_push_stash(const struct pathspec *ps, const char *stash_msg, int q
16861686
}
16871687

16881688
done:
1689+
strbuf_release(&patch);
16891690
free_stash_info(&info);
16901691
strbuf_release(&stash_msg_buf);
1692+
strbuf_release(&untracked_files);
16911693
return ret;
16921694
}
16931695

ref-filter.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1358,6 +1358,7 @@ static void find_subpos(const char *buf,
13581358

13591359
/* parse signature first; we might not even have a subject line */
13601360
parse_signature(buf, end - buf, &payload, &signature);
1361+
strbuf_release(&payload);
13611362

13621363
/* skip past header until we hit empty line */
13631364
while (*buf && *buf != '\n') {

t/helper/test-fake-ssh.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ int cmd_main(int argc, const char **argv)
1717
f = fopen(buf.buf, "w");
1818
if (!f)
1919
die("Could not write to %s", buf.buf);
20+
strbuf_release(&buf);
2021
for (i = 0; i < argc; i++)
2122
fprintf(f, "%s%s", i > 0 ? " " : "", i > 0 ? argv[i] : "ssh:");
2223
fprintf(f, "\n");

t/t3409-rebase-environ.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
test_description='git rebase interactive environment'
44

5+
TEST_PASSES_SANITIZE_LEAK=true
56
. ./test-lib.sh
67

78
test_expect_success 'setup' '

t/t3428-rebase-signoff.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ test_description='git rebase --signoff
55
This test runs git rebase --signoff and make sure that it works.
66
'
77

8+
TEST_PASSES_SANITIZE_LEAK=true
89
. ./test-lib.sh
910

1011
# A simple file to commit

t/t3433-rebase-across-mode-change.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
test_description='git rebase across mode change'
44

5+
TEST_PASSES_SANITIZE_LEAK=true
56
. ./test-lib.sh
67

78
test_expect_success 'setup' '

0 commit comments

Comments
 (0)