-
Notifications
You must be signed in to change notification settings - Fork 471
Fix overflow traps in DispatchTime/DispatchWallTime/DispatchTimeInterval #301
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
Fix overflow traps in DispatchTime/DispatchWallTime/DispatchTimeInterval #301
Conversation
@swift-ci please test |
// Because of the way this function is used, we can always assume | ||
// that m2 > 0. | ||
private func clampedInt64Product(_ m1: Int64, _ m2: Int64) -> Int64 { | ||
assert(m2 > 0, "multiplier must be positive") |
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.
assert
is a noop in release builds. Should perhaps use _precondition
?
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.
Not having the check in release builds is exactly what I want. The only calls to this method are internal to this file and the intent is only to document the assumption and to catch (admittedly unlikely) future changes that violate it.
src/swift/Time.swift
Outdated
private func clampedInt64Product(_ m1: Int64, _ m2: Int64) -> Int64 { | ||
assert(m2 > 0, "multiplier must be positive") | ||
let (result, overflow) = m1.multipliedReportingOverflow(by: m2) | ||
guard overflow == false else { |
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.
This reads a little backwards to me. What do you think about the following instead?
if overflow {
return m1 > 0 ? Int64,max : Int64.min
}
return result
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.
Fixed.
@swift-ci please test |
1 similar comment
@swift-ci please test |
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.
LGTM
…val. rdar://problem/34440944
Fix overflow traps in DispatchTime/DispatchWallTime/DispatchTimeInterval Signed-off-by: Daniel A. Steffen <dsteffen@apple.com>
This is the swift-corelibs-libdispatch version of the PR at swiftlang/swift#11927.
Fixes overflows in DispatchTime/DispatchWallTime/DispatchTimeInterval.
Examples:
DispatchTime.now() + Date.distantFuture.timeIntervalSinceNow // traps
DispatchTime.now() + Date.distantPast.timeIntervalSinceNow. // traps
let t = DispatchTimeInterval.seconds(Int.max)
t == t // Traps due to conversion to nanoseconds.
rdar://problem/34440944