PSD import: bound group nesting at 64 so thousands of nested groups cannot overflow the stack (#1128)
Psd::tree flattens groups nested deeper than MAX_GROUP_DEPTH into their depth-64 parent, keeping every layer, so the importer recursion and the tree's Drop/Clone/PartialEq are bounded. The import result reports how many groups were flattened. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Sonnet 5.5
parent
470ea7a3af
commit
4f9a04fcf5
+34
-3
@@ -306,6 +306,10 @@ impl Layer {
|
||||
}
|
||||
}
|
||||
|
||||
/// How deep groups nest in [`Psd::tree`]; deeper ones are flattened (a hostile file can nest
|
||||
/// thousands, and walking or dropping that tree would overflow the stack).
|
||||
pub const MAX_GROUP_DEPTH: usize = 64;
|
||||
|
||||
/// The layer tree, top of the stack first.
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub enum Node {
|
||||
@@ -742,13 +746,39 @@ impl Psd {
|
||||
Ok(vm)
|
||||
}
|
||||
|
||||
/// The layer tree, top of the stack first.
|
||||
/// The layer tree, top of the stack first. Groups nest at most [`MAX_GROUP_DEPTH`] deep (see
|
||||
/// [`Psd::tree_report`]).
|
||||
pub fn tree(&self) -> Vec<Node> {
|
||||
self.tree_report().0
|
||||
}
|
||||
|
||||
/// The layer tree plus the number of groups nested deeper than [`MAX_GROUP_DEPTH`]. Those
|
||||
/// are flattened into their deepest allowed ancestor: their children stay, in order, and
|
||||
/// the group record itself becomes a plain [`Node::Layer`], so no layer is lost and the
|
||||
/// tree (and its `Drop`, `Clone` and `PartialEq`) cannot recurse deeper than the limit.
|
||||
pub fn tree_report(&self) -> (Vec<Node>, usize) {
|
||||
let mut stack: Vec<Vec<Node>> = vec![vec![]];
|
||||
// Dividers seen while `MAX_GROUP_DEPTH` groups were already open.
|
||||
let mut flattened_open = 0usize;
|
||||
let mut flattened = 0usize;
|
||||
for l in &self.layers {
|
||||
match l.section {
|
||||
Section::Divider => stack.push(vec![]),
|
||||
Section::Divider => {
|
||||
if stack.len() > MAX_GROUP_DEPTH {
|
||||
flattened_open += 1;
|
||||
} else {
|
||||
stack.push(vec![]);
|
||||
}
|
||||
}
|
||||
Section::OpenFolder | Section::ClosedFolder => {
|
||||
if flattened_open > 0 {
|
||||
flattened_open -= 1;
|
||||
flattened += 1;
|
||||
if let Some(top) = stack.last_mut() {
|
||||
top.push(Node::Layer(l.index));
|
||||
}
|
||||
continue;
|
||||
}
|
||||
let children = if stack.len() > 1 { stack.pop().unwrap_or_default() } else { vec![] };
|
||||
if let Some(top) = stack.last_mut() {
|
||||
top.push(Node::Group { layer: l.index, children });
|
||||
@@ -767,6 +797,7 @@ impl Psd {
|
||||
top.extend(c);
|
||||
}
|
||||
}
|
||||
// Depth is at most `MAX_GROUP_DEPTH`, so recursion is bounded.
|
||||
fn rev(v: &mut Vec<Node>) {
|
||||
v.reverse();
|
||||
for n in v {
|
||||
@@ -777,7 +808,7 @@ impl Psd {
|
||||
}
|
||||
let mut out = stack.pop().unwrap_or_default();
|
||||
rev(&mut out);
|
||||
out
|
||||
(out, flattened)
|
||||
}
|
||||
|
||||
fn bytes_per_sample(&self) -> usize {
|
||||
|
||||
@@ -252,3 +252,69 @@ fn huge_layer_bounds_error_instead_of_allocating() {
|
||||
let p = Psd::parse(bytes).unwrap();
|
||||
assert!(matches!(p.layer_pixels(0, false), Err(Error::Unsupported(_))));
|
||||
}
|
||||
|
||||
/// `n` groups nested in each other, each holding one pixel layer (file order: bottom first).
|
||||
fn nested_groups_doc(n: usize) -> WDoc {
|
||||
let mut d = WDoc::new(4, 4);
|
||||
for _ in 0..n {
|
||||
d.layers.push(WLayer::group_end());
|
||||
}
|
||||
for i in 0..n {
|
||||
d.layers.push(WLayer::solid(&format!("L{i}"), Rect::new(0, 0, 1, 1), [1.0; 4]));
|
||||
d.layers.push(WLayer::group(&format!("G{i}"), true, *b"norm"));
|
||||
}
|
||||
d
|
||||
}
|
||||
|
||||
fn depth_of(nodes: &[Node]) -> usize {
|
||||
nodes.iter().map(|n| if let Node::Group { children, .. } = n { 1 + depth_of(children) } else { 0 }).max().unwrap_or(0)
|
||||
}
|
||||
|
||||
fn layer_indices(nodes: &[Node], out: &mut Vec<usize>) {
|
||||
for n in nodes {
|
||||
match n {
|
||||
Node::Layer(i) => out.push(*i),
|
||||
Node::Group { layer, children } => {
|
||||
out.push(*layer);
|
||||
layer_indices(children, out);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn deeply_nested_groups_are_flattened_at_the_depth_limit() {
|
||||
assert_eq!(MAX_GROUP_DEPTH, 64);
|
||||
let n = 70;
|
||||
let p = Psd::parse(write(&nested_groups_doc(n))).unwrap();
|
||||
let (t, flattened) = p.tree_report();
|
||||
assert_eq!(depth_of(&t), MAX_GROUP_DEPTH);
|
||||
assert_eq!(flattened, 6);
|
||||
// No layer or group record is lost, each appears exactly once (dividers are not nodes).
|
||||
let mut seen = vec![];
|
||||
layer_indices(&t, &mut seen);
|
||||
seen.sort_unstable();
|
||||
assert_eq!(seen, (n..p.layers.len()).collect::<Vec<_>>());
|
||||
// At the limit nothing is flattened.
|
||||
let (t, flattened) = Psd::parse(write(&nested_groups_doc(MAX_GROUP_DEPTH))).unwrap().tree_report();
|
||||
assert_eq!((depth_of(&t), flattened), (MAX_GROUP_DEPTH, 0));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn thousands_of_nested_groups_do_not_overflow_a_small_stack() {
|
||||
let bytes = write(&nested_groups_doc(5000));
|
||||
let h = std::thread::Builder::new()
|
||||
.stack_size(2 << 20)
|
||||
.spawn(move || {
|
||||
let p = Psd::parse(bytes).unwrap();
|
||||
let t = p.tree();
|
||||
let c = t.clone();
|
||||
assert_eq!(t, c);
|
||||
let (_, flattened) = p.tree_report();
|
||||
drop(t);
|
||||
drop(c);
|
||||
flattened
|
||||
})
|
||||
.unwrap();
|
||||
assert_eq!(h.join().unwrap(), 5000 - MAX_GROUP_DEPTH);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user