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 <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Patrick Steinhardt committed
Oct 7, 2024 at 06:38 UTC
ecb5c4318cd8b5bbb72f943b6128fe946ffcb56e
2 files changed
+7
-2
t/t4058-diff-duplicates.sh
+5
-2
@@ -10,6 +10,8 @@
10
# that the diff output isn't wildly unreasonable.
11
12
test_description='test tree diff when trees have duplicate entries'
13
+
14
+TEST_PASSES_SANITIZE_LEAK=true
15
. ./test-lib.sh
16
17
# make_tree_entry <mode> <mode> <sha1>
@@ -143,11 +145,12 @@ test_expect_success 'reset --hard does not segfault' '
145
test_grep "error: corrupted cache-tree has entries not present in index" err
146
'
147
146
-test_expect_failure 'git diff HEAD does not segfault' '
148
+test_expect_success 'git diff HEAD does not segfault' '
149
git checkout base &&
150
GIT_TEST_CHECK_CACHE_TREE=false &&
151
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
154
'
155
156
test_expect_failure 'can switch to another branch when status is empty' '
unpack-trees.c
+2
@@ -808,6 +808,8 @@ static int traverse_by_cache_tree(int pos, int nr_entries, int nr_names,
808
809
if (!o->merge)
810
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"));
813
814
/*
815
* Do what unpack_callback() and unpack_single_entry() normally