Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keep capacity sane when creating a bitmap with a capacity #499

Merged
merged 2 commits into from
Aug 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/roaring_array.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,10 @@ bool ra_init_with_capacity(roaring_array_t *new_ra, uint32_t cap) {
if (!new_ra) return false;
ra_init(new_ra);

if (cap > INT32_MAX) { return false; }
// Containers hold 64Ki elements, so 64Ki containers is enough to hold `0x10000 * 0x10000` (all 2^32) elements
if (cap > 0x10000) {
cap = 0x10000;
}

if(cap > 0) {
void *bigalloc = roaring_malloc(cap *
Expand Down
8 changes: 8 additions & 0 deletions tests/toplevel_unit.c
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,13 @@ DEFINE_TEST(test_stats) {
roaring_bitmap_free(r1);
}

DEFINE_TEST(with_huge_capacity) {
roaring_bitmap_t *r = roaring_bitmap_create_with_capacity(UINT32_MAX);
assert_non_null(r);
assert_int_equal(r->high_low_container.allocation_size, (1 << 16));
roaring_bitmap_free(r);
}

// this should expose memory leaks
// (https://github.com/RoaringBitmap/CRoaring/pull/70)
void leaks_with_empty(bool copy_on_write) {
Expand Down Expand Up @@ -4623,6 +4630,7 @@ int main() {
cmocka_unit_test(test_silly_range),
cmocka_unit_test(test_uint32_iterator_true),
cmocka_unit_test(test_uint32_iterator_false),
cmocka_unit_test(with_huge_capacity),
cmocka_unit_test(leaks_with_empty_true),
cmocka_unit_test(leaks_with_empty_false),
cmocka_unit_test(test_bitmap_from_range),
Expand Down
Loading