Skip to content

Commit

Permalink
Add deserialize fuzzer
Browse files Browse the repository at this point in the history
  • Loading branch information
Dr-Emann committed Oct 4, 2023
1 parent b4fc42c commit 807b810
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
6 changes: 6 additions & 0 deletions fuzz/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,9 @@ name = "against_bitvec"
path = "fuzz_targets/against_bitvec.rs"
test = false
doc = false

[[bin]]
name = "deserialize"
path = "fuzz_targets/deserialize.rs"
test = false
doc = false
29 changes: 29 additions & 0 deletions fuzz/fuzz_targets/deserialize.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#![no_main]

use croaring::{Bitmap, Native, Portable};
use libfuzzer_sys::fuzz_target;

fn check_bitmap<D: croaring::bitmap::Deserializer>(input: &[u8]) {
let bitmap = Bitmap::try_deserialize::<D>(input);
if let Some(mut bitmap) = bitmap {
bitmap.internal_validate().unwrap();

let start_cardinality = bitmap.cardinality();
let mut new_cardinality = start_cardinality;
for i in 100..1000 {
if !bitmap.contains(i) {
bitmap.add(i);
new_cardinality += 1;
}
}
assert_eq!(new_cardinality, bitmap.cardinality());

let unsafe_version = unsafe { D::try_deserialize_unchecked(input) };
assert_eq!(bitmap, unsafe_version);
}
}

fuzz_target!(|input: &[u8]| {
check_bitmap::<Portable>(input);
check_bitmap::<Native>(input);
});

0 comments on commit 807b810

Please sign in to comment.