Skip to content

Fix TimeSpan parsing of long fractional seconds - #137147

Merged
tarekgh merged 2 commits into
dotnet:mainfrom
makazeu:fix-timespan-long-fraction
Nov 9, 2026
Merged

tarekgh merged 2 commits into
dotnet:mainfrom
makazeu:fix-timespan-long-fraction

Conversation

@makazeu

@makazeu makazeu commented Nov 8, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Restore parsing of long fractional seconds with leading zeroes by extending the integer powers-of-ten table through 10^9. Reuse the shared uint.PowersOf10 table directly instead of maintaining a TimeSpanParse-specific helper.

Reproduction and rationale

According to the TIMESPAN-1 report, .NET 8 successfully parses the original input, "0:0:0.0000000123456789", as TimeSpan.Zero: TryParse returns true rather than throwing. The same input throws on .NET 9 and 10, so this is a regression of previously accepted input, not just an unsupported format.

The following behavior was reproduced locally on .NET 10.0.14:

TimeSpan.TryParse("0:0:0.0000000123456789", CultureInfo.InvariantCulture, out _);
// Throws IndexOutOfRangeException.

TimeSpan.TryParse("0:0:0.00000000123456789", CultureInfo.InvariantCulture, out TimeSpan result);
// Adding one leading zero to the fraction succeeds: true, result == TimeSpan.Zero.

TimeSpan.TryParse("0:0:0.000000050000000", CultureInfo.InvariantCulture, out _);
// Also throws IndexOutOfRangeException; this value should round to one tick.

The inconsistency comes from two normalization paths. With eight leading zeroes, the fraction is small enough to round to zero and returns early. With seven leading zeroes and nine significant digits, normalization divides the stored integer by 10^(16 - 7), but the existing table only contains 10^0 through 10^7.

The shared powers-of-ten table was previously limited to that range. The fuzz report identifies the resulting behavior as a regression from .NET 8 to .NET 9; that version comparison was not independently rerun here.

There are two reasons to fix this rather than reject all fractions longer than seven digits:

  • The documented TryParse contract returns false for an invalid time-interval format or an out-of-range value. This input should not cause an internal array-index exception to escape an ordinary parse attempt.
  • Existing parsing behavior and tests already support rounding fractional seconds with leading zeroes beyond seven digits. Adding one more leading zero currently makes parsing succeed only because it avoids the broken lookup, not because of an intentional format restriction.

The fix preserves the existing integer rounding and tokenizer overflow rules. It does not introduce arbitrary-length or higher-precision parsing. Parse and TryParse, including their standard "g" and span paths, use the corrected normalization code. The follow-up test coverage also exercises long-fraction "g" parsing through the shared ParseExact test data.

Validation

Linux ARM64 Docker (Ubuntu 24.04, checked CoreCLR): builds passed, and the regression test fails on the unfixed code. After the fix, the TimeSpan and related date/time tests passed without failures.

Relates to #134839 (TIMESPAN-1 only).

Note

This pull request description was generated with GitHub Copilot.

Extend the integer power-of-ten table to cover the scaling exponents required by long fractions with leading zeroes. Preserve existing rounding and token overflow behavior, and add regression coverage for Parse and TryParse.

Co-authored-by: Copilot App <[email protected]>
@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label Nov 8, 2026
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 3 pipeline(s).
12 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @dotnet/area-system-datetime
See info in area-owners.md if you want to be subscribed.

@tarekgh

tarekgh commented Nov 8, 2026

Copy link
Copy Markdown
Member

CC @lilinus @huoyaoyuan

@tarekgh tarekgh added this to the 12.0.0 milestone Nov 8, 2026
Use uint.PowersOf10 directly and remove the TimeSpanParse helper. Add long-fraction G cases to the shared ParseExact test data.

Co-authored-by: Copilot App <[email protected]>

@tarekgh tarekgh left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @makazeu for providing the changes.

@tarekgh
tarekgh merged commit c6d1679 into dotnet:main Nov 9, 2026
133 of 135 checks passed
@makazeu
makazeu deleted the fix-timespan-long-fraction branch November 9, 2026 16:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-System.DateTime community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants