From d9f4b423eb5d0feddb4d9cd6f3221933a0a2adcd Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Sat, 7 Nov 2026 09:45:11 -0800 Subject: [PATCH] Prepress: keep catching panics around the linesweeper contour walk The contour walk returns an error for inconsistent winding numbers, but other degenerate input can still reach panics in linesweeper's walk and sweep. Arrangement::regions and nonzero catch them again, so a crafted path gives GeomError::Degenerate or keeps the even-odd path as it is, instead of ending the process. --- crates/prepress/src/flatten/geom.rs | 7 +++++-- crates/prepress/src/flatten/pdf.rs | 11 +++++++---- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/crates/prepress/src/flatten/geom.rs b/crates/prepress/src/flatten/geom.rs index c106bcd2ce..460c4a9bb2 100644 --- a/crates/prepress/src/flatten/geom.rs +++ b/crates/prepress/src/flatten/geom.rs @@ -412,9 +412,12 @@ impl Arrangement { } /// The contours of `top`'s faces for which `inside` holds. The contour walk returns an error when the -/// sweep left winding numbers that disagree around a point; that input is degenerate. +/// sweep left winding numbers that disagree around a point, and can still panic on other degenerate +/// input; either way that input is degenerate. fn contours(top: &Topology, inside: impl Fn(&Multi) -> bool) -> Result { - top.try_contours(inside).map_err(|_| GeomError::Degenerate) + std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| top.try_contours(inside))) + .map_err(|_| GeomError::Degenerate)? + .map_err(|_| GeomError::Degenerate) } /// A point as a map key (the arrangement's vertices are exact). diff --git a/crates/prepress/src/flatten/pdf.rs b/crates/prepress/src/flatten/pdf.rs index 845c114e3f..97263858b0 100644 --- a/crates/prepress/src/flatten/pdf.rs +++ b/crates/prepress/src/flatten/pdf.rs @@ -1272,21 +1272,24 @@ fn dash_pattern(path: &BezPath, dashes: &[f32]) -> Option> { } /// `path` filled by `rule`, as a path filled nonzero. An even-odd path is rebuilt from its -/// boundary; if that fails it is kept as it is. +/// boundary; if that fails or panics it is kept as it is. fn nonzero(path: &BezPath, rule: FillRule) -> BezPath { if rule == FillRule::NonZero { return path.clone(); } let closed = closed(path); - match linesweeper::binary_op(&closed, &BezPath::new(), linesweeper::FillRule::EvenOdd, linesweeper::BinaryOp::Union) { - Ok(contours) => { + let op = catch_unwind(AssertUnwindSafe(|| { + linesweeper::binary_op(&closed, &BezPath::new(), linesweeper::FillRule::EvenOdd, linesweeper::BinaryOp::Union) + })); + match op { + Ok(Ok(contours)) => { let mut out = BezPath::new(); for contour in contours.contours() { out.extend(contour.path.iter()); } out } - Err(_) => closed, + Ok(Err(_)) | Err(_) => closed, } }