Skip to content

Commit 970bfdd

Browse files
authored
Fix overflow in TimeSpec addition on 32-bit architectures (#1074)
1 parent b8385e7 commit 970bfdd

File tree

2 files changed

+5
-6
lines changed

2 files changed

+5
-6
lines changed

Sources/DistributedCluster/TimeSpec.swift

+4-5
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,14 @@ extension TimeSpec {
4545
@usableFromInline
4646
internal static func + (a: timespec, b: timespec) -> timespec {
4747
let totalNanos = a.toNanos() + b.toNanos()
48-
let seconds = totalNanos / NANOS
4948
var result = timespec()
50-
result.tv_sec = seconds
51-
result.tv_nsec = totalNanos % NANOS
49+
result.tv_sec = Int(totalNanos / Int64(NANOS))
50+
result.tv_nsec = Int(totalNanos % Int64(NANOS))
5251
return result
5352
}
5453

5554
@usableFromInline
56-
internal func toNanos() -> Int {
57-
self.tv_nsec + (self.tv_sec * NANOS)
55+
internal func toNanos() -> Int64 {
56+
Int64(self.tv_nsec) + (Int64(self.tv_sec) * Int64(NANOS))
5857
}
5958
}

Tests/DistributedClusterTests/TimeSpecTests.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class TimeSpecTests: XCTestCase {
3838
}
3939

4040
func test_timeSpecShouldBeCreatedProperlyFromDuration() {
41-
self.total.toNanos().shouldEqual(Int(self.totalDuration.nanoseconds))
41+
self.total.toNanos().shouldEqual(Int64(self.totalDuration.nanoseconds))
4242
self.total.tv_sec.shouldEqual(Int(self.totalDuration.nanoseconds) / NANOS)
4343
self.total.tv_nsec.shouldEqual(Int(self.totalDuration.nanoseconds) % NANOS)
4444
}

0 commit comments

Comments
 (0)