Skip to content

Commit 00d8c31

Browse files
committed
commit: fix "author_ident" leak
Since 4c28e4a (commit: die before asking to edit the log message, 2010-12-20), we have been "leaking" the "author_ident" when prepare_to_commit() fails. Instead of returning from right there, introduce an exit status variable and jump to the clean-up label at the end. Instead of explicitly releasing the resource with strbuf_release(), mark the variable with UNLEAK() at the end, together with two other variables that are already marked as such. If this were in a utility function that is called number of times, but these are different, we should explicitly release resources that grow proportionally to the size of the problem being solved, but cmd_commit() is like main() and there is no point in spending extra cycles to release individual pieces of resource at the end, just before process exit will clean everything for us for free anyway. This fixes a leak demonstrated by e.g. "t3505-cherry-pick-empty.sh", but unfortunately we cannot mark it or other affected tests as passing now with "TEST_PASSES_SANITIZE_LEAK=true" as we'll need to fix many other memory leaks before doing so. Incidentally there are two tests that always passes the leak checker with or without this change. Mark them as such. This is based on an earlier patch by Ævar, but takes a different approach that is more maintainable. Signed-off-by: Ævar Arnfjörð Bjarmason <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 4c53a8c commit 00d8c31

File tree

3 files changed

+8
-3
lines changed

3 files changed

+8
-3
lines changed

builtin/commit.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1688,6 +1688,7 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
16881688
struct commit *current_head = NULL;
16891689
struct commit_extra_header *extra = NULL;
16901690
struct strbuf err = STRBUF_INIT;
1691+
int ret = 0;
16911692

16921693
if (argc == 2 && !strcmp(argv[1], "-h"))
16931694
usage_with_options(builtin_commit_usage, builtin_commit_options);
@@ -1722,8 +1723,9 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
17221723
running hooks, writing the trees, and interacting with the user. */
17231724
if (!prepare_to_commit(index_file, prefix,
17241725
current_head, &s, &author_ident)) {
1726+
ret = 1;
17251727
rollback_index_files();
1726-
return 1;
1728+
goto cleanup;
17271729
}
17281730

17291731
/* Determine parents */
@@ -1821,7 +1823,6 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
18211823
rollback_index_files();
18221824
die(_("failed to write commit object"));
18231825
}
1824-
strbuf_release(&author_ident);
18251826
free_commit_extra_headers(extra);
18261827

18271828
if (update_head_with_reflog(current_head, &oid, reflog_msg, &sb,
@@ -1862,7 +1863,9 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
18621863

18631864
apply_autostash(git_path_merge_autostash(the_repository));
18641865

1866+
cleanup:
1867+
UNLEAK(author_ident);
18651868
UNLEAK(err);
18661869
UNLEAK(sb);
1867-
return 0;
1870+
return ret;
18681871
}

t/t2203-add-intent.sh

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

33
test_description='Intent to add'
44

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

78
test_expect_success 'intent to add' '

t/t7011-skip-worktree-reading.sh

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

66
test_description='skip-worktree bit test'
77

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

1011
cat >expect.full <<EOF

0 commit comments

Comments
 (0)