Skip to content

Commit 02b92c7

Browse files
committed
Fix soundness bug in NotNan::partial_cmp.
Ill-behaved FloatCore implementations for user types could have where `x.partial_cmp(&x) == None` even when `x.is_nan() == false`. This crate will now panic in those cases, rather than execute undefined behavior. Fixes #150.
1 parent 8eb3455 commit 02b92c7

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

src/lib.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ use core::cmp::Ordering;
1515
use core::convert::TryFrom;
1616
use core::fmt;
1717
use core::hash::{Hash, Hasher};
18-
use core::hint::unreachable_unchecked;
1918
use core::iter::{Product, Sum};
2019
use core::num::FpCategory;
2120
use core::ops::{
@@ -1163,9 +1162,11 @@ impl Borrow<f64> for NotNan<f64> {
11631162
#[allow(clippy::derive_ord_xor_partial_ord)]
11641163
impl<T: FloatCore> Ord for NotNan<T> {
11651164
fn cmp(&self, other: &NotNan<T>) -> Ordering {
1165+
// Can't use unreachable_unchecked because unsafe code can't depend on FloatCore impl.
1166+
// https://github.com/reem/rust-ordered-float/issues/150
11661167
match self.partial_cmp(other) {
11671168
Some(ord) => ord,
1168-
None => unsafe { unreachable_unchecked() },
1169+
None => unreachable!(),
11691170
}
11701171
}
11711172
}

0 commit comments

Comments
 (0)