Coverage Report

Created: 2025-07-18 06:49

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