Skip to content

Commit e8d2713

Browse files
committed
Adjust tests for ImageReader limits usage
1 parent 46487ad commit e8d2713

File tree

3 files changed

+10
-6
lines changed

3 files changed

+10
-6
lines changed

fuzz/fuzzers/fuzzer_script_exr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ fn roundtrip(bytes: &[u8]) -> ImageResult<()> {
1717
// TODO this method should probably already exist in the main image crate
1818
fn read_as_rgba_byte_image(read: impl BufRead + Seek) -> ImageResult<(u32, u32, Vec<u8>)> {
1919
let mut decoder = OpenExrDecoder::with_alpha_preference(read, Some(true))?;
20-
match usize::try_from(decoder.total_bytes()) {
20+
match usize::try_from(decoder.peek_layout()?.total_bytes()) {
2121
Ok(decoded_size) if decoded_size <= 256 * 1024 * 1024 => {
2222
decoder.set_limits(Limits::default())?;
2323
let (width, height) = decoder.dimensions();

fuzz/fuzzers/fuzzer_script_tga.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ fuzz_target!(|data: &[u8]| {
88

99
fn decode(data: &[u8]) -> Result<(), image::ImageError> {
1010
use image::ImageDecoder;
11-
let decoder = image::codecs::tga::TgaDecoder::new(std::io::Cursor::new(data))?;
12-
if decoder.total_bytes() > 4_000_000 {
11+
let mut decoder = image::codecs::tga::TgaDecoder::new(std::io::Cursor::new(data))?;
12+
if decoder.peek_layout()?.total_bytes() > 4_000_000 {
1313
return Ok(());
1414
}
15-
let mut buffer = vec![0; decoder.total_bytes() as usize];
15+
let mut buffer = vec![0; decoder.peek_layout()?.total_bytes() as usize];
1616
decoder.read_image(&mut buffer)?;
1717
Ok(())
1818
}

tests/limits.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,10 @@ fn permissive_limits() -> Limits {
5151
let mut limits = Limits::no_limits();
5252
limits.max_image_width = Some(WIDTH);
5353
limits.max_image_height = Some(HEIGHT);
54-
limits.max_alloc = Some((WIDTH * HEIGHT * 5).into()); // `* 3`` would be an exact fit for RGB; `* 5`` allows some slack space
54+
// `* 3`` would be an exact fit for RGB;
55+
// `* 6` allows a duplicate buffer (ImageReader and internal)
56+
// `* 8` gives some slack for reserving half in ImageReader
57+
limits.max_alloc = Some((WIDTH * HEIGHT * 8).into());
5558
limits
5659
}
5760

@@ -153,7 +156,8 @@ fn tiff() {
153156
// so there is a copy from the buffer allocated by `tiff` to a buffer allocated by `image`.
154157
// This results in memory usage overhead the size of the output buffer.
155158
let mut tiff_permissive_limits = permissive_limits();
156-
tiff_permissive_limits.max_alloc = Some((WIDTH * HEIGHT * 10).into()); // `* 9` would be exactly three output buffers, `* 10`` has some slack space
159+
// `* 6` would be exactly two output buffers, `* 12`` accounts for ImageReader taking half.
160+
tiff_permissive_limits.max_alloc = Some((WIDTH * HEIGHT * 12).into());
157161
load_through_reader(&image, ImageFormat::Tiff, tiff_permissive_limits).unwrap();
158162

159163
// image::ImageReader

0 commit comments

Comments
 (0)