Skip to content

Commit 0943b16

Browse files
committed
std: Fix overflow when subtracting Instant
This code was currently only exercised on OSX, but this applies the same method of subtraction used on Linux which doesn't have the same overflow issues. Note that this currently includes no tests, but that's because this is only visible with debug assertions enabled. Soon, however, I'll enable debug assertions on all auto builds on the bots so we should get testing for this. Closes #32268
1 parent 1efa752 commit 0943b16

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

src/libstd/sys/unix/time.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,11 @@ mod inner {
8888
-> Result<Duration, Duration> {
8989
if self >= other {
9090
Ok(if self.t.tv_usec >= other.t.tv_usec {
91-
Duration::new(self.t.tv_sec as u64 - other.t.tv_sec as u64,
92-
(self.t.tv_usec as u32 -
93-
other.t.tv_usec as u32) * 1000)
91+
Duration::new((self.t.tv_sec - other.t.tv_sec) as u64,
92+
((self.t.tv_usec -
93+
other.t.tv_usec) as u32) * 1000)
9494
} else {
95-
Duration::new(self.t.tv_sec as u64 - 1 - other.t.tv_sec as u64,
95+
Duration::new((self.t.tv_sec - 1 - other.t.tv_sec) as u64,
9696
(self.t.tv_usec as u32 + (USEC_PER_SEC as u32) -
9797
other.t.tv_usec as u32) * 1000)
9898
})

0 commit comments

Comments
 (0)