[PR #1429] bvh_geometry_check: catch NaN leaf bounds, infinite-bound override tolerance blow-up, invalid --geom, and strict JSON (BVH-GEOMETRY-CHECK-NAN-INF-GEOM) #1459

Open
opened 2026-11-26 20:21:19 +01:00 by 4ld0 · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/ubiquitarianism/leonida/pull/1429
Author: @IlyassRS3
Created: 11/25/2026
Status: 🔄 Open

Base: main ← Head: fix/bvh-geometry-check-nan-inf-geom


📝 Commits (1)

  • e5f7426 bvh_geometry_check: catch NaN leaf bounds, infinite-bound override tolerance blow-up, invalid --geom, and strict JSON (BVH-GEOMETRY-CHECK-NAN-INF-GEOM)

📊 Changes

2 files changed (+129 additions, -7 deletions)

View changed files

📝 tests/test_bvh_geometry_check.py (+94 -0)
📝 tools/bvh_geometry_check.py (+35 -7)

📄 Description

Item ID: BVH-GEOMETRY-CHECK-NAN-INF-GEOM
AI-assisted: yes

Summary

Fixes four soundness and input-validation bugs in tools/bvh_geometry_check.py:

  1. Non-finite (NaN/±inf) leaf and scene bounds silently passed check_bounds():
    • When a leaf's vertices produced NaN bounds, max([box[a] - b[a], ...]) > OUTSIDE_TOLERANCE * extent evaluated to False, and Python's min(union[a], NaN) / max(union[a + 3], NaN) dropped NaN once another finite leaf populated union — falsely reporting leaves_outside=0, bounds_gap=0.0, consistent=True.
    • check_bounds() now rejects non-finite mapped sceneBounds corners and counts any leaf with non-finite bounds as outside (setting bounds_gap = math.inf, consistent = False).
  2. check_overrides() tolerance blew up to +inf on infinite child bounds:
    • Line 98 checked if math.isfinite(value) without checking math.isfinite(b[comp]). When a child bound was ±inf (e.g. a BOX16 bound overflowing half-float range) and the stored override value was finite, tol became +inf, turning lo - tol <= value <= hi + tol into -inf <= value <= +inf and accepting overrides wildly outside [lo, hi].
    • Added _ulp_tol(value, ref) so each bound comparison (b[comp], lo, hi) applies ULP tolerance only when both operands are finite.
  3. Strict --geom VB[,IB] validation and early --json directory check:
    • Rejects malformed --geom specs (len(parts) > 2 or empty VB path such as --geom vb,ib,extra, --geom ,ib, --geom "") with exit status 2 instead of silently dropping extra tokens or opening "".
    • Rejects --json <existing_dir> before reading/rehydrating.
  4. Strict RFC 8259 --json output (allow_nan=False):
    • Non-finite float fields (such as bounds_gap = inf on inconsistent non-finite inputs) are serialized as JSON null with allow_nan=False rather than emitting non-standard NaN/Infinity tokens.

Verification (macOS Apple M5, arm64, Python 3.14; no game files)

  • python3 -m compileall -q make_game.py tools tests — OK
  • python3 -I scripts/check_no_game_data.py tools/bvh_geometry_check.py tests/test_bvh_geometry_check.py — OK
  • python3 -I tests/test_bvh_geometry_check.py -v — 12/12 OK (5 new regression tests)
  • All 11 tests/test_bvh_*.py suites — 214/214 OK
  • python3 -I tests/test_text_write_line_endings.py — 3/3 OK
  • python3 -I tests/test_ci_registration.py — 16/16 OK
  • Mutation proof (running the new tests/test_bvh_geometry_check.py against unpatched upstream/main 1154976d4): Ran 12 tests — FAILED (failures=5) — all 5 new regression tests fail without this fix.

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/ubiquitarianism/leonida/pull/1429 **Author:** [@IlyassRS3](https://github.com/IlyassRS3) **Created:** 11/25/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `fix/bvh-geometry-check-nan-inf-geom` --- ### 📝 Commits (1) - [`e5f7426`](https://github.com/ubiquitarianism/leonida/commit/e5f742680e6290f3a4a6d8ea5a6d2ac1e80b1920) bvh_geometry_check: catch NaN leaf bounds, infinite-bound override tolerance blow-up, invalid --geom, and strict JSON (BVH-GEOMETRY-CHECK-NAN-INF-GEOM) ### 📊 Changes **2 files changed** (+129 additions, -7 deletions) <details> <summary>View changed files</summary> 📝 `tests/test_bvh_geometry_check.py` (+94 -0) 📝 `tools/bvh_geometry_check.py` (+35 -7) </details> ### 📄 Description Item ID: `BVH-GEOMETRY-CHECK-NAN-INF-GEOM` AI-assisted: yes ### Summary Fixes four soundness and input-validation bugs in `tools/bvh_geometry_check.py`: 1. **Non-finite (`NaN`/`±inf`) leaf and scene bounds silently passed `check_bounds()`**: - When a leaf's vertices produced `NaN` bounds, `max([box[a] - b[a], ...]) > OUTSIDE_TOLERANCE * extent` evaluated to `False`, and Python's `min(union[a], NaN)` / `max(union[a + 3], NaN)` dropped `NaN` once another finite leaf populated `union` — falsely reporting `leaves_outside=0, bounds_gap=0.0, consistent=True`. - `check_bounds()` now rejects non-finite mapped `sceneBounds` corners and counts any leaf with non-finite bounds as outside (setting `bounds_gap = math.inf`, `consistent = False`). 2. **`check_overrides()` tolerance blew up to `+inf` on infinite child bounds**: - Line 98 checked `if math.isfinite(value)` without checking `math.isfinite(b[comp])`. When a child bound was `±inf` (e.g. a BOX16 bound overflowing half-float range) and the stored override `value` was finite, `tol` became `+inf`, turning `lo - tol <= value <= hi + tol` into `-inf <= value <= +inf` and accepting overrides wildly outside `[lo, hi]`. - Added `_ulp_tol(value, ref)` so each bound comparison (`b[comp]`, `lo`, `hi`) applies ULP tolerance only when both operands are finite. 3. **Strict `--geom VB[,IB]` validation and early `--json` directory check**: - Rejects malformed `--geom` specs (`len(parts) > 2` or empty VB path such as `--geom vb,ib,extra`, `--geom ,ib`, `--geom ""`) with exit status 2 instead of silently dropping extra tokens or opening `""`. - Rejects `--json <existing_dir>` before reading/rehydrating. 4. **Strict RFC 8259 `--json` output (`allow_nan=False`)**: - Non-finite float fields (such as `bounds_gap = inf` on inconsistent non-finite inputs) are serialized as JSON `null` with `allow_nan=False` rather than emitting non-standard `NaN`/`Infinity` tokens. ### Verification (macOS Apple M5, arm64, Python 3.14; no game files) - `python3 -m compileall -q make_game.py tools tests` — OK - `python3 -I scripts/check_no_game_data.py tools/bvh_geometry_check.py tests/test_bvh_geometry_check.py` — OK - `python3 -I tests/test_bvh_geometry_check.py -v` — 12/12 OK (5 new regression tests) - All 11 `tests/test_bvh_*.py` suites — 214/214 OK - `python3 -I tests/test_text_write_line_endings.py` — 3/3 OK - `python3 -I tests/test_ci_registration.py` — 16/16 OK - **Mutation proof** (running the new `tests/test_bvh_geometry_check.py` against unpatched `upstream/main` `1154976d4`): `Ran 12 tests — FAILED (failures=5)` — all 5 new regression tests fail without this fix. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
Sign in to join this conversation.
No labels
bug
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
starred/leonida#1459
No description provided.