From 0148633267b601ca8cffdc56c16248c9923b4034 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Tue, 17 Nov 2026 16:35:18 -0500 Subject: [PATCH] Avoid recursive VectorDrawable adaptation and destruction --- crates/vectordrawable/src/xml.rs | 66 +++++++++++++++++-- crates/vectordrawable/src/xml_parity_tests.rs | 19 ++++++ 2 files changed, 78 insertions(+), 7 deletions(-) diff --git a/crates/vectordrawable/src/xml.rs b/crates/vectordrawable/src/xml.rs index 466e57fd6..40a13761c 100644 --- a/crates/vectordrawable/src/xml.rs +++ b/crates/vectordrawable/src/xml.rs @@ -25,7 +25,7 @@ pub(crate) fn color(c: Color) -> String { craft_vectordrawable::write::color(wire_color(c)) } -fn item(it: Item, depth: usize) -> wire::Item { +fn item(it: Item) -> wire::Item { let kind = match it.kind { ItemKind::Path(p) => wire::ItemKind::Path(wire::Path { name: p.name, @@ -62,17 +62,69 @@ fn item(it: Item, depth: usize) -> wire::Item { name: g.name, clip: g.clip, transform: g.xf.map(|x| wire::GroupTransform { translate: x.translate, rotation: x.rotation, scale: x.scale }), - // Bound adaptation as well as writing. Production trees are bounded by the source - // layer guard and generated wrappers; don't recurse beyond that invariant. - children: if depth < XML_DEPTH { g.children.into_iter().map(|it| item(it, depth + 1)).collect() } else { Vec::new() }, + children: Vec::new(), }), }; wire::Item { kind, bbox: it.bbox } } -pub(crate) fn document(width: f64, height: f64, items: Vec) -> String { - let items: Vec<_> = items.into_iter().map(|it| item(it, 1)).collect(); - craft_vectordrawable::write::document_with_depth_limit(width, height, &items, XML_DEPTH) +// Explicit iterator frames keep traversal scratch proportional to depth, not sibling count. +fn items(input: Vec) -> Vec { + let mut frames = vec![(input.into_iter(), Vec::new(), None::)]; + loop { + let depth = frames.len(); + let frame = frames.last_mut().unwrap(); + if let Some(mut source) = frame.0.next() { + let children = match &mut source.kind { + ItemKind::Group(g) => std::mem::take(&mut g.children), + ItemKind::Path(_) => Vec::new(), + }; + let mapped = item(source); + if !children.is_empty() && depth < XML_DEPTH { + frames.push((children.into_iter(), Vec::new(), Some(mapped))); + } else { + discard_items(children); + frame.1.push(mapped); + } + } else { + let (_, children, parent) = frames.pop().unwrap(); + let Some(mut parent) = parent else { return children }; + if let wire::ItemKind::Group(g) = &mut parent.kind { + g.children = children; + } + frames.last_mut().unwrap().1.push(parent); + } + } +} + +fn discard_items(items: Vec) { + let mut frames = vec![items.into_iter()]; + while let Some(frame) = frames.last_mut() { + if let Some(mut item) = frame.next() { + if let ItemKind::Group(g) = &mut item.kind { + frames.push(std::mem::take(&mut g.children).into_iter()); + } + } else { + frames.pop(); + } + } +} + +pub(crate) fn document(width: f64, height: f64, input: Vec) -> String { + let items = items(input); + let output = craft_vectordrawable::write::document_with_depth_limit(width, height, &items, XML_DEPTH); + // Recursive enum destruction also consumes stack, even with an iterative writer. + let mut frames = vec![items.into_iter()]; + while let Some(frame) = frames.last_mut() { + if let Some(mut item) = frame.next() { + if let wire::ItemKind::Group(g) = &mut item.kind { + frames.push(std::mem::take(&mut g.children).into_iter()); + } + } else { + frames.pop(); + } + } + output } #[cfg(test)] diff --git a/crates/vectordrawable/src/xml_parity_tests.rs b/crates/vectordrawable/src/xml_parity_tests.rs index 52b89de43..bb2d8aa99 100644 --- a/crates/vectordrawable/src/xml_parity_tests.rs +++ b/crates/vectordrawable/src/xml_parity_tests.rs @@ -134,3 +134,22 @@ fn mixed_siblings_nested_empty_and_clipped_groups_keep_closing_order() { ]; parity(48.0, 32.0, &items); } + +#[test] +fn over_depth_input_is_discarded_without_recursive_destruction() { + std::thread::Builder::new() + .stack_size(1024 * 1024) + .spawn(|| { + let mut leaf = path(Some(Fill::Solid(Color::BLACK)), "butt", "miter", Color::BLACK); + for _ in 0..XML_DEPTH * 3 { + leaf = Item { bbox: Rect::ZERO, kind: ItemKind::Group(GroupItem { name: None, xf: None, clip: None, children: vec![leaf] }) }; + } + let output = document(1.0, 1.0, vec![leaf]); + assert_eq!(output.matches("").count(), XML_DEPTH - 1); + assert!(!output.contains("