Skip to content

world_index: member type schema validation and strict JSON float compliance - #6318

Open
HalpDmaGetIommuInterface wants to merge 2 commits into
ItsNotRexed:mainfrom
HalpDmaGetIommuInterface:fix-world-index-types-and-json
Open

HalpDmaGetIommuInterface wants to merge 2 commits into
ItsNotRexed:mainfrom
HalpDmaGetIommuInterface:fix-world-index-types-and-json

Conversation

@HalpDmaGetIommuInterface

Copy link
Copy Markdown

Summary

Fixes two issues in tools/world_index.py:

  1. Schema Member Type Validation (world_index: a mismatched member type crashes the scan with TypeError #5601):

    • When extracting fields from CEntityDef and CMapData schemas, parse_resource() previously read named members by hash without validating that their type byte matched the expected field type.
    • For example, if position was declared as a scalar float (0x21) rather than vector3 (0x32/0x33), positions.append(pos) appended a float, subsequently causing min(p[i] for p in positions) to raise TypeError: 'float' object is not subscriptable escaping main() as a traceback.
    • Similarly, declaring archetypeName or lodLevel as vector types raised TypeError in set/dict lookups.
    • Now, known member types and bounds (offset + width <= struct_size) are validated against ENTITY_MEMBER_TYPES and MAPDATA_MEMBER_TYPES before block processing, returning None if the schema is malformed as documented in the docstring. Defensive type guards are also added to entity collection.
  2. Strict JSON Non-Finite Float Compliance (world_index: non-finite placement floats are written as NaN/Infinity, which is not JSON #5602):

    • Placement vectors containing non-finite floats (NaN, Infinity, -Infinity) previously passed through _r4 and were written directly to --out JSON via Python's default json.dump, producing non-standard JSON that causes syntax errors in strict parsers.
    • In addition, lodDist >= 0 permitted +inf into lod_dists.
    • Now, non-finite values are filtered using math.isfinite():
      • position_min/position_max drop non-finite components during aggregation and emit null for components lacking finite values.
      • Extents in _r4() emit null for non-finite components or None if entirely non-finite.
      • lod_dist_min/lod_dist_max ignore +inf and NaN.
      • allow_nan=False is enforced across json.dump / json.dumps calls matching other repo tools (world_build.py, rbf0_tree.py, glb_inspect.py, etc.).

Verification

  • Tested against synthetic reproducers from world_index: a mismatched member type crashes the scan with TypeError #5601 and world_index: non-finite placement floats are written as NaN/Infinity, which is not JSON #5602.
  • Added comprehensive unit tests in tests/test_world_index.py:
    • test_schema_member_type_mismatch_is_rejected: verifies malformed member types (0x21 position, 0x32 archetype/lodLevel/extent, out-of-bounds offset) reject cleanly.
    • test_non_finite_placement_floats_produce_valid_json: verifies non-finite coordinates produce valid strict JSON (null), verified with strict JSON parser.
    • test_non_finite_lod_dist_is_ignored: verifies +inf / NaN lod distances are ignored.
  • Ran all 40 tests in tests/test_world_index.py (40 passed).
  • Ran entire world test suite:
    • tests/test_world_archetypes.py: PASS
    • tests/test_world_build.py: PASS
    • tests/test_world_build_output_lock.py: PASS
    • tests/test_world_census.py: PASS
    • tests/test_world_index.py: PASS
    • tests/test_world_index_three_streams.py: PASS
    • tests/test_world_instancing.py: PASS
    • tests/test_world_interior_names.py: PASS
    • tests/test_world_json_atomic.py: PASS
    • tests/test_world_lights_paths.py: PASS
    • tests/test_world_names.py: PASS
    • tests/test_world_place.py: PASS
    • tests/test_world_placement_identity.py: PASS

Closes #5601
Closes #5602
Coordinated on #6089 (#6089 (comment))

AI-assisted: yes.

@shdann

shdann commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

The new schema check accepts a one-byte map name, but the reader uses four bytes for that field. Three bytes after the field can therefore change the reported map name.

I reproduced this on 76b8990 with the synthetic fixture. Run this script from the repository root with py -3.14 -I.

import runpy
t = runpy.run_path("tests/test_world_index.py")
b = bytearray(t["make_resource"]())
b[0x3A8] = 0x11  # unsigned byte, accepted by MAPDATA_MEMBER_TYPES for name
b[0x508:0x50C] = bytes.fromhex("12 34 56 78")
print(hex(t["WI"].parse_resource(b)["map_name"]))

Actual result: 0x78563412. For the accepted one-byte declaration, the value is 0x12. If map names require four bytes, reject this declaration. Otherwise, read the value at its declared width. The same read occurs on base 3faac55. The new validation still permits it.

The world_index suite passed 39 tests and skipped one on Windows. The three added tests failed on the base.

AI-assisted.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants