From 4b658873b745cb5d941d58ccfd80f07cc49d79b9 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Mon, 16 Nov 2026 08:34:11 -0500 Subject: [PATCH 1/3] psd: delegate bounded recovery PackBits rows to craft-psd --- Cargo.lock | 2 +- Cargo.toml | 2 +- crates/psd/src/pixels.rs | 27 ++++++--------------------- crates/psd/src/tests.rs | 14 ++++++++++++++ 4 files changed, 22 insertions(+), 23 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ae7f294a7..a9c0fcf11 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1359,7 +1359,7 @@ dependencies = [ [[package]] name = "craft-psd" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=5a7a63a21dd03dd1a2bc6c308a3a6ce818caae87#5a7a63a21dd03dd1a2bc6c308a3a6ce818caae87" +source = "git+https://github.com/storytold/craft-libs?rev=55baef0c8d2180b50bcbd8f99a4e8d5bb2389dd6#55baef0c8d2180b50bcbd8f99a4e8d5bb2389dd6" dependencies = [ "encoding_rs", "flate2", diff --git a/Cargo.toml b/Cargo.toml index 21f47439c..94aae50b0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,7 +20,7 @@ craft-dpx = { version = "0.1.0", git = "https://github.com/storytold/craft-libs" craft-ps = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "808730f49b9637b7909fe2ae56f9d63fdb100cae", default-features = false, features = ["read"] } craft-exr = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "5a7a63a21dd03dd1a2bc6c308a3a6ce818caae87" } craft-gif = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "5a7a63a21dd03dd1a2bc6c308a3a6ce818caae87" } -craft-psd = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "5a7a63a21dd03dd1a2bc6c308a3a6ce818caae87" } +craft-psd = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "55baef0c8d2180b50bcbd8f99a4e8d5bb2389dd6" } craft-svg = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "5a7a63a21dd03dd1a2bc6c308a3a6ce818caae87" } craft-webp = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "5a7a63a21dd03dd1a2bc6c308a3a6ce818caae87" } craft-lottie = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "5a7a63a21dd03dd1a2bc6c308a3a6ce818caae87" } diff --git a/crates/psd/src/pixels.rs b/crates/psd/src/pixels.rs index 290a7b6f4..6a2bfc65a 100644 --- a/crates/psd/src/pixels.rs +++ b/crates/psd/src/pixels.rs @@ -482,27 +482,12 @@ enum PlaneStream { /// PackBits decoding of one row (appends exactly `row` bytes, zero-padded). pub(crate) fn unpack_bits(src: &[u8], row: usize, out: &mut Vec) -> Result<()> { - let start = out.len(); - let mut i = 0; - while i < src.len() && out.len() - start < row { - let n = src[i] as i8; - i += 1; - // A packet is written only up to the row's end, so the reserved plane never grows. - let room = row - (out.len() - start); - if n >= 0 { - let k = n as usize + 1; - let s = src.get(i..i + k).ok_or(Error::Truncated)?; - out.extend_from_slice(&s[..k.min(room)]); - i += k; - } else if n != -128 { - let k = (1 - n as isize) as usize; - let v = *src.get(i).ok_or(Error::Truncated)?; - out.extend(std::iter::repeat_n(v, k.min(room))); - i += 1; - } - } - out.resize(start + row, 0); - Ok(()) + // The production caller already charges/reserves the complete plane in Budget. + // Keep the shared recovery API bounded to this row; never relax the app budget. + craft_psd::compression::packbits::decode_padded_into(src, row, row, out).map_err(|error| match error { + craft_psd::PsdError::Decompress(_) => Error::Truncated, + other => Error::Invalid(other.to_string()), + }) } /// PackBits encoding of one row. diff --git a/crates/psd/src/tests.rs b/crates/psd/src/tests.rs index 22c575154..3930e9c53 100644 --- a/crates/psd/src/tests.rs +++ b/crates/psd/src/tests.rs @@ -520,3 +520,17 @@ fn a_failed_inflate_reports_its_status_not_its_output() { let Err(Error::Invalid(msg)) = crate::limits::inflate(&z, 1 << 21) else { panic!("the checksum fails") }; assert!(msg.len() < 100, "{} bytes", msg.len()); } + +#[test] +fn shared_packbits_keeps_padded_rows_and_truncated_packet_errors() { + let mut out = vec![42]; + unpack_bits(&[0, 7], 3, &mut out).unwrap(); + assert_eq!(out, [42, 7, 0, 0]); + unpack_bits(&[128], 2, &mut out).unwrap(); + assert_eq!(out, [42, 7, 0, 0, 0, 0]); + assert_eq!(unpack_bits(&[3, 1], 1, &mut vec![]), Err(Error::Truncated)); + assert_eq!(unpack_bits(&[255], 1, &mut vec![]), Err(Error::Truncated)); + let mut row = vec![]; + unpack_bits(&[0, 9, 255], 1, &mut row).unwrap(); + assert_eq!(row, [9]); +} From 9f2f4a314cdaa1f0edc9cd1cc892f675e1a83ade Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Mon, 16 Nov 2026 10:03:37 -0500 Subject: [PATCH 2/3] Pin merged shared PSD PackBits recovery API --- Cargo.lock | 2 +- Cargo.toml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e91297213..a26ae3284 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1359,7 +1359,7 @@ dependencies = [ [[package]] name = "craft-psd" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=25d2abeb737ccae9ae36ff9aea8af07027e9dbbd#25d2abeb737ccae9ae36ff9aea8af07027e9dbbd" +source = "git+https://github.com/storytold/craft-libs?rev=b774e4d1680a69ab0dc17bc437db93fe530cce36#b774e4d1680a69ab0dc17bc437db93fe530cce36" dependencies = [ "encoding_rs", "flate2", diff --git a/Cargo.toml b/Cargo.toml index a1520d0b8..63f457c41 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,7 +20,7 @@ craft-dpx = { version = "0.1.0", git = "https://github.com/storytold/craft-libs" craft-ps = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "808730f49b9637b7909fe2ae56f9d63fdb100cae", default-features = false, features = ["read"] } craft-exr = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "5a7a63a21dd03dd1a2bc6c308a3a6ce818caae87" } craft-gif = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "5a7a63a21dd03dd1a2bc6c308a3a6ce818caae87" } -craft-psd = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "25d2abeb737ccae9ae36ff9aea8af07027e9dbbd" } +craft-psd = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "b774e4d1680a69ab0dc17bc437db93fe530cce36" } craft-svg = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "5a7a63a21dd03dd1a2bc6c308a3a6ce818caae87" } craft-webp = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "5a7a63a21dd03dd1a2bc6c308a3a6ce818caae87" } craft-lottie = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "5a7a63a21dd03dd1a2bc6c308a3a6ce818caae87" } From 47384980a4bbf1e03ce94d8e06f9c5fa057295b1 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Wed, 18 Nov 2026 07:44:01 -0500 Subject: [PATCH 3/3] psd: cover the shared PackBits row-limit error mapping The recovery wrapper maps every non-decompress failure to Error::Invalid so a limit refusal is never reported as truncated data. That arm had no test, which left the changed lines under the coverage gate. --- crates/psd/src/tests.rs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/crates/psd/src/tests.rs b/crates/psd/src/tests.rs index e2b92b180..7fe3fe387 100644 --- a/crates/psd/src/tests.rs +++ b/crates/psd/src/tests.rs @@ -609,3 +609,10 @@ fn shared_packbits_keeps_padded_rows_and_truncated_packet_errors() { unpack_bits(&[0, 9, 255], 1, &mut row).unwrap(); assert_eq!(row, [9]); } + +#[test] +fn shared_packbits_reports_row_limits_as_invalid_not_truncated() { + // The shared recovery decoder refuses a row beyond its decoded-size limit. That refusal + // is a limit, not truncated PackBits data, so the wrapper must not report it as one. + assert!(matches!(unpack_bits(&[], usize::MAX, &mut Vec::new()), Err(Error::Invalid(_)))); +}