This repository was archived by the owner on Jan 23, 2023. It is now read-only.
Fix 8+ fractional digit checking (TimeSpan.Parse) #33581
Closed
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.
Fixes: #33577 TimeSpan.Parse gives inconsistent and incorrect results with 8 fractional digits.
InvalidFractionTest()is the culprit. It fails all the same things I was failing before. Adjusted code passes all the tests I can throw at it. Simply an off-by-one error (which becomes an off-by-ten error thanks to logarithms).TimeSpan.Parse has some fairly convoluted logic when it gets to
IsInvalidFraction(). It attempts to reconstruct the number of fractional digits in aTTTtoken based on the number of leading zeroes and the integer number itself, or rather, it avoids reconstructing it, and just attempts to check if there's not too many digits, involving the largest possible number, divided by 10 or 100 etc depending on the number of leading zeroes. It was getting this wrong by 10 or by one digit, which meant it was falling back onto some weaker checks earlier in the code. These gave very odd results (e.g. different results for numbers ending in 99 and 98)This patch fixes that very simply.
I've created an
InvalidFractionTestin my own repo to test the code against all the cases I could think of, but I haven't attempted to integrate the Unit Tests into corefx's codebase. This patch just has the fix itself. So I assume you'll want some tests before committing.