Skip to content

Conversation

@ktopley-apple
Copy link
Contributor

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

@ktopley-apple
Copy link
Contributor Author

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

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?

Copy link
Contributor Author

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.

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 {
Copy link
Contributor

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed.

@ktopley-apple
Copy link
Contributor Author

@swift-ci please test

1 similar comment
@ktopley-apple
Copy link
Contributor Author

@swift-ci please test

Copy link
Contributor

@dgrove-oss dgrove-oss left a comment

Choose a reason for hiding this comment

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

LGTM

@ktopley-apple ktopley-apple merged commit 947b51c into swiftlang:master Sep 20, 2017
ktopley-apple added a commit that referenced this pull request Dec 6, 2018
Fix overflow traps in DispatchTime/DispatchWallTime/DispatchTimeInterval

Signed-off-by: Daniel A. Steffen <dsteffen@apple.com>
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.

6 participants