Skip to content

LightCraft: An open-source non-destructive photo editor written by scratch in pure Rust

Merge remote-tracking branch 'origin/main' into fix/2119-erf-mef-strip-length

Michael Andersonab3f6af82a49
diff --cc crates/raw/src/vendor/mef.rs
index 30993ad72,d5c5d83b4..d1c9a7754
--- a/crates/raw/src/vendor/mef.rs
+++ b/crates/raw/src/vendor/mef.rs
@@@ -87,12 -87,15 +87,19 @@@ pub(crate) fn decode(bytes: &[u8], mode
None => Rect::new(0, 0, w, h),
};
// the white balance is measured from the samples, so the header probe reads them too
+ // the strip length is the tag value, so a short file must fail before the sample buffer is sized from it
let chunks = info.chunks(bytes.len() as u64);
- let src = chunks.first().and_then(|c| chunk_bytes(bytes, c)).ok_or_else(|| RawError::Corrupt("Mef strip outside file".into()))?;
+ let src = chunks
+ .first()
+ .and_then(|c| chunk_bytes(bytes, c).filter(|s| s.len() as u64 == c.len))
+ .ok_or_else(|| RawError::Corrupt("Mef strip outside file".into()))?;
+ // 12-bit MSB packing takes 3 bytes per 2 samples; a strip that cannot hold the declared
+ // samples is a truncated file, not a picture — unpacking it would run past the real bytes
+ // through every output sample (a short strip asks for the whole allocation and more).
+ let need = n.div_ceil(2).saturating_mul(3);
+ if src.len() < need {
+ return Err(RawError::Corrupt(format!("a Mef strip of {} bytes cannot hold the declared {n} samples", src.len())));
+ }
let mut d = vec![0u16; n];
crate::unpack::unpack_msb(src, 12, &mut d);
let white = super::white_from_data(&d, 12);