Coverage Report

Created: 2026-05-30 07:32

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/image/fuzz/fuzzers/fuzzer_script_exr.rs
Line
Count
Source
1
#![no_main]
2
#[macro_use]
3
extern crate libfuzzer_sys;
4
extern crate image;
5
6
use image::codecs::openexr::*;
7
use image::ExtendedColorType;
8
use image::ImageDecoder;
9
use image::ImageEncoder;
10
use image::ImageResult;
11
use image::Limits;
12
use std::io::{BufRead, Cursor, Seek, Write};
13
14
// "just dont panic"
15
0
fn roundtrip(bytes: &[u8]) -> ImageResult<()> {
16
    /// Read the file from the specified path into an `Rgba32FImage`.
17
    // TODO this method should probably already exist in the main image crate
18
0
    fn read_as_rgba_byte_image(read: impl BufRead + Seek) -> ImageResult<(u32, u32, Vec<u8>)> {
19
0
        let mut decoder = OpenExrDecoder::with_alpha_preference(read, Some(true))?;
20
0
        let prep = decoder.prepare_image()?;
21
0
        match usize::try_from(prep.total_bytes()) {
22
0
            Ok(decoded_size) if decoded_size <= 256 * 1024 * 1024 => {
23
0
                decoder.set_limits(Limits::default())?;
24
0
                let (width, height) = prep.layout.dimensions();
25
0
                let mut buffer = vec![0; decoded_size];
26
0
                decoder.read_image(buffer.as_mut_slice())?;
27
0
                Ok((width, height, buffer))
28
            }
29
0
            _ => Err(image::ImageError::Limits(
30
0
                image::error::LimitError::from_kind(
31
0
                    image::error::LimitErrorKind::InsufficientMemory,
32
0
                ),
33
0
            )),
34
        }
35
0
    }
Unexecuted instantiation: fuzzer_script_exr::roundtrip::read_as_rgba_byte_image::<std::io::cursor::Cursor<alloc::vec::Vec<u8>>>
Unexecuted instantiation: fuzzer_script_exr::roundtrip::read_as_rgba_byte_image::<std::io::cursor::Cursor<&[u8]>>
36
37
    /// Write an `Rgba32FImage`.
38
    /// Assumes the writer is buffered. In most cases,
39
    /// you should wrap your writer in a `BufWriter` for best performance.
40
    // TODO this method should probably already exist in the main image crate
41
0
    fn write_rgba_image(
42
0
        write: impl Write + Seek,
43
0
        (width, height, data): &(u32, u32, Vec<u8>),
44
0
    ) -> ImageResult<()> {
45
0
        OpenExrEncoder::new(write).write_image(
46
0
            data.as_slice(),
47
0
            *width,
48
0
            *height,
49
0
            ExtendedColorType::Rgba32F,
50
        )
51
0
    }
52
53
0
    let decoded_image = read_as_rgba_byte_image(Cursor::new(bytes))?;
54
55
    #[allow(clippy::disallowed_methods)]
56
0
    let mut bytes = Vec::with_capacity(bytes.len() + 20);
57
0
    write_rgba_image(Cursor::new(&mut bytes), &decoded_image)?;
58
59
0
    let redecoded_image = read_as_rgba_byte_image(Cursor::new(bytes))?;
60
61
    // if both images are valid, assert read/write consistency
62
0
    assert_eq!(
63
        decoded_image, redecoded_image,
64
0
        "image was valid but was not reproducible"
65
    );
66
0
    Ok(())
67
0
}
68
69
fuzz_target!(|data: &[u8]| {
70
    let _img = roundtrip(data); // fixme not optimized away?
71
});