Skip to content

Commit

Permalink
Fix overflow in TimeSpec addition on 32-bit architectures (#1074)
Browse files Browse the repository at this point in the history
  • Loading branch information
orobio authored Oct 27, 2022
1 parent b8385e7 commit 970bfdd
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 6 deletions.
9 changes: 4 additions & 5 deletions Sources/DistributedCluster/TimeSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,14 @@ extension TimeSpec {
@usableFromInline
internal static func + (a: timespec, b: timespec) -> timespec {
let totalNanos = a.toNanos() + b.toNanos()
let seconds = totalNanos / NANOS
var result = timespec()
result.tv_sec = seconds
result.tv_nsec = totalNanos % NANOS
result.tv_sec = Int(totalNanos / Int64(NANOS))
result.tv_nsec = Int(totalNanos % Int64(NANOS))
return result
}

@usableFromInline
internal func toNanos() -> Int {
self.tv_nsec + (self.tv_sec * NANOS)
internal func toNanos() -> Int64 {
Int64(self.tv_nsec) + (Int64(self.tv_sec) * Int64(NANOS))
}
}
2 changes: 1 addition & 1 deletion Tests/DistributedClusterTests/TimeSpecTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class TimeSpecTests: XCTestCase {
}

func test_timeSpecShouldBeCreatedProperlyFromDuration() {
self.total.toNanos().shouldEqual(Int(self.totalDuration.nanoseconds))
self.total.toNanos().shouldEqual(Int64(self.totalDuration.nanoseconds))
self.total.tv_sec.shouldEqual(Int(self.totalDuration.nanoseconds) / NANOS)
self.total.tv_nsec.shouldEqual(Int(self.totalDuration.nanoseconds) % NANOS)
}
Expand Down

0 comments on commit 970bfdd

Please sign in to comment.