Skip to content

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

Merged
merged 3 commits into from
Sep 17, 2024

Conversation

itingliu
Copy link
Contributor

@itingliu itingliu commented Sep 4, 2024

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 throwing whenever applicable. Also adds fuzzing tests to cover many more Calendar API.

Resolves rdar://133558250

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
@itingliu
Copy link
Contributor Author

itingliu commented Sep 5, 2024

@swift-ci please test

@itingliu
Copy link
Contributor Author

itingliu commented Sep 6, 2024

@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)
Copy link
Contributor

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)))

Copy link
Contributor

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?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks. Updated

@itingliu
Copy link
Contributor Author

@swift-ci please test

@itingliu itingliu merged commit 3cf61a2 into swiftlang:main Sep 17, 2024
3 checks passed
@itingliu itingliu deleted the pr/2499 branch September 17, 2024 17:01
cthielen pushed a commit to cthielen/swift-foundation that referenced this pull request Nov 8, 2024
…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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants