Skip to content

Commit 03267e8

Browse files
avargitster
authored andcommitted
commit: discard partial cache before (re-)reading it
The read_cache() in prepare_to_commit() would end up clobbering the pointer we had for a previously populated "the_index.cache_tree" in the very common case of "git commit" stressed by e.g. the tests being changed here. We'd populate "the_index.cache_tree" by calling "update_main_cache_tree" in prepare_index(), but would not end up with a "fully prepared" index. What constitutes an existing index is clearly overly fuzzy, here we'll check "active_nr" (aka "the_index.cache_nr"), but our "the_index.cache_tree" might have been malloc()'d already. Thus the code added in 11c8a74 (commit: write cache-tree data when writing index anyway, 2011-12-06) would end up allocating the "cache_tree", and would interact here with code added in 7168624 (Do not generate full commit log message if it is not going to be used, 2007-11-28). The result was a very common memory leak. Signed-off-by: Ævar Arnfjörð Bjarmason <[email protected]> Signed-off-by: Taylor Blau <[email protected]>
1 parent ab2cf37 commit 03267e8

19 files changed

+29
-2
lines changed

builtin/commit.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -987,8 +987,11 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
987987
struct object_id oid;
988988
const char *parent = "HEAD";
989989

990-
if (!active_nr && read_cache() < 0)
991-
die(_("Cannot read index"));
990+
if (!active_nr) {
991+
discard_cache();
992+
if (read_cache() < 0)
993+
die(_("Cannot read index"));
994+
}
992995

993996
if (amend)
994997
parent = "HEAD^1";

t/t0068-for-each-repo.sh

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

33
test_description='git for-each-repo builtin'
44

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

78
test_expect_success 'run based on configured value' '

t/t0070-fundamental.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ test_description='check that the most basic functions work
66
Verify wrappers and compatibility functions.
77
'
88

9+
TEST_PASSES_SANITIZE_LEAK=true
910
. ./test-lib.sh
1011

1112
test_expect_success 'character classes (isspace, isalpha etc.)' '

t/t1404-update-ref-errors.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#!/bin/sh
22

33
test_description='Test git update-ref error handling'
4+
5+
TEST_PASSES_SANITIZE_LEAK=true
46
. ./test-lib.sh
57

68
# Create some references, perhaps run pack-refs --all, then try to

t/t1409-avoid-packing-refs.sh

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

33
test_description='avoid rewriting packed-refs unnecessarily'
44

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

78
# Add an identifying mark to the packed-refs file header line. This

t/t1413-reflog-detach.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ test_description='Test reflog interaction with detached HEAD'
44
GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
55
export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
66

7+
TEST_PASSES_SANITIZE_LEAK=true
78
. ./test-lib.sh
89

910
reset_state () {

t/t1501-work-tree.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#!/bin/sh
22

33
test_description='test separate work tree'
4+
5+
TEST_PASSES_SANITIZE_LEAK=true
46
. ./test-lib.sh
57

68
test_expect_success 'setup' '

t/t2025-checkout-no-overlay.sh

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

33
test_description='checkout --no-overlay <tree-ish> -- <pathspec>'
44

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

78
test_expect_success 'setup' '

t/t3009-ls-files-others-nonsubmodule.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ This test runs git ls-files --others with the following working tree:
1818
git repository with a commit and an untracked file
1919
'
2020

21+
TEST_PASSES_SANITIZE_LEAK=true
2122
. ./test-lib.sh
2223

2324
test_expect_success 'setup: directories' '

t/t3010-ls-files-killed-modified.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ Also for modification test, the cache and working tree have:
4141
We should report path0, path1, path2/file2, path3/file3, path7 and path8
4242
modified without reporting path9 and path10. submod1 is also modified.
4343
'
44+
45+
TEST_PASSES_SANITIZE_LEAK=true
4446
. ./test-lib.sh
4547

4648
test_expect_success 'git update-index --add to add various paths.' '

t/t4045-diff-relative.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#!/bin/sh
22

33
test_description='diff --relative tests'
4+
5+
TEST_PASSES_SANITIZE_LEAK=true
46
. ./test-lib.sh
57

68
test_expect_success 'setup' '

t/t4111-apply-subdir.sh

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

33
test_description='patching from inconvenient places'
44

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

78
test_expect_success 'setup' '

t/t4135-apply-weird-filenames.sh

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

33
test_description='git apply with weird postimage filenames'
44

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

78
test_expect_success 'setup' '

t/t4213-log-tabexpand.sh

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

33
test_description='log/show --expand-tabs'
44

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

78
HT=" "

t/t5618-alternate-refs.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#!/bin/sh
22

33
test_description='test handling of --alternate-refs traversal'
4+
5+
TEST_PASSES_SANITIZE_LEAK=true
46
. ./test-lib.sh
57

68
# Avoid test_commit because we want a specific and known set of refs:

t/t6301-for-each-ref-errors.sh

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

33
test_description='for-each-ref errors for broken refs'
44

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

78
ZEROS=$ZERO_OID

t/t7520-ignored-hook-warning.sh

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

33
test_description='ignored hook warning'
44

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

78
test_expect_success setup '

t/t7614-merge-signoff.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ This test runs git merge --signoff and makes sure that it works.
88
GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
99
export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
1010

11+
TEST_PASSES_SANITIZE_LEAK=true
1112
. ./test-lib.sh
1213

1314
# Setup test files

t/t9003-help-autocorrect.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#!/bin/sh
22

33
test_description='help.autocorrect finding a match'
4+
5+
TEST_PASSES_SANITIZE_LEAK=true
46
. ./test-lib.sh
57

68
test_expect_success 'setup' '

0 commit comments

Comments
 (0)