Skip to content

Commit 9516a7e

Browse files
committed
Panic if NaN is passed to Duration constructors
1 parent 8cb9c44 commit 9516a7e

File tree

2 files changed

+8
-0
lines changed

2 files changed

+8
-0
lines changed

src/duration.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,9 @@ impl Duration {
323323
if seconds > i64::MAX as f64 || seconds < i64::MIN as f64 {
324324
crate::expect_failed("overflow constructing `time::Duration`");
325325
}
326+
if seconds.is_nan() {
327+
crate::expect_failed("passed NaN to `time::Duration::seconds_f64`");
328+
}
326329
Self::new_unchecked(seconds as _, ((seconds % 1.) * 1_000_000_000.) as _)
327330
}
328331

@@ -337,6 +340,9 @@ impl Duration {
337340
if seconds > i64::MAX as f32 || seconds < i64::MIN as f32 {
338341
crate::expect_failed("overflow constructing `time::Duration`");
339342
}
343+
if seconds.is_nan() {
344+
crate::expect_failed("passed NaN to `time::Duration::seconds_f32`");
345+
}
340346
Self::new_unchecked(seconds as _, ((seconds % 1.) * 1_000_000_000.) as _)
341347
}
342348

tests/integration/duration.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ fn seconds_f64() {
186186

187187
assert_panic!(Duration::seconds_f64(f64::MAX));
188188
assert_panic!(Duration::seconds_f64(f64::MIN));
189+
assert_panic!(Duration::seconds_f64(f64::NAN));
189190
}
190191

191192
#[test]
@@ -206,6 +207,7 @@ fn seconds_f32() {
206207

207208
assert_panic!(Duration::seconds_f32(f32::MAX));
208209
assert_panic!(Duration::seconds_f32(f32::MIN));
210+
assert_panic!(Duration::seconds_f32(f32::NAN));
209211
}
210212

211213
#[test]

0 commit comments

Comments
 (0)