Repository navigation
fix: bound the decimal precision check by what decimal holds, not by the text - #277
Merged
Merged
Conversation
…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]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #276
Problem
DecimalLiteral.Represents(#166, for #115) sized its work by the supplied text instead of by whatdecimalcan hold. That reaches every decimal entry point:DecimalRange,RangeSet<DecimalRange>,DecimalSetandDecimalSet<T>, through a literal, JSON,TypeConverterand the EF Core JSON document.0e-100000001e-10000000.+ 1M zeros +11.+ 2M zeros1e-99999999999OverflowExceptionfromParseFormatException0e99999999999OverflowExceptionfromParseA stack overflow can't be caught, so
TryParseand 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 withTryParse. What's left is one pass over the text plus a constant, with no stack buffer and noBigInteger.Only the defects above change an answer. Over 2.5 million random texts that
decimal.Parseaccepts, 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_CostsNoWorkProportionalToItselffails 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_IsReadOrRefusedAsFormatfails withOverflowException.LongElement_CostsNoStackProportionalToItsLengthkills 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