From 8410ca663258423cd16b0741f7a053b320e13da0 Mon Sep 17 00:00:00 2001 From: kennytm Date: Fri, 1 Sep 2017 01:16:24 +0800 Subject: Properly detect overflow in Instance +/- Duration. Avoid unchecked cast from `u64` to `i64`. Use `try_into()` for checked cast. (On Unix, cast to `time_t` instead of `i64`.) --- src/libstd/sys/windows/time.rs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'src/libstd/sys/windows') diff --git a/src/libstd/sys/windows/time.rs b/src/libstd/sys/windows/time.rs index ef8ed606526..1be29b5139a 100644 --- a/src/libstd/sys/windows/time.rs +++ b/src/libstd/sys/windows/time.rs @@ -16,6 +16,7 @@ use sys::c; use sys::cvt; use sys_common::mul_div_u64; use time::Duration; +use convert::TryInto; const NANOS_PER_SEC: u64 = 1_000_000_000; const INTERVALS_PER_SEC: u64 = NANOS_PER_SEC / 100; @@ -173,9 +174,11 @@ impl From for SystemTime { } fn dur2intervals(d: &Duration) -> i64 { - d.as_secs().checked_mul(INTERVALS_PER_SEC).and_then(|i| { - i.checked_add(d.subsec_nanos() as u64 / 100) - }).expect("overflow when converting duration to intervals") as i64 + d.as_secs() + .checked_mul(INTERVALS_PER_SEC) + .and_then(|i| i.checked_add(d.subsec_nanos() as u64 / 100)) + .and_then(|i| i.try_into().ok()) + .expect("overflow when converting duration to intervals") } fn intervals2dur(intervals: u64) -> Duration { -- cgit 1.4.1-3-g733a5