Skip to content

Fix TimeSpan constructor overflow - #134113

Merged
tarekgh merged 2 commits into
dotnet:mainfrom
asad24-dev:fix-timespan-constructor-overflow
Sep 17, 2026
Merged

tarekgh merged 2 commits into
dotnet:mainfrom
asad24-dev:fix-timespan-constructor-overflow

Conversation

@asad24-dev

Copy link
Copy Markdown
Contributor

Fixes an overflow in the day-based TimeSpan constructors where intermediate microsecond arithmetic could wrap before the existing range check.

The calculation now uses widened Int128 arithmetic before validating against the supported TimeSpan range and narrowing to long.

Regression coverage has been added for positive and negative overflow, including the reported wraparound case and the affected constructor overloads.

Local validation:

  • System.Private.CoreLib compilation succeeded.
  • git diff --check HEAD^ HEAD succeeded.

Resolves #134109

Accumulate day-based constructor inputs with widened arithmetic before\nrange checking so oversized durations cannot wrap.\n\nAdd regression coverage for the affected overloads.\n\nFix dotnet#134109
@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label Sep 16, 2026
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 3 pipeline(s).
13 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.

@asad24-dev

Copy link
Copy Markdown
Contributor Author

@dotnet-policy-service agree

Comment thread src/libraries/System.Runtime/tests/System.Runtime.Tests/System/TimeSpanTests.cs Outdated
@tarekgh tarekgh added this to the 12.0.0 milestone Sep 16, 2026
@tarekgh
tarekgh marked this pull request as ready for review September 16, 2026 23:45
Copilot AI lite review requested due to automatic review settings September 16, 2026 23:45
@azure-pipelines

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

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🔵 Needs a closer look

Address the moderate performance concern regarding widened multiplication overhead in TimeSpan.cs.

Pull request overview

Fixes overflow in day-based TimeSpan constructors by widening intermediate arithmetic before range validation.

Changes:

  • Uses Int128 for intermediate calculations.
  • Adds regression tests for overflow and wraparound cases.
File summaries
File Summary
src/libraries/System.Runtime/tests/System.Runtime.Tests/System/TimeSpanTests.cs Adds constructor overflow regression coverage.
src/libraries/System.Private.CoreLib/src/System/TimeSpan.cs Prevents intermediate arithmetic overflow.
Review details

Suppressed comments (1)

src/libraries/System.Private.CoreLib/src/System/TimeSpan.cs:298

  • Because all constructor inputs are int, only the day product needs a widened multiplication: even int.MaxValue * MicrosecondsPerHour fits in long, and the smaller-unit products are narrower. Calling the full 64-bit Math.BigMul for each of these four terms adds unnecessary high-half multiplications to every normal constructor call; keep the Int128 accumulator but use ordinary long products here (or provide benchmark evidence for the added cost).
                                     + Math.BigMul(hours, MicrosecondsPerHour)
                                     + Math.BigMul(minutes, MicrosecondsPerMinute)
                                     + Math.BigMul(seconds, MicrosecondsPerSecond)
                                     + Math.BigMul(milliseconds, MicrosecondsPerMillisecond)
  • Files reviewed: 2/2 changed files
  • Comments generated: 0
  • Review effort level: Lite

Report overflow regressions independently and cover valid day-based boundaries.\n\nFix dotnet#134109
Copilot AI review requested due to automatic review settings September 16, 2026 23:54

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟢 Approval recommended

The remaining performance comment is a non-blocking nit.

Review details

Suppressed comments (1)

src/libraries/System.Private.CoreLib/src/System/TimeSpan.cs:298

  • Because all four non-day constructor components are int, each product fits in long (even int.MaxValue * MicrosecondsPerHour is below long.MaxValue), while the Int128 accumulator already prevents overflow in the additions. Calling Math.BigMul for hours/minutes/seconds/milliseconds therefore performs four unnecessary full-width multiplications on every constructor call (including the 4- and 5-argument overloads, which delegate here). Keep the widened day product, but use ordinary long products for these terms.
                                     + Math.BigMul(hours, MicrosecondsPerHour)
                                     + Math.BigMul(minutes, MicrosecondsPerMinute)
                                     + Math.BigMul(seconds, MicrosecondsPerSecond)
                                     + Math.BigMul(milliseconds, MicrosecondsPerMillisecond)
  • Files reviewed: 2/2 changed files
  • Comments generated: 0 new
  • Review effort level: Lite

@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.

LGTM, thanks @asad24-dev

@tarekgh

tarekgh commented Sep 17, 2026

Copy link
Copy Markdown
Member

/ba-g the failure is unrelated

@tarekgh
tarekgh merged commit a414ddf into dotnet:main Sep 17, 2026
136 of 138 checks passed
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.

Libs: (bug) TimeSpan day-based constructors silently wrap instead of throwing for out-of-range durations

3 participants