PhotoCraft: An open-source, photo editor written by scratch in pure Rust
fix(psd): make packbits::decode_into crate-private and overflow-safe
Finn Newickdd57638d9ecd
diff --git a/crates/psd/src/compression.rs b/crates/psd/src/compression.rsindex aab785dff..68782464a 100644--- a/crates/psd/src/compression.rs+++ b/crates/psd/src/compression.rs@@ -111,9 +111,10 @@ pub mod packbits {////// Errors if the input is exhausted early or a packet would overflow the/// row. Trailing input after the row is complete is ignored except for- /// `-128` no-op bytes (which are always permitted).- pub fn decode_into(src: &[u8], expected: usize, out: &mut Vec<u8>) -> Result<()> {- let target = out.len() + expected;+ /// `-128` no-op bytes (which are always permitted). Errors, rather than+ /// overflowing, when `out.len() + expected` does not fit in `usize`.+ pub(crate) fn decode_into(src: &[u8], expected: usize, out: &mut Vec<u8>) -> Result<()> {+ let target = out.len().checked_add(expected).ok_or(PsdError::LimitExceeded("PackBits output length overflows usize"))?;let mut i = 0;while out.len() < target {let Some(&h) = src.get(i) else {@@ -126,7 +127,7 @@ pub mod packbits {let Some(lit) = src.get(i..i + len) else {return Err(PsdError::Decompress("PackBits literal truncated".into()));};- if out.len() + len > target {+ if len > target - out.len() {return Err(PsdError::Decompress("PackBits literal overflows row".into()));}out.extend_from_slice(lit);@@ -138,7 +139,7 @@ pub mod packbits {return Err(PsdError::Decompress("PackBits run truncated".into()));};i += 1;- if out.len() + len > target {+ if len > target - out.len() {return Err(PsdError::Decompress("PackBits run overflows row".into()));}out.extend(std::iter::repeat_n(b, len));@@ -687,6 +688,20 @@ mod tests {assert!(packbits::decode(&[3, 1, 2, 3, 4], 3).is_err()); // literal overflows}+ /// `out.len() + expected` past `usize::MAX` used to panic in debug builds and, in release,+ /// wrap and return `Ok` having decoded nothing.+ #[test]+ fn packbits_decode_into_output_length_overflow_is_an_error() {+ let mut out = vec![7u8];+ let r = packbits::decode_into(&[0x00, 0xaa], usize::MAX, &mut out);+ assert!(matches!(r, Err(PsdError::LimitExceeded(_))), "{r:?}");+ assert_eq!(out, [7]);+ // The largest `expected` that fits still decodes up to the input, then reports it short.+ let r = packbits::decode_into(&[0x00, 0xaa], usize::MAX - 1, &mut out);+ assert!(matches!(r, Err(PsdError::Decompress(_))), "{r:?}");+ assert_eq!(out, [7, 0xaa]);+ }+#[test]fn packbits_zero_expected_ignores_input() {assert_eq!(packbits::decode(&[0xff, 1], 0).unwrap(), Vec::<u8>::new());