Skip to content

Commit 7959bb6

Browse files
pks-tdscho
authored andcommitted
unpack-trees: detect mismatching number of cache-tree/index entries
Same as the preceding commit, we unconditionally dereference the index's cache entries depending on the number of cache-tree entries, which can lead to a segfault when the cache-tree is corrupted. Fix this bug. This also makes t4058 pass with the leak sanitizer enabled. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent ce5ee8d commit 7959bb6

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

t/t4058-diff-duplicates.sh

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
# that the diff output isn't wildly unreasonable.
1111

1212
test_description='test tree diff when trees have duplicate entries'
13+
14+
TEST_PASSES_SANITIZE_LEAK=true
1315
. ./test-lib.sh
1416

1517
# make_tree_entry <mode> <mode> <sha1>
@@ -143,11 +145,12 @@ test_expect_success 'reset --hard does not segfault' '
143145
test_grep "error: corrupted cache-tree has entries not present in index" err
144146
'
145147

146-
test_expect_failure 'git diff HEAD does not segfault' '
148+
test_expect_success 'git diff HEAD does not segfault' '
147149
git checkout base &&
148150
GIT_TEST_CHECK_CACHE_TREE=false &&
149151
git reset --hard &&
150-
test_might_fail git diff HEAD
152+
test_must_fail git diff HEAD 2>err &&
153+
test_grep "error: corrupted cache-tree has entries not present in index" err
151154
'
152155

153156
test_expect_failure 'can switch to another branch when status is empty' '

unpack-trees.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -808,6 +808,8 @@ static int traverse_by_cache_tree(int pos, int nr_entries, int nr_names,
808808

809809
if (!o->merge)
810810
BUG("We need cache-tree to do this optimization");
811+
if (nr_entries + pos > o->src_index->cache_nr)
812+
return error(_("corrupted cache-tree has entries not present in index"));
811813

812814
/*
813815
* Do what unpack_callback() and unpack_single_entry() normally

0 commit comments

Comments
 (0)