Skip to content

Commit

Permalink
Add WebP roundtrip fuzzing target (#2201)
Browse files Browse the repository at this point in the history
  • Loading branch information
Shnatsel committed Apr 11, 2024
1 parent dff0e20 commit 779c293
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
4 changes: 4 additions & 0 deletions fuzz/Cargo.toml
Expand Up @@ -65,3 +65,7 @@ path = "fuzzers/fuzzer_script_hdr.rs"
[[bin]]
name = "fuzzer_script_exr"
path = "fuzzers/fuzzer_script_exr.rs"

[[bin]]
name = "roundtrip_webp"
path = "fuzzers/roundtrip_webp.rs"
40 changes: 40 additions & 0 deletions fuzz/fuzzers/roundtrip_webp.rs
@@ -0,0 +1,40 @@
//! Verifies that any data encoded with WebP lossless encoder can be decoded,
//! and is unaltered after the encode-decode roundtrip.

#![no_main]

use std::io::Cursor;

use image::{ImageBuffer, Rgba};
#[macro_use] extern crate libfuzzer_sys;
extern crate image;

fuzz_target!(|data: &[u8]| {
if data.len() > 3 {
let width = data[0] as usize;
let height = data[1] as usize;
let content = &data[2..];

if width == 0 || height == 0 {
return;
}

let content_len = width * height * 4;
if content.len() >= content_len {
let content = content[..content_len].to_owned();
let image : ImageBuffer<Rgba<u8>, Vec<u8>> = ImageBuffer::from_vec(
width as u32, height as u32,
content
).unwrap();

let encoded: Vec<u8> = Vec::new();
let mut cursor = Cursor::new(encoded);
image.write_to(&mut cursor, image::ImageFormat::WebP).unwrap();
let encoded = cursor.into_inner();
// verify that the imade decoded without errors
let decoded = image::load_from_memory_with_format(&encoded, image::ImageFormat::WebP).unwrap();
// compare contents - the encoding should be lossless and roundtrip bit-perfectly
assert_eq!(image.into_vec(), decoded.into_bytes());
}
}
});

0 comments on commit 779c293

Please sign in to comment.