tree-cache: Don't segfault upon corruption
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
diff --git a/src/tree-cache.c b/src/tree-cache.c
index a693f4f..57cc294 100644
--- a/src/tree-cache.c
+++ b/src/tree-cache.c
@@ -178,9 +178,12 @@ void git_tree_cache_free(git_tree_cache *tree)
if (tree == NULL)
return;
- for (i = 0; i < tree->children_count; ++i)
- git_tree_cache_free(tree->children[i]);
+ if (tree->children != NULL) {
+ for (i = 0; i < tree->children_count; ++i)
+ git_tree_cache_free(tree->children[i]);
+
+ git__free(tree->children);
+ }
- git__free(tree->children);
git__free(tree);
}
diff --git a/tests/index/tests.c b/tests/index/tests.c
index e520298..217e1bb 100644
--- a/tests/index/tests.c
+++ b/tests/index/tests.c
@@ -6,6 +6,7 @@ static const size_t index_entry_count_2 = 1437;
#define TEST_INDEX_PATH cl_fixture("testrepo.git/index")
#define TEST_INDEX2_PATH cl_fixture("gitgit.index")
#define TEST_INDEXBIG_PATH cl_fixture("big.index")
+#define TEST_INDEXBAD_PATH cl_fixture("bad.index")
/* Suite data */
@@ -535,3 +536,11 @@ void test_index_tests__reload_from_disk(void)
git_index_free(write_index);
git_repository_free(repo);
}
+
+void test_index_tests__corrupted_extension(void)
+{
+ /* sort the entires in an empty index */
+ git_index *index;
+
+ cl_git_fail_with(git_index_open(&index, TEST_INDEXBAD_PATH), GIT_ERROR);
+}
diff --git a/tests/resources/bad.index b/tests/resources/bad.index
new file mode 100644
index 0000000..5374654
Binary files /dev/null and b/tests/resources/bad.index differ