Repository navigation
Harden the public API against trapping and invalid input - #117
Merged
Merged
Conversation
Trapping accessors - layerVisible(_:), layerSRS(_:), layerQueryable(_:) and layerVisible(_:atScaleDenominator:) used a plain Int32(index), which traps for indices outside Int32 range. They now return nil as documented. layerEnvelope, zoomToLayer and setLayerVisible already clamped; the four accessors now share the same helper. - Metatile.init, TileSequence.init and MetatileSequence.init threw away invalid input via precondition. They are now throwing and report MapnikError.invalidInput, so callers that build blocks or sequences from external data get an error instead of a crash. MetatileSequence uses an internal non-validating initializer for its own tile lists. - MetatileOptions.metatileSize/validate no longer trap on a huge width x scale. Silent truncation - srs and layerSRS grew the buffer instead of returning a silently truncated projection string. Long PROJ.4 strings (+towgs84, +nadgrids, +comment) exceed 255 characters easily. Validation - ImageFormat validates its options (png colors 1...256, jpeg/webp quality 0...100), so a bad value surfaces as MapnikError.invalidInput instead of a renderingFailed from deep inside mapnik after the map was already resized. - setBufferSize clamps negative values, matching RenderOptions.validate. Errors - MapnikError is Equatable and LocalizedError, and gained a serializationFailed case. - MapnikPoolError.exhausted carries the pool limit and is Equatable, so tests and logs have context. BoundingBox parsing - BoundingBox(parsing:) parses positionally and rejects non-finite values. It previously used compactMap, so "1,2,foo,3,4" was accepted as (1,2,3,4) with the bad field silently dropped and the rest shifted. Tests - New APIHardeningTests: huge layer indices, long SRS round trip, negative bufferSize, ImageFormat validation, error equality, Metatile/sequence validation instead of trapping, and malformed bounding-box strings. Fixes #7 Fixes #14 Fixes #29 Fixes #43 Fixes #65 Fixes #66 Fixes #83 Fixes #85 Fixes #92 Fixes #94 Fixes #103
Rebased onto main after #111-#116 landed. Conflicts were resolved by keeping main's newer logic and re-applying this PR's hardening on top: - MapnikPool keeps the unlocked-factory acquire from #112. - TileSequence/MetatileSequence keep main's bounds.isGeographic check, now thrown as MapnikError.invalidInput instead of a precondition. Trapping accessors - layerVisible(_:), layerSRS(_:), layerQueryable(_:) and layerVisible(_:atScaleDenominator:) used a plain Int32(index), which traps for indices outside Int32 range. They now return nil as documented. - Metatile.init, TileSequence.init and MetatileSequence.init used precondition for user-facing validation. They now throw MapnikError.invalidInput, so callers that build blocks or sequences from external data get an error instead of a crash. - MetatileOptions.metatileSize/validate no longer trap on a huge width x scale. Silent truncation - srs and layerSRS grow the buffer instead of returning a silently truncated projection string; long PROJ.4 strings exceed 255 characters. Validation - ImageFormat validates its options (png colors 1...256, jpeg/webp quality 0...100), so a bad value surfaces as MapnikError.invalidInput instead of a renderingFailed from deep inside mapnik. - setBufferSize clamps negative values, matching RenderOptions.validate. Errors - MapnikError is Equatable and LocalizedError, and gained a serializationFailed case. - MapnikPoolError.exhausted carries the pool limit and is Equatable. BoundingBox parsing - BoundingBox(parsing:) parses positionally and rejects non-finite values. It previously used compactMap, so "1,2,foo,3,4" was accepted as (1,2,3,4) with the bad field silently dropped. Tests - APIHardeningTests: huge layer indices, long SRS round trip, negative bufferSize, ImageFormat validation, error equality, Metatile/sequence validation instead of trapping, and malformed bounding-box strings.
The docs job builds with --warnings-as-errors, so the link to the internal validate() method failed the run. Describe the behaviour instead of linking.
trasch
added a commit
that referenced
this pull request
Nov 2, 2026
Rebased onto main after #117 landed. Conflicts were resolved by keeping main's newer render pipeline (withRenderState, the cairo resize helper) and re-applying this PR's additions on top. TilelistBuilder - tileCount counts unique tiles instead of additions, so it agrees with tilelist (which de-duplicates). Vector rendering - renderSVG/renderPDF/renderPostScript take optional width/height so the page size is explicit instead of depending on the map's previous size. The map size is restored afterwards. ImageFormat - typeString is public, so the documented mapnik type mapping is observable (and testable without @testable import). C shim - The cairo temp file honours TMPDIR via std::filesystem::temp_directory_path instead of hard-coding /tmp. - Removed the unused AGG include. Packaging - Package.resolved is no longer committed: for a library it only pins the repo's own builds and hides incompatibilities with newer gis-tools releases. Tests - tileCount uniqueness, vector page size (and that the map size is restored), default vector size, and every ImageFormat type string.
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.
Summary
Several public APIs trapped (crashed) on bad input, one silently truncated data, and
BoundingBox(parsing:)accepted malformed strings. This PR makes the failure modes explicit.Trapping accessors
Four accessors used a plain
Int32(index)while their neighbours usedInt32(clamping:). They now share alayerIndex(_:)helper and returnnilas documented:layerVisible(_:)nillayerSRS(_:)nillayerQueryable(_:)nillayerVisible(_:atScaleDenominator:)nilMetatile.init,TileSequence.initandMetatileSequence.initusedpreconditionfor user-facing validation. They now throwMapnikError.invalidInput, so a block or sequence built from external data (a tile list from a database, a zoom range from a URL) reports an error instead of crashing:Silent truncation
srsandlayerSRSused a fixed 256-byte buffer and ignored the shim's reported length:Long PROJ.4 strings (
+towgs84,+nadgrids,+comment) exceed 255 characters, and a round trip throughsetSRS→srscorrupted them. Both now grow the buffer once.Validation
ImageFormat's options were only checked deep inside mapnik, after the map had already been resized:They now throw
MapnikError.invalidInputup front (png colors 1...256,jpeg/webpquality0...100).setBufferSizeclamps negative values, matchingRenderOptions.validate.Errors
MapnikErrorisEquatableandLocalizedError, and gainedserializationFailed.MapnikPoolError.exhaustedcarries the pool limit and isEquatable, so tests can assert the specific error and logs have context.BoundingBox parsing
compactMapdropped unparseable fields, shifting the rest into the wrong slots. Parsing is now positional, rejects non-finite values, and requires exactly four fields.Testing
New
APIHardeningTestscovers huge layer indices, long SRS round trips, negativebufferSize,ImageFormatvalidation, error equality,Metatile/sequence validation instead of trapping, and malformed bounding-box strings. 88 tests pass across the affected suites.Breaking changes
Metatile.init,TileSequence.initandMetatileSequence.initare nowthrows.MapnikPoolError.exhaustedcarrieslimit:.MapnikErrorgained a case (exhaustive switches need updating).BoundingBox(parsing:)rejects strings it previously accepted.ImageFormatvalidation rejects out-of-range options earlier.Closes #7
Closes #14
Closes #29
Closes #43
Closes #65
Closes #66
Closes #83
Closes #85
Closes #92
Closes #94
Closes #103