fix(parser,data): refuse a corrupted STEP numeric literal instead of truncating it (#5193)

Merged
LO
2mo ago
+454-33

Closes #5193

Root cause

Every TypeScript STEP token reader trusted parseFloat, and parseFloat returns any leading numeric prefix and discards the rest. So 1.52.3 (a dropped comma between two reals) read as 1.52, and 1.5abc read as 1.5, with no diagnostic. The existing Number.isFinite guard only covered the separate overflow hazard (1.0E400). Three sites had the defect:

  • packages/parser/src/entity-extractor.ts parseAttributeValue (the one the issue executed),
  • packages/parser/src/attribute-helpers.ts getNumber (dormant copy named in the issue),
  • packages/data/src/step-serializers.ts parseStepValue (public @ifc-lite/data export; same class, not named in the issue): parseStepValue('1.52.3') === 1.52 on main.

Fix

  • isCompleteStepNumericLiteral(token): a single forward scan encoding ISO 10303-21 REAL/INTEGER (sign, digits, optional ., bare-dot .5, optional exponent), with nothing allowed before or after. It lives in @ifc-lite/data (step-numeric-literal.ts), the lowest layer both readers share, so there's one copy of the grammar. All three sites gate on it before parseFloat. A malformed token keeps its raw string, the same way an overflowing one already does.
  • Consumers that turn a number into a coordinate refuse and warn through the existing channel. isUnrepresentableNumericValue now also flags isMalformedNumericLiteral (a token that starts like a number but isn't complete), so extractMapConversion refuses a corrupted Eastings instead of treating it as || 0. Enums and identifiers (.T., .UNSPECIFIED., *) are never flagged.
  • Legal forms still parse: 1., .5, 1.E3, +1.5, -3, and 1.0E400 (still preserved as an overflow).

The parser half is integrated from BIMvoice's branch fix-5193-numeric-literal-validation, with authorship kept. The follow-up commit moves the grammar down to @ifc-lite/data, fixes parseStepValue, and switches the scan to charCodeAt.

Evidence (RED -> GREEN)

Reverting entity-extractor.ts and step-serializers.ts to origin/main:

  • entity-extractor-numeric-literal-5193.test.ts: 2 fail ("1.52.3" -> 1.52, "1.5abc" -> 1.5)
  • data/src/step-numeric-literal.test.ts: parseStepValue('(1.52.3,4.,5.)') returns [1.52, 4, 5] instead of ['1.52.3', 4, 5]

With the fix: pnpm turbo run test --filter=@ifc-lite/data --filter=@ifc-lite/parser gives data 310/310 and parser 1675 passed. One parser test failed on the full run: parser-worker-panic-forward hit its 30s timeout under local load. It passes 2/2 in isolation and is unrelated to this change. pnpm turbo run typecheck for both packages is clean, as are oxlint, check-module-size and check-changesets.

Perf (TS parser hot path)

parseAttributeValue runs once per attribute of every entity, so I measured it. I ran EntityExtractor.extractEntity over all 44,249 entities of AC20-FZK-Haus.ifc, best/median of 9, base and branch interleaved three times:

base best / medianbranch best / median
155.8 / 60.9 ms57.3 / 63.3 ms
254.3 / 55.7 ms54.1 / 55.7 ms
355.4 / 58.8 ms56.3 / 58.6 ms

The difference is within noise. Output is identical: an FNV-1a hash over every extracted attribute list is 63ea1480 on both. (scripts/perf/probe.sh is the native Rust pipeline and doesn't touch this TS code, so this measures the changed path directly.)

API / changesets

  • @ifc-lite/data minor: new export isCompleteStepNumericLiteral (real consumer: @ifc-lite/parser), plus the parseStepValue fix. scripts/api-surface.json updated.
  • @ifc-lite/parser patch.

Related

The Rust fast coordinate readers have a worse sibling: get_cartesian_point_fast turns (1.52.3,4.0,5.0) into (1.52, 0.3, 4.0). It's out of scope here and filed as #5266.

šŸ¤– Generated with Claude Code