message: try_unwrap check + decode_size limit up to 256MB
diff: @@ -12,23 +12,18 @@ use std::convert::TryFrom;
  use std::io::Cursor;
  use std::io::Read;
  use std::io::Seek;
  -use std::io::SeekFrom;
  use std::io::Write;
  // "just dont panic"
  fn roundtrip(bytes: &[u8]) -> ImageResult<()> {
  /// Read the file from the specified path into an `Rgba32FImage`.
  // TODO this method should probably already exist in the main image crate
  - fn read_as_rgba_byte_image(read: &mut (impl Read + Seek)) -> ImageResult<(u32, u32, Vec<u8>)> {
  - let current_pos = read.stream_position()?;
  - let total_size = read.seek(SeekFrom::End(0))?;
  - let _ = read.seek(SeekFrom::Start(current_pos))?;
  -
  + fn read_as_rgba_byte_image(read: impl Read + Seek) -> ImageResult<(u32, u32, Vec<u8>)> {
  let decoder = OpenExrDecoder::with_alpha_preference(read, Some(true))?;
  let (width, height) = decoder.dimensions();
  - let decoded_size = usize::try_from(decoder.total_bytes()).unwrap();
  - if total_size - current_pos < decoded_size as u64 {
  + if let Ok(decoded_size) = usize::try_from(decoder.total_bytes()) {
  + if decoded_size > 256 * 1024 * 1024 {
  return Err(image::ImageError::Limits(
  image::error::LimitError::from_kind(
  image::error::LimitErrorKind::InsufficientMemory,
  @@ -39,6 +34,13 @@ fn roundtrip(bytes: &[u8]) -> ImageResult<()> {
  decoder.read_image(buffer.as_mut_slice())?;
  Ok((width, height, buffer))
  + } else {
  + Err(image::ImageError::Limits(
  + image::error::LimitError::from_kind(
  + image::error::LimitErrorKind::InsufficientMemory,
  + ),
  + ))
  + }
  }
  /// Write an `Rgba32FImage`.
  @@ -52,12 +54,12 @@ fn roundtrip(bytes: &[u8]) -> ImageResult<()> {
  OpenExrEncoder::new(write).write_image(data.as_slice(), *width, *height, ColorType::Rgba32F)
  }
  - let decoded_image = read_as_rgba_byte_image(&mut Cursor::new(bytes))?;
  + let decoded_image = read_as_rgba_byte_image(Cursor::new(bytes))?;
  let mut bytes = Vec::with_capacity(bytes.len() + 20);
  write_rgba_image(Cursor::new(&mut bytes), &decoded_image)?;
  - let redecoded_image = read_as_rgba_byte_image(&mut Cursor::new(bytes))?;
  + let redecoded_image = read_as_rgba_byte_image(Cursor::new(bytes))?;
  // if both images are valid, assert read/write consistency
  assert_eq!(
  