Skip to content

Commit 3d23d0a

Browse files
Copilotdanmoseley
andcommitted
Revert to manual validation approach instead of try-catch for TimeSpan overflow handling
Co-authored-by: danmoseley <[email protected]>
1 parent f10d364 commit 3d23d0a

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

src/Http/Headers/src/SetCookieHeaderValue.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -587,15 +587,17 @@ private static int GetSetCookieLength(StringSegment input, int startIndex, out S
587587
maxAge = -maxAge;
588588
}
589589

590-
try
591-
{
592-
result.MaxAge = TimeSpan.FromSeconds(maxAge);
593-
}
594-
catch (ArgumentOutOfRangeException)
590+
// Check if maxAge would cause TimeSpan.FromSeconds to overflow
591+
// TimeSpan.MaxValue.TotalSeconds is approximately 922337203685.4775
592+
const long MaxTimeSpanSeconds = 922337203685L;
593+
const long MinTimeSpanSeconds = -922337203685L;
594+
if (maxAge > MaxTimeSpanSeconds || maxAge < MinTimeSpanSeconds)
595595
{
596596
// MaxAge value would overflow TimeSpan, abort
597597
return 0;
598598
}
599+
600+
result.MaxAge = TimeSpan.FromSeconds(maxAge);
599601
offset += itemLength;
600602
}
601603
// domain-av = "Domain=" domain-value

0 commit comments

Comments
 (0)