From 345433598c21c4e35f817a0cc01d023c949961d1 Mon Sep 17 00:00:00 2001 From: Daniel Lemire Date: Tue, 1 Oct 2024 13:53:35 -0400 Subject: [PATCH] adding roaring64 to fuzzer as a step toward solving issue 662 --- fuzz/croaring_fuzzer.c | 43 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) diff --git a/fuzz/croaring_fuzzer.c b/fuzz/croaring_fuzzer.c index 1e62749a9..4c652378a 100644 --- a/fuzz/croaring_fuzzer.c +++ b/fuzz/croaring_fuzzer.c @@ -20,7 +20,7 @@ #include "roaring/roaring.h" -int LLVMFuzzerTestOneInput(const char *data, size_t size) { +int bitmap32(const char *data, size_t size) { // We test that deserialization never fails. roaring_bitmap_t *bitmap = roaring_bitmap_portable_deserialize_safe(data, size); @@ -48,3 +48,44 @@ int LLVMFuzzerTestOneInput(const char *data, size_t size) { } return 0; } + +int bitmap64(const char *data, size_t size) { + // We test that deserialization never fails. + roaring64_bitmap_t *bitmap = + roaring64_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 (roaring64_bitmap_internal_validate(bitmap, &reason_failure)) { + // the bitmap is ok! + uint64_t cardinality = roaring64_bitmap_get_cardinality(bitmap); + + for (uint32_t i = 100; i < 1000; i++) { + if (!roaring64_bitmap_contains(bitmap, i)) { + cardinality++; + roaring64_bitmap_add(bitmap, i); + } + } + uint64_t new_cardinality = roaring64_bitmap_get_cardinality(bitmap); + if (cardinality != new_cardinality) { + printf("bug\n"); + exit(1); + } + } + roaring64_bitmap_free(bitmap); + } + return 0; +} +int LLVMFuzzerTestOneInput(const char *data, size_t size) { + int r; + r = bitmap32(data, size); + if (r) { + return r; + } + r = bitmap64(data, size); + if (r) { + return r; + } + return 0; +}