Refactor: fuzz with a compression level that matches the method or is just past the edge

This commit is contained in:
Chris Hennick 2023-05-27 16:55:21 -07:00
parent 543f469277
commit 7e6472c761
No known key found for this signature in database
GPG key ID: 25653935CC8B6C74

View file

@ -156,7 +156,7 @@ impl arbitrary::Arbitrary<'_> for FileOptions {
fn arbitrary(u: &mut arbitrary::Unstructured) -> arbitrary::Result<Self> {
let mut options = FileOptions {
compression_method: CompressionMethod::arbitrary(u)?,
compression_level: Option::<i32>::arbitrary(u)?,
compression_level: None,
last_modified_time: DateTime::arbitrary(u)?,
permissions: Option::<u32>::arbitrary(u)?,
large_file: bool::arbitrary(u)?,
@ -167,10 +167,15 @@ impl arbitrary::Arbitrary<'_> for FileOptions {
#[cfg(feature = "deflate-zopfli")]
zopfli_buffer_size: None,
};
#[cfg(feature = "deflate-zopfli")]
if options.compression_method == Deflated
&& options.compression_level > Compression::best().level() {
options.zopfli_buffer_size = Some(1 << u.int_in_range(10..=30)?);
if options.compression_method == Deflated {
options.compression_level = Some(u.int_in_range(0..=265)?);
if options.compression_level > Compression::best().level() {
options.zopfli_buffer_size = Some(1 << u.int_in_range(9..=30)?);
}
} else if options.compression_method != Stored {
options.compression_level = Some(u.int_in_range(0..=10)?);
} else if bool::arbitrary(u) {
options.compression_level = Some(1);
}
u.arbitrary_loop(Some(0), Some((u16::MAX / 4) as u32), |u| {
options