From 083920ebd7abe287cd14661b7797b09d4785bf95 Mon Sep 17 00:00:00 2001 From: FredApps <153173854+FredApps@users.noreply.github.com> Date: Mon, 16 Nov 2026 13:19:08 +0100 Subject: [PATCH] psd: keep a layer record's blocks before an overlong one (#1845) --- crates/psd/src/layer.rs | 4 ++-- crates/psd/src/tagged.rs | 43 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 2 deletions(-) diff --git a/crates/psd/src/layer.rs b/crates/psd/src/layer.rs index e84b87e1d..eb1399f21 100644 --- a/crates/psd/src/layer.rs +++ b/crates/psd/src/layer.rs @@ -8,7 +8,7 @@ use crate::error::{PsdError, Result}; use crate::header::{MAX_DIMENSION, Version}; use crate::io::{Reader, Sink, WriteExt, append_with, decode_legacy_name, read_pascal, write_pascal}; use crate::source::Stored; -use crate::tagged::{BlockData, SectionDivider, SectionType, TaggedBlock, read_blocks, uses_long_length, write_blocks}; +use crate::tagged::{BlockData, SectionDivider, SectionType, TaggedBlock, read_record_blocks, uses_long_length, write_blocks}; /// Channel id of the transparency mask. pub const CHANNEL_TRANSPARENCY: i16 = -1; @@ -651,7 +651,7 @@ impl LayerRecord { let br_len = x.u32()?; let blending_ranges = BlendingRanges { data: x.bytes_u64(u64::from(br_len))?.to_vec() }; let name = read_pascal(&mut x, 4)?; - let (blocks, extra_trailing) = read_blocks(&mut x, version)?; + let (blocks, extra_trailing) = read_record_blocks(&mut x, version)?; Ok((LayerRecord { rect, channels, blend_mode, opacity, clipping, flags, filler, mask, blending_ranges, name, blocks, extra_trailing }, lens)) } diff --git a/crates/psd/src/tagged.rs b/crates/psd/src/tagged.rs index a934dad01..c8ec1789f 100644 --- a/crates/psd/src/tagged.rs +++ b/crates/psd/src/tagged.rs @@ -533,11 +533,28 @@ pub(crate) fn read_blocks(r: &mut Reader<'_>, version: Version) -> Result<(Vec, version: Version) -> Result<(Vec, Vec)> { + read_blocks_impl(r, version, 2, true, |_, _, _, _, _| false) +} + /// The callback consumes selected payloads before they are copied into owned blocks. pub(crate) fn read_blocks_with( r: &mut Reader<'_>, version: Version, alignment: usize, + consume: impl FnMut(usize, [u8; 4], [u8; 4], &[u8], &Option>) -> bool, +) -> Result<(Vec, Vec)> { + read_blocks_impl(r, version, alignment, false, consume) +} + +fn read_blocks_impl( + r: &mut Reader<'_>, + version: Version, + alignment: usize, + overlong_is_trailing: bool, mut consume: impl FnMut(usize, [u8; 4], [u8; 4], &[u8], &Option>) -> bool, ) -> Result<(Vec, Vec)> { let mut blocks = Vec::new(); @@ -548,6 +565,11 @@ pub(crate) fn read_blocks_with( r.skip(rest.len())?; return Ok((blocks, trailing)); } + if overlong_is_trailing && !block_fits(r, version) { + let trailing = rest.to_vec(); + r.skip(rest.len())?; + return Ok((blocks, trailing)); + } let signature = r.array::<4>()?; let key = r.array::<4>()?; let len = r.len_field(uses_long_length(version, &key))?; @@ -577,6 +599,13 @@ pub(crate) fn read_blocks_with( } } +/// Whether the block at `r` (signature and key already checked present) fits in what remains. +fn block_fits(r: &Reader<'_>, version: Version) -> bool { + let mut probe = r.clone(); + let Ok(key) = probe.skip(4).and_then(|()| probe.array::<4>()) else { return false }; + probe.len_field(uses_long_length(version, &key)).is_ok_and(|len| len <= probe.remaining() as u64) +} + pub(crate) fn write_blocks(out: &mut Vec, blocks: &[TaggedBlock], version: Version, alignment: usize) -> Result<()> { for b in blocks { b.write(out, version, alignment)?; @@ -766,4 +795,18 @@ mod tests { out.put(&[0; 4]); assert!(read_blocks(&mut Reader::new(&out), Version::Psd).is_err()); } + + #[test] + fn overlong_length_ends_a_records_blocks() { + let mut out = Vec::new(); + write_blocks(&mut out, &[TaggedBlock::layer_id(1)], Version::Psd, 2).unwrap(); + let damaged = out.len(); + out.put(b"8BIMabcd"); + out.put_u32(100); + out.put(&[0; 4]); + // The blocks before the damage survive; the damaged one and what follows stay as bytes. + let (blocks, trailing) = read_record_blocks(&mut Reader::new(&out), Version::Psd).unwrap(); + assert_eq!(blocks, vec![TaggedBlock::layer_id(1)]); + assert_eq!(trailing, out[damaged..]); + } }