From 7888c863dffc9d55d349c76e4f0006149fe33e2d Mon Sep 17 00:00:00 2001 From: FredApps <153173854+FredApps@users.noreply.github.com> Date: Mon, 16 Nov 2026 13:51:42 +0100 Subject: [PATCH] svg: bound entity declaration scans and reset smooth controls after Z #778, #789. Comments and CDATA are skipped, and a declaration name is only scanned up to MAX_NAME+1, so a comment full of ` HashMap<&str, Vec<&str>> { let mut map: HashMap<&str, Vec<&str>> = HashMap::new(); - for (at, _) in svg.match_indices("") { + Some(n) => at + 1 + 3 + n + 3, + None => svg.len(), + }; continue; } - let Some(end) = rest.find(char::is_whitespace) else { continue }; - let (name, rest) = rest.split_at(end); - let rest = rest.trim_start(); - let Some(quote) = rest.chars().next().filter(|q| matches!(q, '"' | '\'')) else { continue }; - let Some(value) = rest.get(1..).and_then(|r| r.split_once(quote)).map(|(v, _)| v) else { continue }; - map.entry(name).or_default().push(value); + if let Some(body) = after.strip_prefix("![CDATA[") { + i = match body.find("]]>") { + Some(n) => at + 1 + 8 + n + 3, + None => svg.len(), + }; + continue; + } + if let Some(rest) = after.strip_prefix("!ENTITY") { + let rest = rest.trim_start(); + // A parameter entity (`%`) isn't referenced as `&name;`. + if rest.starts_with('%') { + i = at + 1; + continue; + } + // Bound the name scan: a comment full of ` MAX_NAME { + // A name longer than MAX_NAME cannot be referenced, but it must still be + // recorded so `entities_expand_past` can refuse the document. + map.entry(window).or_default().push(""); + } + i = at + 1; + continue; + } + i = at + 1; } map } @@ -99,7 +133,8 @@ pub fn entities_expand_past(svg: &str, most: usize) -> bool { /// an entity becomes elements where it is referenced, so what the text shows of the tree is not /// what a reader builds; drawing apps declare entities for namespace names only. A caller may /// refuse these outright instead of counting them ([`prescan`] counts them ten times). Text that -/// only looks like a declaration (in a comment) counts too. `<` is text, not markup. +/// only looks like a declaration in another entity's value counts too; comments are skipped. +/// `<` is text, not markup. pub fn entity_holds_markup(svg: &str) -> bool { declared(svg).values().flatten().any(|value| value.contains('<')) } @@ -396,6 +431,16 @@ mod tests { assert!(expansion_too_large(&long_name)); } + #[test] + fn entity_markers_in_a_comment_are_scanned_in_linear_time() { + // 100,000 `"); + assert!(!entities_expand_past(&svg, MAX_ENTITY_EXPANSION)); + } + fn svg(body: &str) -> String { format!(r#"{body}"#) } diff --git a/crates/svg/src/path_data.rs b/crates/svg/src/path_data.rs index 96d0db771..fc31fbaf2 100644 --- a/crates/svg/src/path_data.rs +++ b/crates/svg/src/path_data.rs @@ -214,6 +214,10 @@ pub fn parse_path(d: &str) -> Result> { } cur = start; cmd = 0; + // Z is not a cubic or quadratic, so a following S or T starts from the + // current point rather than reflecting the pre-close control. + prev_cubic = None; + prev_quad = None; continue; } cmd = c; @@ -434,6 +438,15 @@ mod tests { assert_eq!(paths[0].segs[1], Segment::Cubic(Point::new(8.0, -2.0), Point::new(10.0, -2.0), Point::new(12.0, 0.0))); } + #[test] + fn close_resets_smooth_control_history() { + // After Z, S must not reflect the preceding C's control (20,20) → (0,0). + let paths = parse_path("M10 10 C10 20 20 20 20 10 Z S30 0 30 10").unwrap(); + assert_eq!(paths.len(), 2); + assert!(paths[0].closed); + assert_eq!(paths[1].segs[0], Segment::Cubic(Point::new(10.0, 10.0), Point::new(30.0, 0.0), Point::new(30.0, 10.0))); + } + #[test] fn arc_centers_keep_radius_correction_and_sweep() { let (center, delta) = arc_center_circular(Point::new(0.0, 0.0), Point::new(10.0, 0.0), 2.0, false, true).unwrap();