You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Libs: (bug) TimeSpan day-based constructors silently wrap instead of throwing for out-of-range durations #134109
new TimeSpan(days, hours, minutes, seconds[, ms[, µs]]) accumulates into an unchecked Int64 microsecond total and only range-checks the already-wrapped sum, so large days values silently produce a tiny (or zero) TimeSpan instead of throwing ArgumentOutOfRangeException.
Minimal Repro
usingSystem;Console.WriteLine(newTimeSpan(213503983,0,0,0));// ~584,942 years requestedConsole.WriteLine(newTimeSpan(-213503983,0,0,0));Console.WriteLine(newTimeSpan(213503982,8,1,49,551,616));// sums to exactly 2^64 microsecondsConsole.WriteLine(newTimeSpan(213503983,0,0,0,0));Console.WriteLine(newTimeSpan(213503983,0,0,0,0,0));TimeSpan.FromDays(213503983,0,0,0);// correctly throwsnewTimeSpan(TimeSpan.MaxValue.Days+1,0,0,0);// correctly throws
Expected
All five constructor calls throw ArgumentOutOfRangeException, as documented and as TimeSpan.FromDays(int, int, int, int) already does for the same arguments. TimeSpanTests.Ctor_Int_Int_Int_Int_Int_Invalid asserts exactly this contract for MaxValue.Days + 1.
Actual
15:58:10.4483840 // new TimeSpan(213503983, 0, 0, 0)
-15:58:10.4483840
00:00:00 // a ~584,942-year duration becomes TimeSpan.Zero
15:58:10.4483840
15:58:10.4483840
ArgumentOutOfRangeException: TimeSpan overflowed because the duration is too long. // FromDays
ArgumentOutOfRangeException: TimeSpan overflowed because the duration is too long. // MaxValue.Days + 1
Reproduces identically on .NET 8, 9 and 10.0.7.
Notes
MicrosecondsPerDay is 86,400,000,000, so days only needs to exceed ~213,503,982 for days * MicrosecondsPerDay to wrap Int64 — well inside int and reachable from arithmetic on user input or config. The TimeSpan.Zero case is the dangerous one: a huge duration handed to a timeout/delay API becomes an immediate-fire value. Dates from c88b783bd9d (#94226), which switched the intermediate accumulator from milliseconds to microseconds; #98633 later fixed the same class of problem in the integer From* factories (using Int128) but left the constructors alone. Fix: accumulate in Int128 as FromDays(int,int,int,int) does, or range-check days before multiplying.
I’d like to work on this. I’ve reproduced the overflow path in the current implementation and have a fix that uses widened arithmetic before the range check, together with regression coverage for the affected constructors.
Activity
dotnet-policy-service commentedon Sep 16, 2026
Tagging subscribers to this area: @dotnet/area-system-datetime
See info in area-owners.md if you want to be subscribed.
Fix TimeSpan constructor overflow
asad24-dev commentedon Sep 16, 2026
I’d like to work on this. I’ve reproduced the overflow path in the current implementation and have a fix that uses widened arithmetic before the range check, together with regression coverage for the affected constructors.
Improve TimeSpan overflow coverage
Fix TimeSpan constructor overflow (#134113)
Fix TimeSpan constructor overflow (dotnet#134113)