Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

PSD: don't preallocate knots from untrusted path length counts (#3635) (#3654)

Valentin Klein7c77143b97d2
diff --git a/crates/psd/src/path.rs b/crates/psd/src/path.rs
index ea345b8ce..79f055cb0 100644
--- a/crates/psd/src/path.rs
+++ b/crates/psd/src/path.rs
@@ -155,7 +155,8 @@ impl PathData {
out.push(PsdSubpath {
closed: r.selector == selector::CLOSED_LENGTH,
operation: r.u16_at(2) as i16,
- knots: Vec::with_capacity(remaining.min(4096)),
+ // No preallocation: the count is untrusted (u16) and PsdKnot is 56 bytes.
+ knots: Vec::new(),
});
} else if r.is_knot() {
let closed = r.selector <= 2;
@@ -261,6 +262,19 @@ mod tests {
]
}
+ #[test]
+ fn subpaths_does_not_preallocate_from_untrusted_counts() {
+ let mut bytes = Vec::new();
+ for _ in 0..100_000 {
+ bytes.extend_from_slice(&selector::CLOSED_LENGTH.to_be_bytes());
+ bytes.extend_from_slice(&0xFFFFu16.to_be_bytes());
+ bytes.extend_from_slice(&[0u8; 22]);
+ }
+ let subs = PathData::from_bytes(&bytes).unwrap().subpaths();
+ assert_eq!(subs.len(), 100_000);
+ assert!(subs.iter().all(|s| s.knots.capacity() == 0));
+ }
+
#[test]
fn subpaths_roundtrip_and_bytes_are_stable() {
let p = PathData::from_subpaths(&sample(), true);