Repository navigation
fix(render): count SVG marker images more tightly and fairly before reading them (#2713) - #2801
Conversation
…eading them (opengeos#2713) Part of opengeos#2713 (bounds on reading; the refusal phrases follow in their own PR). - A marker counts only on the vertices of the shapes that set it (by attribute, style or inheritance, a <use>'s copy included), or on all of them when a style sheet sets one: an icon with an unused marker is no longer refused. - Whether a style sets a marker is read with simplecss, as usvg reads it, so a class like .marker-dot sets none and a comment before the colon still counts. - Matching the style sheets' rules is bounded by the steps simplecss's own matcher takes (CSS_STEPS), on each <use> copy as usvg matches them, which also bounds descendant selectors deep down. The sheets are parsed once. - A gradient past two stops costs its stops at each pixel (STOPS). - An arc may come to at most 200 cubic curves (was 1,000). - A polyline's points are its pairs of numbers as svgtypes reads them. - A radius in % is resolved against the largest viewport; 0 in any unit is 0. - A subtree counted shallow is checked again where a <use> reaches it deeper.
|
No actionable comments were generated in the recent review. Walkthrough
Merge Risk:
|
Code reviewBugs
Security
Performance
Quality
AGENTS.md
Already raised
Inline comments posted:
I could not verify the external crate APIs (simplecss, svgtypes, roxmltree) because the cargo registry is not on disk, and I had no Bash access. Those points rest on reading the code. Change sizeCompared with
|
📖 Docs preview
GitHub Pages takes a minute or two to publish a new commit. Newer commits from the fork aren't published until a maintainer adds the |
Code reviewBugs. None found. I read the full diff, the helpers it touches, and the Security. None found. No new input path bypasses a bound, and the new arithmetic is saturating or checked against Performance. None found. Per-element CSS matching is bounded by the global step cap, and the other new passes are linear or bounded by the depth limit. Two points were already raised on earlier threads and answered by the author, so I did not repeat them: the linear attribute scan in Quality
AGENTS.md. No breaks found. The PR adds no command, no layer data copy, no asset, and no UI surface. The Prior threads. Two earlier concerns were left alone because the author answered them and the code they point at is unchanged: the attribute scan cost, and the sheet-sets-marker refusal. I did not re-raise either. Change sizeCompared with
|
Summary
SVG marker images are counted before they are read (
crates/render/src/picture/svg.rs, from #2704). This makes that count tighter against crafted SVGs and fairer to real icons, the bounds part of #2713. The refusal phrases, the other part, follow in their own PR.Fairer (valid icons that were drawn as a circle):
<marker>used to count on every vertex of the document, so an icon with an unused marker (editors keep arrowheads in<defs>) and about 5,000 points was refused. Now only a marker some marker property names (url(#id);nonenames none) counts (named_markers), and it counts on the vertices of the shapes that set a marker: by attribute, in astyle, inherited from what holds them, or through a<use>that copies them. When a style sheet sets a marker (aurl(#id)value, read as usvg reads it;marker: nonesets none), it may reach any shape, so every vertex counts, as before.marker,marker-start,marker-midandmarker-endset one (is_marker). A sheet such as.marker-dot { fill: red }therefore no longer makes two side-by-side markers refused as nested. A comment before the colon (marker-mid/**/:) still counts, as the reader takes it.<circle r="50%">,rx="10%") is resolved against the largest viewport anywhere in the SVG: its view boxes and the sizes of its<svg>,<symbol>and<use>elements (viewport), the root's % of 100 a side as usvg takes it. A nested size in %, em or ex may be any size, so while the SVG has one, every % radius in it is still refused, as all were before. A radius of 0 is 0 in any unit; one in em or ex stays refused.PointsParser). They used to be runs of digits, which counted12.345,67.891as two points.Tighter (crafted SVGs that took too long):
<use>makes (it reads what a<use>repeats again for each). simplecss tries each descendant selector (a b) against every element above, again for each descendant before it, sox g g g g g g recton 40 nested groups took 0.5 s, and deeper it doesn't finish. The count now runs simplecss's own matcher on each element, with each step counted (css_steps, anElementthat counts), each node walked back past to a previous sibling included (comments too, which usvg's own tree leaves out), and an attribute's value matched a step each 64 bytes. Matching stops once all the elements matched so far passCSS_STEPS(1,000,000, ~11 ms measured), and each<use>'s copy counts against that cap again. Each rule tried on an element is at least one step, so the cap is also about 200 class rules on 2,500 elements; marker icons come to thousands of steps. The sheets are parsed once for this and for the marker checks; usvg parses them again when it reads the SVG. This makessimplecss0.2.2, already in the lock file through usvg, a direct dependency ofrustgis-render.gradientstage). Measured: 2 stops take 21 ns a pixel, 1,000 stops 695 ns. Such a gradient now costscovered × stops / 16more (STOPS).<use>. A subtree counted where it is shallow was reused, without checking its levels again, where a<use>reached it deeper. Such an SVG passed the count and was then refused by usvg's own depth limit, with the wrong phrase. Each count now keeps its height, checked again wherever it is reached.Checked and left as they are:
usvg/src/parser/shapes.rs:50,quad_to); a test now pins it.<rect>'s points and a<use>link with spaces were already counted onmain.Testing
marker: nonein a sheet, an attribute or astyle, comments walked past by:first-child, a longclassmatched by many rules, a.marker-dotclass, a comment before a marker's colon, % and zero radii, the 200-curve arc, polyline numbers, the style sheet bound (many rules,<use>copies, deep descendants, the cap over all elements), gradient stops, depth through a reused subtree. Two tests pin what already held: quadratic points, and hostile geometry drawn without panicking.cargo fmt --all --check,cargo clippy --all-targets -- -D warnings,cargo nextest run --workspace(9122 passed; the local DuckDB test left out), doctests,cargo xtask layersandcargo xtask schema --check: all pass.cargo check -p rustgis-web --target wasm32-unknown-unknown: passes (simplecss is pure Rust, already built for the web app through usvg).simplecssis already listed in everyTHIRD-PARTY-LICENSES.txt(it came in through usvg), so they don't change.Surfaces
The count runs in
render's marker cache (picture::svg_in), which every way an image marker is set or read goes through: thelayer.marker,layer.lineStrokesandlayer.fillLayerscommands (desktop, browser, command line, Python, the Assistant and MCP, scripts and tasks alike), project and style loading, the QGIS and ArcGIS style importers, and the Image… picker. No command, parameter, control or undo step changes, so the ribbon, the phone and plugins see the same thing.UI evidence
Cities with an SVG marker whose rectangle has
rx="25%"and whose circle hasr="25%". Before (left,main): refused with "…this one is an SVG with more to draw than a 2048 × 2048 image", and drawn as circles. After (right): the icon is drawn.Related issue
Part of #2713 (the bounds on reading; the refusal phrases follow). Part of #2266.
Not in this PR
main(usvg parses the sheet). That is its own issue (SVG picture markers: a large style sheet takes tens of seconds to read #2800).@\69mport,\75rl(): simplecss 0.2.2 doesn't decode them and usvg never fetches an import, so nothing is lost or read; no change.Summary by CodeRabbit