maps: degrade corrupt POI blocks instead of crashing
- Commit
5321d5695b16c91a7f22b60e0f8771dab2251b69- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-19 02:11 UTC (17 days ago)
- Parents
da3be06
PoiBlockSection inflates planet5 block-compressed sections lazily, on first touch. Load-time validation (PoiBytes.of / readAttrs / readRegion, all runCatching) only inflates the blocks it touches, so a corrupt DEFLATE block deeper in poi_attrs or poi_region was not discovered until query time -- and there it escaped as an IllegalArgumentException (or IndexOutOfBoundsException from the block section's own bounds check). Throw paths: - PoiIndex.attributesAt -> decodeAttrRecord -> PoiBytes.get/bytes -> block(), reached from osmPlace on a place tap / search select. Its own doc already promised a truncated file reads as 'no attributes, not throw out of a tap'. - PoiIndex.Mapped.regionId -> region.getLong, reached from every Mapped.record() built by searchByName / nearest / inViewport, so a corrupt region block crashed ordinary POI searches. Fix: attributesAt now routes through the existing bufferFailure degradation (null on IndexOutOfBounds/IllegalArgument, anything else still propagates), and regionId does the same per-read (returns REGION_NONE) plus rejects ordinals outside the index. No behaviour change for valid archives or raw (uncompressed) sections, whose reads were already bounds-checked. Verified: baseline ./gradlew :maps:testDevUnitTest passed before the change; module test + ./check maps re-run after it (see session report).
Diff
maps/src/main/java/com/vayunmathur/maps/data/PoiIndex.kt
diff --git a/maps/src/main/java/com/vayunmathur/maps/data/PoiIndex.kt b/maps/src/main/java/com/vayunmathur/maps/data/PoiIndex.ktindex ef1a053ad6..4dc861662f 100644--- a/maps/src/main/java/com/vayunmathur/maps/data/PoiIndex.kt+++ b/maps/src/main/java/com/vayunmathur/maps/data/PoiIndex.kt@@ -700,8 +700,13 @@ object PoiIndex { fun attributesAt(ordinal: Int): PoiAttributes? { val m = mapped ?: return null val buf = m.attrs ?: return null- val at = attrBodyAt(m, buf, ordinal) ?: return null- return decodeAttrRecord(buf, at.first, at.second)+ // A planet5 block-compressed section inflates on first touch, so a+ // corrupt block only fails here, at tap time, not when the file was+ // validated. That must read as "no attributes", not throw.+ return runCatching {+ val at = attrBodyAt(m, buf, ordinal) ?: return null+ decodeAttrRecord(buf, at.first, at.second)+ }.getOrElse { bufferFailure(it, ATTRS_FILE) } } /** (bodyStart, bodyEnd) of the [ordinal]th record's attribute blob, or null. */
maps/src/main/java/com/vayunmathur/maps/data/PoiIndexRegion.kt
diff --git a/maps/src/main/java/com/vayunmathur/maps/data/PoiIndexRegion.kt b/maps/src/main/java/com/vayunmathur/maps/data/PoiIndexRegion.ktindex 2684606057..3ef5b18c3f 100644--- a/maps/src/main/java/com/vayunmathur/maps/data/PoiIndexRegion.kt+++ b/maps/src/main/java/com/vayunmathur/maps/data/PoiIndexRegion.kt@@ -31,8 +31,20 @@ internal const val REGION_ID_BYTES = 8 internal const val REGION_NONE = 0L /** Tagged OSM boundary id for record [i], or 0 when the region sidecar is absent. */-internal fun PoiIndex.Mapped.regionId(i: Int): Long =- region?.getLong(REGION_HEADER_BYTES + i * REGION_ID_BYTES) ?: REGION_NONE+internal fun PoiIndex.Mapped.regionId(i: Int): Long {+ val bytes = region ?: return REGION_NONE+ if (i !in 0 until count) return REGION_NONE+ // A planet5 block-compressed section inflates on first touch, so a corrupt+ // block only fails here, at query time, not when the file was validated.+ // That must read as "no boundary", not throw out of a search or tap.+ return runCatching { bytes.getLong(REGION_HEADER_BYTES + i * REGION_ID_BYTES) }+ .getOrElse { cause ->+ when (cause) {+ is IndexOutOfBoundsException, is IllegalArgumentException -> REGION_NONE+ else -> throw cause+ }+ }+} /** * Buffer core of the region sidecar, reused by the single-archive path. A planet5 archive