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

Co-authored-by: Claude Opus 5.5 <[email protected]>
This commit is contained in:
Valentin KleinandClaude Opus 5.5 authored and GitHub committed 2026-10-12 06:28:09 -04:00
1 parent 04396dd85f
commit 7c77143b97
1 file changed
+15 -1
+15 -1
View File
@@ -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);