Address CodeRabbit review: patterns past the recursion cutoff count as too costly

usvg keeps a chain of single-use patterns past 200 levels and resvg draws
every one, so a costly filter at the end of the chain went uncharged. A
referenced root (pattern, mask, clip path, embedded drawing) the check
would skip at the cutoff now makes the drawing too costly; ordinary
children there still count as before.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
David MannandClaude Opus 5.5 committed 2026-10-25 15:06:22 +01:00
1 parent 1c595a34d9
commit 4ab5cdc3ad
2 files changed
+48 -1

No files matched your search

+17 -1
View File
@@ -492,7 +492,8 @@ fn count_nodes(root: &usvg::Group) -> usize {
/// the whole radius window per pixel, convolution the whole kernel, and turbulence runs every
/// octave (usvg doesn't clamp `numOctaves`). Masks, clip paths, patterns and embedded drawings
/// count too, at the scale of the element that uses them, as resvg renders them. Past `visits`
/// elements the drawing counts as too costly.
/// elements, or with a referenced root nested past [`MAX_RECURSION`], the drawing counts as too
/// costly.
fn filter_cost(root: &usvg::Group, base: tiny_skia::Transform, rec: usize, visits: &mut usize) -> u64 {
use usvg::filter::Kind;
let mut cost = 0u64;
@@ -524,6 +525,21 @@ fn filter_cost(root: &usvg::Group, base: tiny_skia::Transform, rec: usize, visit
stack.push(child);
}
if rec >= MAX_RECURSION {
// usvg keeps referenced roots past this depth and resvg draws them: one the check
// would skip makes the drawing too costly. Ordinary children still count.
let mut skipped = false;
match node {
usvg::Node::Path(p) => {
skipped = [p.fill().map(|f| f.paint()), p.stroke().map(|s| s.paint())]
.into_iter()
.flatten()
.any(|paint| matches!(paint, usvg::Paint::Pattern(_)))
}
_ => node.subroots(|_| skipped = true),
}
if skipped {
return u64::MAX;
}
continue;
}
let world = base.pre_concat(node.abs_transform());
+31
View File
@@ -476,6 +476,37 @@ fn patterns_nested_too_deep_are_refused_not_walked() {
assert!(!open(&chain(4)).document.layers.is_empty());
}
/// usvg keeps a chain of single-use patterns past the check's recursion cutoff and resvg draws
/// every one of them: a costly filter at the end of the chain is refused, not rendered unseen.
#[test]
fn a_costly_filter_past_the_pattern_recursion_cutoff_is_refused() {
let chain = |depth: usize, fe: &str| {
let defs: String = (0..depth)
.map(|i| {
let body = if i + 1 < depth {
format!("<rect width=\"500\" height=\"500\" fill=\"url(#p{})\"/>", i + 1)
} else {
"<rect width=\"500\" height=\"500\" fill=\"red\" filter=\"url(#f)\"/>".into()
};
format!("<pattern id=\"p{i}\" width=\"500\" height=\"500\" patternUnits=\"userSpaceOnUse\">{body}</pattern>")
})
.collect();
format!("<svg {NS} width=\"500\" height=\"500\"><filter id=\"f\">{fe}</filter>{defs}<rect width=\"500\" height=\"500\" fill=\"url(#p0)\"/></svg>")
};
// Just over the budget (about 3.5 billion reads), so a regression fails in seconds, not hours.
let fe = "<feMorphology operator=\"dilate\" radius=\"50\"/>";
assert!(import("x.svg", chain(4, fe).as_bytes()).is_err(), "the filter alone is over the budget");
let deep = chain(210, fe);
let e = import("x.svg", deep.as_bytes()).err().unwrap_or_else(|| panic!("opened a costly filter 210 patterns deep"));
assert!(matches!(&e, IoError::Svg(m) if m.contains("too costly")), "{e}");
assert!(svg::rasterize(deep.as_bytes(), 1.0).is_err());
// A short chain with an ordinary filter still opens, and so do groups nested past the cutoff.
assert!(!open(&chain(4, "<feGaussianBlur stdDeviation=\"2\"/>")).document.layers.is_empty());
let groups =
format!("<svg {NS} width=\"10\" height=\"10\">{}<rect width=\"10\" height=\"10\"/>{}</svg>", "<g opacity=\"0.99\">".repeat(250), "</g>".repeat(250));
assert!(!open(&groups).document.layers.is_empty());
}
#[test]
fn an_svg_round_trips_through_pcraft_as_shapes() {
let r = open(&format!(