image icon indicating copy to clipboard operation
image copied to clipboard

InsufficientMemory error while trying to decode

Open negezor opened this issue 3 years ago • 3 comments

This happens in random pngs, like this

Expected

Decoded png

Actual behaviour

Err(Limits(LimitError { kind: InsufficientMemory }))

Reproduction steps

Repository

use std::io::Read;

fn main() {
    let path_to_file = "./broken.png";

    let mut f = std::fs::File::open(path_to_file).expect("no file found");
    let metadata = std::fs::metadata(&path_to_file).expect("unable to read metadata");
    let mut buffer = vec![0; metadata.len() as usize];
    f.read(&mut buffer).expect("buffer overflow");

    let format = image::guess_format(&buffer).unwrap();

    let mut reader = image::io::Reader::with_format(std::io::Cursor::new(buffer), format);

    reader.no_limits();

    let im = reader.decode().unwrap();
}

negezor avatar Sep 13 '22 13:09 negezor

I also have that bug. Here's more info with wallpapers that fail https://github.com/danyspin97/wpaperd/issues/21#issue-1393401394

pm4rcin avatar Oct 19 '22 09:10 pm4rcin

This issue hasn't been fixed, please reopen.

danyspin97 avatar Nov 14 '22 10:11 danyspin97

I looked into this some more. Seems that the original image contains an 18MB (!) text chunk before the image data, and the png crate has a hardcoded limit for such chunks.

So, most large but "normal" PNGs should work with the new limits support, but unfortunately more work is required to handle this specific case

fintelia avatar Nov 15 '22 05:11 fintelia

@fintelia the issue was resolved https://github.com/danyspin97/wpaperd/issues/21#issuecomment-1994461074 and it works now.

pm4rcin avatar Mar 13 '24 14:03 pm4rcin