Skip to content

fix: bound the decimal precision check by what decimal holds, not by the text - #277

Merged
CaffeinatedCoder merged 1 commit into
mainfrom
fix/276-decimal-literal-bounds
Oct 19, 2026
Merged

CaffeinatedCoder merged 1 commit into
mainfrom
fix/276-decimal-literal-bounds

Conversation

@CaffeinatedCoder

Copy link
Copy Markdown
Owner

Closes #276

Problem

DecimalLiteral.Represents (#166, for #115) sized its work by the supplied text instead of by what decimal can hold. That reaches every decimal entry point: DecimalRange, RangeSet<DecimalRange>, DecimalSet and DecimalSet<T>, through a literal, JSON, TypeConverter and the EF Core JSON document.

Element Before After
0e-10000000 still running after 5 s 9 ms, reads 0
1e-1000000 154 ms, allocates 15 MB 0 ms, refused
0. + 1M zeros + 1 stack overflow, process killed 12 ms, refused
1. + 2M zeros stack overflow, process killed 25 ms, reads 1
1e-99999999999 OverflowException from Parse FormatException
0e99999999999 OverflowException from Parse reads 0 (9.1.0 read 0 too)

A stack overflow can't be caught, so TryParse and JSON killed the process.

Change

src/CodoMetis.ValueRanges/Internals/DecimalLiteral.cs: both sides are reduced to a significand with no trailing zero times a power of ten, and the check compares two integers. Significant digits are counted before any are kept, and more than 29 (the most a decimal mantissa has) are refused. An exponent past ±28 is refused, and the exponent is read with TryParse. What's left is one pass over the text plus a constant, with no stack buffer and no BigInteger.

Only the defects above change an answer. Over 2.5 million random texts that decimal.Parse accepts, the old and new check agree on every one. The sibling readers (FloatLiteral, IntegerLiteral, UuidLiteral, IsoTemporal) don't size their work by the text's length or exponent (details in #276). No changelog entry, because the code being fixed hasn't shipped (10.0.0 isn't tagged).

Tests

Three new tests in DecimalPrecisionTests. Each fails with the fix reverted:

  • Exponent_CostsNoWorkProportionalToItself fails on its allocation bound (15 MB against 16 KB). It runs the cases the old code finishes first, so it fails before it reaches {0e-10000000}, which would hang.
  • ExponentBeyondInt32_IsReadOrRefusedAsFormat fails with OverflowException.
  • LongElement_CostsNoStackProportionalToItsLength kills the test host with a stack overflow, because that can't be caught. It runs on a 256 KB thread so the input it needs stays small.

Core, NodaTime, EF translation, conventions and ASP.NET Core tests pass locally (2,412). The integration tests weren't run, since no translation changed.

🤖 Generated with Claude Code

…the text

DecimalLiteral.Represents (#166, for #115) sized its work by the supplied text. It buffered the
digits on the stack at the text's length, so an element decimal.Parse accepts, such as "0." plus a
million zeros and "1", overflowed the stack, which no handler catches: TryParse and JSON killed the
process. It took powers of ten from the exponent, so {1e-1000000} allocated 15 MB and
{0e-10000000} ran for minutes. And it read the exponent with int.Parse, which threw
OverflowException where Parse documents FormatException (#276, critical).

Both sides are now reduced to a significand with no trailing zero times a power of ten, which spells
each non-zero number one way only, and compared as two integers. Significant digits are counted
before any are kept and refused past 29 (the most a decimal mantissa has), an exponent past +-28 is
refused, and the exponent is read with TryParse, so the work is one pass over the text and a
constant. Only the three defects change an answer: an exponent beyond Int32 now reads zero digits as
0 (as 9.1.0 did) and refuses others with FormatException. Over 2.5 million random texts decimal.Parse
accepts, the old and new check agree on every one. The sibling readers (FloatLiteral, IntegerLiteral,
UuidLiteral, IsoTemporal) take no work from the text's length or exponent.

DecimalPrecisionTests gains three tests. With the fix reverted, the exponent test fails on its
allocation bound (15 MB against 16 KB) before it reaches the row that would hang, the Int32 test
fails with OverflowException, and the long-element test kills the test host with a stack overflow;
it runs on a 256 KB thread so the input stays small.

Closes #276

Found-By: RV
Defect-Class: LIT

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@CaffeinatedCoder CaffeinatedCoder added this to the 10.0.0 milestone Oct 19, 2026
@CaffeinatedCoder CaffeinatedCoder added bug Something isn't working area/ranges Range unions, variants and the shape engines area/value-sets Value set families and wrapper arities area/json System.Text.Json and TypeConverter wire forms area/literals Parse, TryParse and formatting package/core Affects CodoMetis.ValueRanges severity/critical Data loss, corruption, or silently wrong results in common use labels Oct 19, 2026
@CaffeinatedCoder
CaffeinatedCoder merged commit e465c45 into main Oct 19, 2026
@CaffeinatedCoder
CaffeinatedCoder deleted the fix/276-decimal-literal-bounds branch October 19, 2026 07:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/json System.Text.Json and TypeConverter wire forms area/literals Parse, TryParse and formatting area/ranges Range unions, variants and the shape engines area/value-sets Value set families and wrapper arities bug Something isn't working package/core Affects CodoMetis.ValueRanges severity/critical Data loss, corruption, or silently wrong results in common use

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Decimal precision check does work proportional to the text, not to what decimal holds

1 participant