From 9516a7e32b4070d21705a39ae57c2702d82744a2 Mon Sep 17 00:00:00 2001 From: Jacob Pratt Date: Sun, 25 Sep 2022 04:24:00 -0400 Subject: [PATCH] Panic if NaN is passed to `Duration` constructors --- src/duration.rs | 6 ++++++ tests/integration/duration.rs | 2 ++ 2 files changed, 8 insertions(+) diff --git a/src/duration.rs b/src/duration.rs index 4499c7b8e..f8d916f45 100644 --- a/src/duration.rs +++ b/src/duration.rs @@ -323,6 +323,9 @@ impl Duration { if seconds > i64::MAX as f64 || seconds < i64::MIN as f64 { crate::expect_failed("overflow constructing `time::Duration`"); } + if seconds.is_nan() { + crate::expect_failed("passed NaN to `time::Duration::seconds_f64`"); + } Self::new_unchecked(seconds as _, ((seconds % 1.) * 1_000_000_000.) as _) } @@ -337,6 +340,9 @@ impl Duration { if seconds > i64::MAX as f32 || seconds < i64::MIN as f32 { crate::expect_failed("overflow constructing `time::Duration`"); } + if seconds.is_nan() { + crate::expect_failed("passed NaN to `time::Duration::seconds_f32`"); + } Self::new_unchecked(seconds as _, ((seconds % 1.) * 1_000_000_000.) as _) } diff --git a/tests/integration/duration.rs b/tests/integration/duration.rs index 285db91aa..b11ccb2c4 100644 --- a/tests/integration/duration.rs +++ b/tests/integration/duration.rs @@ -186,6 +186,7 @@ fn seconds_f64() { assert_panic!(Duration::seconds_f64(f64::MAX)); assert_panic!(Duration::seconds_f64(f64::MIN)); + assert_panic!(Duration::seconds_f64(f64::NAN)); } #[test] @@ -206,6 +207,7 @@ fn seconds_f32() { assert_panic!(Duration::seconds_f32(f32::MAX)); assert_panic!(Duration::seconds_f32(f32::MIN)); + assert_panic!(Duration::seconds_f32(f32::NAN)); } #[test]