Repository: image-rs/image (language: rust)

Issue:
Seemingly incorrect Avif bit depth assertion
I'm using Windows 11, Rust 1.76.0 latest stable and MSVC.

When trying to parse an Avif file that has a bit depth other than 8, this assertion is triggered.

C:\Users\user\.cargo\registry\src\index.crates.io-6f17d22bba15001f\image-0.24.8\src\codecs\avif\decoder.rs:59:9:
assertion `left == right` failed
  left: 10
 right: 8

I used these images to test 

8 bit depth: https://raw.githubusercontent.com/link-u/avif-sample-images/master/fox.profile1.8bpc.yuv444.avif
10 bit depth: https://raw.githubusercontent.com/link-u/avif-sample-images/master/fox.profile1.10bpc.yuv444.avif


It seems Avif supports 8, 10, and 12 bit depths.
https://aomediacodec.github.io/av1-avif/v1.1.0.html#biblio-av1:~:text=NOTE%3A%20%5BAV1%5D%20supports%203%20bit%20depths%3A%208%2C%2010%20and%2012%20bits

Also, I don't think there should be an assertion at all as I think a denial of service can happen by a malicious Avif file (that sets the bit depth to an unsupported amount)?

## Expected

File decodes normally

## Actual behaviour

panics from assertion

## Reproduction steps

let bytes = // open the Avif image in bytes
image::load_from_memory(bytes).unwrap();


Discussion / hints:
Yeah, at the moment we don't support 10 or 12 bit AVIF images. Agreed that this should be an "Unsupported" error rather than an assert until support is added though

Fix patch:
diff --git a/src/codecs/avif/decoder.rs b/src/codecs/avif/decoder.rs
index f8ff0c4410..1efe1443b5 100644
--- a/src/codecs/avif/decoder.rs
+++ b/src/codecs/avif/decoder.rs
@@ -9,7 +9,7 @@ use std::io::{self, Cursor, Read};
 use std::marker::PhantomData;
 use std::mem;
 
-use crate::error::{DecodingError, UnsupportedError, UnsupportedErrorKind};
+use crate::error::{DecodingError, ImageFormatHint, UnsupportedError, UnsupportedErrorKind};
 use crate::{ColorType, ImageDecoder, ImageError, ImageFormat, ImageResult};
 
 use dav1d::{PixelLayout, PlanarImageComponent};
@@ -56,7 +56,29 @@ impl<R: Read> AvifDecoder<R> {
             .map(|x| x.ok().unwrap_or_default())
             .map(|x| x.to_vec());
 
-        assert_eq!(picture.bit_depth(), 8);
+        match picture.bit_depth() {
+            8 => (),
+            10 | 12 => {
+                return ImageResult::Err(ImageError::Unsupported(
+                    UnsupportedError::from_format_and_kind(
+                        ImageFormatHint::Exact(ImageFormat::Avif),
+                        UnsupportedErrorKind::GenericFeature(format!(
+                            "Only 8 bit depth is supported but was {}",
+                            picture.bit_depth()
+                        )),
+                    ),
+                ))
+            }
+            _ => {
+                return ImageResult::Err(ImageError::Decoding(DecodingError::new(
+                    ImageFormatHint::Exact(ImageFormat::Avif),
+                    format!(
+                        "Avif format does not support {} bit depth",
+                        picture.bit_depth()
+                    ),
+                )))
+            }
+        };
         Ok(AvifDecoder {
             inner: PhantomData,
             picture,


Test patch: