From 25abb43bde9e96dd99032b3c93cf19f12a099ca6 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sat, 14 Nov 2026 14:44:05 -0500 Subject: [PATCH] craft-psd: pin that a length not fitting 32 bits is refused, not truncated --- crates/psd/src/io.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/crates/psd/src/io.rs b/crates/psd/src/io.rs index d8aa4b51d..8ca8353b9 100644 --- a/crates/psd/src/io.rs +++ b/crates/psd/src/io.rs @@ -440,6 +440,18 @@ pub(crate) fn write_unicode_units(out: &mut Vec, units: &[u16]) { #[cfg(test)] mod tests { + /// A section length that does not fit the file's 32-bit field is refused, not truncated: + /// this is what keeps every `as u32` on a length inside a serialized file from ever wrapping, + /// and it is why an over-large PSD fails the write instead of producing a short length. + #[test] + fn a_length_that_does_not_fit_32_bits_is_refused() { + let mut out = Vec::new(); + assert!(out.put_len(u64::from(u32::MAX), false).is_ok(), "the largest 32-bit length fits"); + assert!(matches!(out.put_len(u64::from(u32::MAX) + 1, false), Err(PsdError::LimitExceeded(_)))); + // A PSB length field is 64 bits, so the same value is written there. + assert!(out.put_len(u64::from(u32::MAX) + 1, true).is_ok(), "PSB has room for it"); + } + use super::*; #[test]