Coverage Report

Created: 2026-10-17 08:02

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
2.01k
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
2.08k
    fn read_as_rgba_byte_image(read: impl BufRead + Seek) -> ImageResult<(u32, u32, Vec<u8>)> {
19
2.08k
        let mut decoder = OpenExrDecoder::with_alpha_preference(read, Some(true))?;
20
901
        let prep = decoder.prepare_image()?;
21
901
        match usize::try_from(prep.total_bytes()) {
22
901
            Ok(decoded_size) if decoded_size <= 256 * 1024 * 1024 => {
23
889
                decoder.set_limits(Limits::default())?;
24
889
                let (width, height) = prep.layout.dimensions();
25
889
                let mut buffer = vec![0; decoded_size];
26
889
                decoder.read_image(buffer.as_mut_slice())?;
27
156
                Ok((width, height, buffer))
28
            }
29
12
            _ => Err(image::ImageError::Limits(
30
12
                image::error::LimitError::from_kind(
31
12
                    image::error::LimitErrorKind::InsufficientMemory,
32
12
                ),
33
12
            )),
34
        }
35
2.08k
    }
fuzzer_script_exr::roundtrip::read_as_rgba_byte_image::<std::io::cursor::Cursor<alloc::vec::Vec<u8>>>
Line
Count
Source
18
78
    fn read_as_rgba_byte_image(read: impl BufRead + Seek) -> ImageResult<(u32, u32, Vec<u8>)> {
19
78
        let mut decoder = OpenExrDecoder::with_alpha_preference(read, Some(true))?;
20
78
        let prep = decoder.prepare_image()?;
21
78
        match usize::try_from(prep.total_bytes()) {
22
78
            Ok(decoded_size) if decoded_size <= 256 * 1024 * 1024 => {
23
78
                decoder.set_limits(Limits::default())?;
24
78
                let (width, height) = prep.layout.dimensions();
25
78
                let mut buffer = vec![0; decoded_size];
26
78
                decoder.read_image(buffer.as_mut_slice())?;
27
78
                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
78
    }
fuzzer_script_exr::roundtrip::read_as_rgba_byte_image::<std::io::cursor::Cursor<&[u8]>>
Line
Count
Source
18
2.01k
    fn read_as_rgba_byte_image(read: impl BufRead + Seek) -> ImageResult<(u32, u32, Vec<u8>)> {
19
2.01k
        let mut decoder = OpenExrDecoder::with_alpha_preference(read, Some(true))?;
20
823
        let prep = decoder.prepare_image()?;
21
823
        match usize::try_from(prep.total_bytes()) {
22
823
            Ok(decoded_size) if decoded_size <= 256 * 1024 * 1024 => {
23
811
                decoder.set_limits(Limits::default())?;
24
811
                let (width, height) = prep.layout.dimensions();
25
811
                let mut buffer = vec![0; decoded_size];
26
811
                decoder.read_image(buffer.as_mut_slice())?;
27
78
                Ok((width, height, buffer))
28
            }
29
12
            _ => Err(image::ImageError::Limits(
30
12
                image::error::LimitError::from_kind(
31
12
                    image::error::LimitErrorKind::InsufficientMemory,
32
12
                ),
33
12
            )),
34
        }
35
2.01k
    }
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
78
    fn write_rgba_image(
42
78
        write: impl Write + Seek,
43
78
        (width, height, data): &(u32, u32, Vec<u8>),
44
78
    ) -> ImageResult<()> {
45
78
        OpenExrEncoder::new(write).write_image(
46
78
            data.as_slice(),
47
78
            *width,
48
78
            *height,
49
78
            ExtendedColorType::Rgba32F,
50
        )
51
78
    }
52
53
2.01k
    let decoded_image = read_as_rgba_byte_image(Cursor::new(bytes))?;
54
55
    #[allow(clippy::disallowed_methods)]
56
78
    let mut bytes = Vec::with_capacity(bytes.len() + 20);
57
78
    write_rgba_image(Cursor::new(&mut bytes), &decoded_image)?;
58
59
78
    let redecoded_image = read_as_rgba_byte_image(Cursor::new(bytes))?;
60
61
    // if both images are valid, assert read/write consistency
62
78
    assert_eq!(
63
        decoded_image, redecoded_image,
64
0
        "image was valid but was not reproducible"
65
    );
66
78
    Ok(())
67
2.01k
}
68
69
fuzz_target!(|data: &[u8]| {
70
    let _img = roundtrip(data); // fixme not optimized away?
71
});