Repository navigation
Fix polygon clipping crash on pole-reaching bounding boxes (#409) - #411
Merged
Merged
Conversation
Clipping a polygon to a box whose latitude reaches ±90° trapped in the overlay. A latitude of ±90° projects to infinity in the EPSG:3857 working frame, which made the overlay grid's cell size NaN and trapped on the subsequent Int conversion. Replace non-finite coordinates with the Web Mercator limit at the overlay entry point, before any geometry or grid math runs, so a polygon inside the box comes back unchanged. Only non-finite values are touched: finite coordinates keep their exact values, so geometry above the Mercator limit is unaffected in operations that do not involve a pole-reaching box. Because the fix lives in Overlay, it also fixes the same latent trap in union, intersection, difference and symmetricDifference, which share the choke point. Harden both overlay grids as well: the uniform cell index and the prepared-ring scanline index now refuse to build from non-finite bounds, and clamp their indices as Double before converting to Int (a huge but finite value traps just like NaN). The prepared-ring index was a second, independent trap site that only surfaced once the first stopped crashing. Adds regression tests for the issue's case, a south-pole-only box, the EPSG:4978 cross-projection path, and grid robustness against inf and NaN bounds.
…pole-crash # Conflicts: # CHANGELOG.md # Tests/GISToolsTests/Algorithms/BoundingBoxClipTests.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #409
Problem
Clipping a polygon to a box whose latitude reaches ±90° crashed:
The polygon is well inside the box, but the box's ±90° latitudes project to infinity in EPSG:3857, so the overlay grid's extent and cell size became NaN and the
Intconversion trapped.Root cause
Two independent trap sites, both fed by non-finite coordinates:
Overlay.PolygonIndex.Grid— the uniform cell index named in the issue's stack trace.cellHeightbecameNaN, thenInt((value - origin) / cellSize)trapped.OverlayPreparedRing— a second scanline index with the same unguardedInt(...)pattern. This one only surfaced once the first stopped crashing.Note that
Int(...)traps on huge-but-finite values too, not just NaN/infinity.Fix
Sources/GISTools/Algorithms/Overlay.swift:sanitizedForOverlayruns at the top ofoverlayPolygonsand replaces non-finite coordinates with ±GISTool.originShift. Finite values are untouched, so geometry above 85.05° keeps its exact values in operations that do not involve a pole-reaching box.Doublebefore converting toInt.PolygonIndexfalls back to a single cell holding every polygon, so containment tests stay correct rather than silently returningfalse.Because the fix lives in
Overlay, it also fixes the same latent crash inunion,intersection,differenceandsymmetricDifference, which share the choke point.Behaviour
A polygon inside a pole-reaching box now comes back unchanged (the issue's preferred outcome). No clamping is applied to finite geometry, so
union/intersectionof polygons between 85.05° and 90° still return their exact values.Tests
BoundingBoxClipTests: the issue's exact case, a south-pole-only box, and the EPSG:4978 cross-projection path.OverlayTests: grid robustness against infinite and NaN bounds, plus a check that the sanitizer clamps non-finite values and leaves finite polygons untouched.Verification
swift build— clean, no warningsswift test— 2707 tests / 175 suites passOverlay.swiftat 15, same as base;OverlayTests.swiftat 0)Note for reviewers
My first attempt at this turned the crash into a hang: I had written a test that fed the overlay a genuinely infinite polygon, and sanitizing it produced a 20-million-metre-tall shape that sent
poleOfInaccessibilityinto a multi-million-cell grid search. That test was replaced with ones that exercise the grid guards directly. The real-world case — a ±90° box against a normal polygon — is unaffected, since the box only spans the ordinary Mercator world height.