psd: keep a layer record's blocks before an overlong one (#1845)

This commit is contained in:
FredApps authored and GitHub committed 2026-11-16 07:19:08 -05:00
1 parent a80c0f25f1
commit 083920ebd7
2 files changed
+45 -2

No files matched your search

+2 -2
View File
@@ -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))
}
+43
View File
@@ -533,11 +533,28 @@ pub(crate) fn read_blocks(r: &mut Reader<'_>, version: Version) -> Result<(Vec<T
read_blocks_with(r, version, 2, |_, _, _, _, _| false)
}
/// [`read_blocks`] for a layer record's extra data, where a block whose length runs past the
/// record is damage: it and what follows are kept as trailing bytes (written back verbatim), so
/// the record keeps the blocks before it instead of failing, and with it every layer.
pub(crate) fn read_record_blocks(r: &mut Reader<'_>, version: Version) -> Result<(Vec<TaggedBlock>, Vec<u8>)> {
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<Vec<u8>>) -> bool,
) -> Result<(Vec<TaggedBlock>, Vec<u8>)> {
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<Vec<u8>>) -> bool,
) -> Result<(Vec<TaggedBlock>, Vec<u8>)> {
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<u8>, 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..]);
}
}