Skip to content

Commit 6eb095d

Browse files
pks-tgitster
authored andcommitted
delta-islands: fix segfault when freeing island marks
In 647982b (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 <[email protected]> Acked-by: Eric Wong <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 647982b commit 6eb095d

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

delta-islands.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -517,11 +517,13 @@ void free_island_marks(void)
517517
{
518518
struct island_bitmap *bitmap;
519519

520-
kh_foreach_value(island_marks, bitmap, {
521-
if (!--bitmap->refcount)
522-
free(bitmap);
523-
});
524-
kh_destroy_oid_map(island_marks);
520+
if (island_marks) {
521+
kh_foreach_value(island_marks, bitmap, {
522+
if (!--bitmap->refcount)
523+
free(bitmap);
524+
});
525+
kh_destroy_oid_map(island_marks);
526+
}
525527

526528
/* detect use-after-free with a an address which is never valid: */
527529
island_marks = (void *)-1;

t/t5319-multi-pack-index.sh

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1015,4 +1015,20 @@ test_expect_success 'complains when run outside of a repository' '
10151015
grep "not a git repository" err
10161016
'
10171017

1018+
test_expect_success 'repack with delta islands' '
1019+
git init repo &&
1020+
test_when_finished "rm -fr repo" &&
1021+
(
1022+
cd repo &&
1023+
1024+
test_commit first &&
1025+
git repack &&
1026+
test_commit second &&
1027+
git repack &&
1028+
1029+
git multi-pack-index write &&
1030+
git -c repack.useDeltaIslands=true multi-pack-index repack
1031+
)
1032+
'
1033+
10181034
test_done

0 commit comments

Comments
 (0)