From dd57638d9ecd6c88edfcfcd8d07af7f2f21960fc Mon Sep 17 00:00:00 2001 From: Finn Newick Date: Sat, 17 Oct 2026 10:11:57 +0100 Subject: [PATCH] fix(psd): make packbits::decode_into crate-private and overflow-safe decode_into computed out.len() + expected unchecked. With a non-empty buffer and an expected length near usize::MAX it panicked in debug builds and, in release, wrapped and returned Ok having decoded nothing. Compute the target with checked_add and report LimitExceeded, and test each packet as len > target - out.len() so the bound checks cannot overflow either. The only callers are decode and decode_rle in this module, and the crate README documents packbits::{encode, decode} as the public API, so decode_into becomes pub(crate). Co-Authored-By: Claude Opus 5.5 --- crates/psd/src/compression.rs | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/crates/psd/src/compression.rs b/crates/psd/src/compression.rs index aab785dff4..68782464ad 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) -> 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) -> 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::::new());