mirror of
https://github.com/storytold/photocraft.git
synced 2026-11-21 17:37:14 +00:00
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 <noreply@anthropic.com>
This commit is contained in:
1 parent
5f65579ecb
commit
dd57638d9e
1 file changed
+20
-5
@@ -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());
|
||||
|
||||
Reference in new issue
Block a user