Skip to content

Refuse Float and Double data values that overflow to infinity [patch] - #360

Open
matt-edmondson wants to merge 1 commit into
mainfrom
fix/355-refuse-overflowing-floats
Open

matt-edmondson wants to merge 1 commit into
mainfrom
fix/355-refuse-overflowing-floats

Conversation

@matt-edmondson

Copy link
Copy Markdown
Contributor

Fixes #355

What changed

  • SchemaDataValidator: Float and Double values now go through ExpectNumber, which holds a number to its storage type's finite range. Float must satisfy |x| ≤ float.MaxValue and Double must parse to a finite double. Vector, colour, quantity and semantic-type components with Float/Double storage use the same path, so they get the same check. The message states the range, for example Expected a number within the range of Float (±3.4028235E+38) for type 'Float' but found 1e39.
  • SchemaDataValues (the optional part of the issue): GetFloat, GetDouble and the vector/colour component readers treat a non-finite result as malformed and throw InvalidOperationException, the same way they already handle a value that doesn't fit. They no longer hand back ±Infinity.

The migrator's Conforms goes through the validator, so it no longer accepts such values either.

Tests

  • SchemaDataValidatorTypeTests: ±1e39 in a Float is an error, while float.MaxValue, 1e38 and 0 are accepted. 1e309 in a Double is an error and 1e39 is not. Also covered: a Float vector component (error at data.Value[1]), a colour component, a Float-stored quantity, and a semantic type over Float.
  • GeneratedDataEditorTests.TestANumberPastItsTypesRangeThrowsRatherThanReadingAsInfinity: GetFloat, GetDouble and GetVector3 throw. A Double read of 1e39 still succeeds.

With the fix reverted, the 7 new tests fail. With it, the full Schema.Test suite passes on net10.0 (728/728).

This touches SchemaDataValidator.cs like #359 (for #349), but in different methods. It's branched from main on its own, not stacked on that PR.

Main currently fails CA1506 on Schema/Models/Schema.cs (#353 fixes that). I ran the tests locally with that one rule suppressed, and the suppression isn't part of this PR. CI here will stay red on CA1506 until #353 lands.


Generated by Claude Code

The validator checked a Float or Double value only for being a JSON number,
so 1e39 in a Float (or 1e309 in a Double) validated clean and then read back
from the generated record as Infinity. Float, Double, and every vector,
colour, quantity and semantic component stored in one are now held to the
storage type's finite range, and the message states that range.

SchemaDataValues.GetFloat, GetDouble and the vector/colour readers now treat
a non-finite result as malformed and throw, instead of returning it.

Fixes #355

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017vfJW3VYgr52Mw1KS9otcE

Copy link
Copy Markdown
Contributor Author

CI is red on ci / .NET / Test, but the cause isn't this PR. The only error is CA1506: 'Schema' is coupled with '97' different types at Schema/Models/Schema.cs(18,22), which this diff doesn't touch. main fails the same way at 253db1a (run).

#353 has the fix and is mergeable. I haven't copied that refactor in here, since it would duplicate #353 across three open PRs and they would then conflict with it. Once #353 merges I'll bring main into this branch so CI re-runs. Locally, with only CA1506 suppressed, Schema.Test passes 728/728.


Generated by Claude Code

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

A Float data value of 1e39 (or a Double of 1e309) validates clean, then reads back from the generated data record as Infinity

2 participants