fix(avif): check alpha and primary picture dimensions match - #3098
Merged
Conversation
The primary and alpha pictures in an AVIF file are decoded by separate dav1d instances, so nothing guarantees they agree on size. read_image zipped the primary buffer against the alpha plane without checking this, so a malformed file with mismatched dimensions would silently truncate compositing to the shorter of the two instead of erroring on the malformed input. The 16-bit path had the same gap: it reshaped the alpha plane using the primary picture's width/height without confirming the alpha picture actually has those dimensions. Add a dimension check alongside the existing pixel_layout check in both the 8-bit and 16-bit compositing paths, returning a new AvifDecoderError::AlphaPlaneSize on mismatch. Fixes image-rs#3084
197g
approved these changes
Aug 5, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #3084. The primary and alpha pictures in an AVIF file are decoded by separate
dav1dinstances, so nothing guarantees they agree on size.read_imagezipped the primary buffer against the alpha plane without checking this — a malformed file with mismatched primary/alpha dimensions would silently truncate compositing to the shorter of the two, instead of erroring on the malformed input.The 16-bit path (
process_16bit_picture) had the same underlying gap in a different shape: it reshapes the alpha plane viatransmute_y_plane16using the primary picture'swidth/height, without ever confirming the alpha picture's actual dimensions match those.Fix
Added a dimension check alongside the existing
pixel_layoutcheck, in both the 8-bit and 16-bit alpha-compositing paths, returning a newAvifDecoderError::AlphaPlaneSize { alpha, primary }variant on mismatch instead of silently truncating.Test plan
cargo build --features avif -p imageandcargo test --features avif -p image— clean, existingtruncate_aviftest still passes, no regressioncargo fmt -p image -- --checkandcargo clippy --features avif -p image -- -D warnings— both cleanI was not able to add a dedicated regression test for the mismatch case itself: reproducing it requires a malformed AVIF container with two AV1 sub-images (primary + alpha) at different resolutions, and I didn't have a reliable way to construct or find such a fixture. Rather than fabricate something that doesn't actually exercise the mismatch path, I'm disclosing that gap here — happy to add one if pointed at an existing fixture-generation pattern for this codec, or if a maintainer can supply/generate a suitable malformed sample.
Disclosure
Per this org's CONTRIBUTING.md LLM policy: this PR was substantially developed with AI assistance (Claude), under my direction and review — I investigated the reported root cause myself, wrote/reviewed the fix, and ran the verification above locally before opening this.