diff --git a/include/roaring/roaring_types.h b/include/roaring/roaring_types.h index 9d5160892..3a8a61894 100644 --- a/include/roaring/roaring_types.h +++ b/include/roaring/roaring_types.h @@ -7,6 +7,7 @@ #include #include + #include #ifdef __cplusplus diff --git a/src/roaring64.c b/src/roaring64.c index d41507b3c..94c4e0e85 100644 --- a/src/roaring64.c +++ b/src/roaring64.c @@ -1123,7 +1123,7 @@ bool roaring64_bitmap_intersect_with_range(const roaring64_bitmap_t *r, } roaring64_iterator_t it; roaring64_iterator_init_at(r, &it, /*first=*/true); - if (!roaring64_iterator_move_equalorlarger(&it, min)) { + if (!it.has_value || !roaring64_iterator_move_equalorlarger(&it, min)) { return false; } return roaring64_iterator_has_value(&it) && diff --git a/tests/roaring64_unit.cpp b/tests/roaring64_unit.cpp index cad17d46a..acf11bcfa 100644 --- a/tests/roaring64_unit.cpp +++ b/tests/roaring64_unit.cpp @@ -1053,6 +1053,11 @@ DEFINE_TEST(test_intersect) { DEFINE_TEST(test_intersect_with_range) { roaring64_bitmap_t* r = roaring64_bitmap_create(); + // Empty bitmap never intersects + assert_false(roaring64_bitmap_intersect_with_range(r, 0, 0)); + assert_false(roaring64_bitmap_intersect_with_range(r, 0, 50000)); + assert_false(roaring64_bitmap_intersect_with_range(r, 0, UINT64_MAX)); + roaring64_bitmap_add(r, 50000); roaring64_bitmap_add(r, 100000); roaring64_bitmap_add(r, 100001); @@ -1065,6 +1070,13 @@ DEFINE_TEST(test_intersect_with_range) { assert_true(roaring64_bitmap_intersect_with_range(r, 50001, 100001)); assert_false(roaring64_bitmap_intersect_with_range(r, 300001, UINT64_MAX)); + // Empty ranges never intersect + assert_false(roaring64_bitmap_intersect_with_range(r, 0, 0)); + assert_false( + roaring64_bitmap_intersect_with_range(r, UINT64_MAX, UINT64_MAX)); + assert_false(roaring64_bitmap_intersect_with_range(r, UINT64_MAX, 0)); + assert_false(roaring64_bitmap_intersect_with_range(r, 50000, 50000)); + roaring64_bitmap_free(r); }