Skip to content

Commit ab394b9

Browse files
Add panic test for Duration::from_nanos_u128
1 parent e216f1b commit ab394b9

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

library/core/src/time.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -328,12 +328,15 @@ impl Duration {
328328
#[inline]
329329
pub const fn from_nanos_u128(nanos: u128) -> Duration {
330330
const NANOS_PER_SEC: u128 = self::NANOS_PER_SEC as u128;
331-
let secs: u64 = (nanos / NANOS_PER_SEC) as u64;
331+
let secs: u128 = (nanos / NANOS_PER_SEC);
332+
if (secs > u64::MAX.into()) {
333+
panic!("overflow in duration in Duration::from_nanos_u128");
334+
}
332335
let subsec_nanos = (nanos % NANOS_PER_SEC) as u32;
333336
// SAFETY: x % 1_000_000_000 < 1_000_000_000
334337
let subsec_nanos = unsafe { Nanoseconds::new_unchecked(subsec_nanos) };
335338

336-
Duration { secs, nanos: subsec_nanos }
339+
Duration { secs: secs as u64, nanos: subsec_nanos }
337340
}
338341

339342
/// Creates a new `Duration` from the specified number of weeks.

0 commit comments

Comments
 (0)