delta-islands: fix segfault when freeing island marks

In 647982bb71 (delta-islands: free island_marks and bitmaps, 2023-02-03)
we have introduced logic to free `island_marks` in order to reduce heap
memory usage in git-pack-objects(1). This commit is causing segfaults in
the case where this Git command does not load delta islands at all, e.g.
when reading object IDs from standard input. One such crash can be hit
when using repacking multi-pack-indices with delta islands enabled.

The root cause of this bug is that we unconditionally dereference the
`island_marks` variable even in the case where it is a `NULL` pointer,
which is fixed by making it conditional. Note that we still leave the
logic in place to set the pointer to `-1` to detect use-after-free bugs
even when there are no loaded island marks at all.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
Acked-by: Eric Wong <e@80x24.org>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Patrick Steinhardt 2023-02-16 11:29:48 +01:00 committed by Junio C Hamano
parent 647982bb71
commit 6eb095d787
2 changed files with 23 additions and 5 deletions

View File

@ -517,11 +517,13 @@ void free_island_marks(void)
{
struct island_bitmap *bitmap;
kh_foreach_value(island_marks, bitmap, {
if (!--bitmap->refcount)
free(bitmap);
});
kh_destroy_oid_map(island_marks);
if (island_marks) {
kh_foreach_value(island_marks, bitmap, {
if (!--bitmap->refcount)
free(bitmap);
});
kh_destroy_oid_map(island_marks);
}
/* detect use-after-free with a an address which is never valid: */
island_marks = (void *)-1;

View File

@ -1015,4 +1015,20 @@ test_expect_success 'complains when run outside of a repository' '
grep "not a git repository" err
'
test_expect_success 'repack with delta islands' '
git init repo &&
test_when_finished "rm -fr repo" &&
(
cd repo &&
test_commit first &&
git repack &&
test_commit second &&
git repack &&
git multi-pack-index write &&
git -c repack.useDeltaIslands=true multi-pack-index repack
)
'
test_done