heif: count the decoder's 16-bit buffer against max_alloc

This commit is contained in:
Dylan M. Taylor committed 2026-11-11 03:32:54 +00:00
1 parent a1d0126791
commit bd78e0c985
2 files changed
+10 -2

No files matched your search

+5 -2
View File
@@ -44,13 +44,16 @@ pub(crate) fn decode(bytes: &[u8], limits: &Limits, keep_orientation: bool) -> R
let layout = if info.has_alpha { ChannelLayout::Rgba } else { ChannelLayout::Rgb };
let sample = if info.bit_depth > 8 { SampleType::U16 } else { SampleType::U8 };
let (w, h) = if keep_orientation { (info.coded_width, info.coded_height) } else { (info.width, info.height) };
limits.check(w, h, layout, sample)?;
// craft-heif hands back 16-bit samples at any depth, and that buffer is still live while the
// output copy below is built, so the peak is both of them, not the output alone.
let peak = (layout.channels() * (2 + sample.bytes())) as u64;
limits.check_bytes(w, h, peak)?;
// The delegated decoder allocates the full coded image before applying any clean-aperture
// crop or orientation transform, so the coded dimensions must pass the limit policy even
// when the displayed (transformed) size is small. Checking only the transformed size let a
// tiny visible image hide a large coded allocation.
if !keep_orientation && (info.coded_width, info.coded_height) != (w, h) {
limits.check(info.coded_width, info.coded_height, layout, sample)?;
limits.check_bytes(info.coded_width, info.coded_height, peak)?;
}
let options = craft_heif::Options { max_pixels: limits.max_pixels, apply_transforms: !keep_orientation };
let decoded = craft_heif::decode(bytes, &options).map_err(err)?;
+5
View File
@@ -151,6 +151,11 @@ fn limits_are_checked_before_decoding() {
assert!(matches!(decode_with(checker_64(), &tight(few)), Err(CodecError::LimitExceeded(_))));
let bytes = Limits { max_alloc: 64 * 64 * 3 - 1, ..Default::default() };
assert!(matches!(decode_with(checker_64(), &tight(bytes)), Err(CodecError::LimitExceeded(_))));
// An 8-bit file peaks at the decoder's 16-bit buffer plus the 8-bit output: 3 bytes a sample.
let peak = 64 * 64 * 3 * 3;
let short = Limits { max_alloc: peak - 1, ..Default::default() };
assert!(matches!(decode_with(checker_64(), &tight(short)), Err(CodecError::LimitExceeded(_))));
decode_with(checker_64(), &tight(Limits { max_alloc: peak, ..Default::default() })).unwrap();
}
#[test]