-
Notifications
You must be signed in to change notification settings - Fork 194
Properly throw
in case of numeric overflow in Calendar mathematics
#900
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
We have applied multiple different strategies with regard to overflow during calendrical calculation, including capping input `Date` and clipping Julian day calculation with artificial bounds. Those are still insufficient as there are too many paths where numeric overflow may happen. This patch patches those places by `throw`ing whenever applicable. Also adds fuzzing tests to cover many more Calendar API. Resolves rdar://133558250
@swift-ci please test |
@swift-ci please test |
@@ -3257,10 +3257,10 @@ final class GregorianCalendarTests : XCTestCase { | |||
XCTAssertEqual(cal.range(of: .dayOfYear, in: .year, for: leapYearDate), 1..<367) | |||
|
|||
// Addition | |||
let d1 = cal.add(.dayOfYear, to: date, amount: 1, inTimeZone: .gmt) | |||
let d1 = try? cal.add(.dayOfYear, to: date, amount: 1, inTimeZone: .gmt) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we don't expect this to throw, could we mark the test as throws
and just do try
here? That'd mean that if an error is thrown, the test would fail and the failure would indicate the error information rather than just a nil
value for d1
which would probably help with debugging
func test_dateComponentsFromDateOverflow() { | ||
let calendar = Calendar(identifier: .gregorian) | ||
let dc = calendar.dateComponents([.year], from: Date(timeIntervalSinceReferenceDate: Double(Int64.max))) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just confirming - was there anything else that needed to be here or is this just testing that this doesn't crash?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks. Updated
@swift-ci please test |
…wiftlang#900) * Properly `throw` in case of numeric overflow in Calendar mathematics We have applied multiple different strategies with regard to overflow during calendrical calculation, including capping input `Date` and clipping Julian day calculation with artificial bounds. Those are still insufficient as there are too many paths where numeric overflow may happen. This patch patches those places by `throw`ing whenever applicable. Also adds fuzzing tests to cover many more Calendar API. Resolves rdar://133558250 * Review feedback: Adopt typed throw and fix an oversight * Update tests
We have applied multiple different strategies with regard to overflow during calendrical calculation, including capping input
Date
and clipping Julian day calculation with artificial bounds. Those are still insufficient as there are too many paths where numeric overflow may happen.This patch patches those places by
throw
ing whenever applicable. Also adds fuzzing tests to cover many more Calendar API.Resolves rdar://133558250