Skip to content

Commit

Permalink
Caught a bug in roaring_bitmap_internal_validate (#516)
Browse files Browse the repository at this point in the history
* Caught a bug in roaring_bitmap_internal_validate

* Putting back static.

* Comments.

---------

Co-authored-by: Daniel Lemire <[email protected]>
  • Loading branch information
lemire and Daniel Lemire authored Sep 27, 2023
1 parent 1ade8ee commit 0b40505
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/containers/run.c
Original file line number Diff line number Diff line change
Expand Up @@ -710,7 +710,10 @@ bool run_container_validate(const run_container_t *run, const char **reason) {
*reason = "run start + length overflow";
return false;
}

if (end > (1<<16)) {
*reason = "run start + length too large";
return false;
}
if (start < last_end) {
*reason = "run start less than last end";
return false;
Expand Down
38 changes: 38 additions & 0 deletions tests/toplevel_unit.c
Original file line number Diff line number Diff line change
Expand Up @@ -4595,10 +4595,48 @@ DEFINE_TEST(convert_to_bitset) {
roaring_bitmap_free(r1);
}


bool deserialization_test(const char *data, size_t size) {
// We test that deserialization never fails.
roaring_bitmap_t *bitmap =
roaring_bitmap_portable_deserialize_safe(data, size);
if (bitmap) {
// The bitmap may not be usable if it does not follow the specification.
// We can validate the bitmap we recovered to make sure it is proper.
const char *reason_failure = NULL;
if (roaring_bitmap_internal_validate(bitmap, &reason_failure)) {
// the bitmap is ok!
uint32_t cardinality = roaring_bitmap_get_cardinality(bitmap);

for (uint32_t i = 100; i < 1000; i++) {
if (!roaring_bitmap_contains(bitmap, i)) {
cardinality++;
roaring_bitmap_add(bitmap, i);
}
}

uint32_t new_cardinality = roaring_bitmap_get_cardinality(bitmap);
if (cardinality != new_cardinality) {
return false;
}
}
roaring_bitmap_free(bitmap);
}
return true;
}

DEFINE_TEST(robust_deserialization) {
assert_true(deserialization_test(NULL, 0));
// contains a run container that overflows the 16-bit boundary.
const char test1[] = "\x3b\x30\x00\x00\x01\x00\x00\xfa\x2e\x01\x00\x00\x02\xff\xff";
assert_true(deserialization_test(test1, sizeof(test1)));
}

int main() {
tellmeall();

const struct CMUnitTest tests[] = {
cmocka_unit_test(robust_deserialization),
cmocka_unit_test(issue457),
cmocka_unit_test(convert_to_bitset),
cmocka_unit_test(issue440),
Expand Down

0 comments on commit 0b40505

Please sign in to comment.