Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix soundness bug in NotNan::cmp. #151

Merged
merged 1 commit into from
Jun 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ use core::cmp::Ordering;
use core::convert::TryFrom;
use core::fmt;
use core::hash::{Hash, Hasher};
use core::hint::unreachable_unchecked;
use core::iter::{Product, Sum};
use core::num::FpCategory;
use core::ops::{
Expand Down Expand Up @@ -1163,10 +1162,10 @@ impl Borrow<f64> for NotNan<f64> {
#[allow(clippy::derive_ord_xor_partial_ord)]
impl<T: FloatCore> Ord for NotNan<T> {
fn cmp(&self, other: &NotNan<T>) -> Ordering {
match self.partial_cmp(other) {
Some(ord) => ord,
None => unsafe { unreachable_unchecked() },
}
// Can't use unreachable_unchecked because unsafe code can't depend on FloatCore impl.
// https://github.com/reem/rust-ordered-float/issues/150
self.partial_cmp(other)
.expect("partial_cmp failed for non-NaN value")
}
}

Expand Down Expand Up @@ -2535,7 +2534,7 @@ mod impl_rand {
fn uniform_sampling_panic_on_infinity_notnan() {
let (low, high) = (
NotNan::new(0f64).unwrap(),
NotNan::new(core::f64::INFINITY).unwrap(),
NotNan::new(f64::INFINITY).unwrap(),
);
let uniform = Uniform::new(low, high);
let _ = uniform.sample(&mut rand::thread_rng());
Expand All @@ -2544,15 +2543,15 @@ mod impl_rand {
#[test]
#[should_panic]
fn uniform_sampling_panic_on_infinity_ordered() {
let (low, high) = (OrderedFloat(0f64), OrderedFloat(core::f64::INFINITY));
let (low, high) = (OrderedFloat(0f64), OrderedFloat(f64::INFINITY));
let uniform = Uniform::new(low, high);
let _ = uniform.sample(&mut rand::thread_rng());
}

#[test]
#[should_panic]
fn uniform_sampling_panic_on_nan_ordered() {
let (low, high) = (OrderedFloat(0f64), OrderedFloat(core::f64::NAN));
let (low, high) = (OrderedFloat(0f64), OrderedFloat(f64::NAN));
let uniform = Uniform::new(low, high);
let _ = uniform.sample(&mut rand::thread_rng());
}
Expand Down
42 changes: 21 additions & 21 deletions tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -576,72 +576,72 @@ fn hash_is_good_for_fractional_numbers() {
#[test]
#[should_panic]
fn test_add_fails_on_nan() {
let a = not_nan(std::f32::INFINITY);
let b = not_nan(std::f32::NEG_INFINITY);
let a = not_nan(f32::INFINITY);
let b = not_nan(f32::NEG_INFINITY);
let _c = a + b;
}

#[test]
#[should_panic]
fn test_add_fails_on_nan_ref() {
let a = not_nan(std::f32::INFINITY);
let b = not_nan(std::f32::NEG_INFINITY);
let a = not_nan(f32::INFINITY);
let b = not_nan(f32::NEG_INFINITY);
let _c = a + &b;
}

#[test]
#[should_panic]
fn test_add_fails_on_nan_ref_ref() {
let a = not_nan(std::f32::INFINITY);
let b = not_nan(std::f32::NEG_INFINITY);
let a = not_nan(f32::INFINITY);
let b = not_nan(f32::NEG_INFINITY);
let _c = &a + &b;
}

#[test]
#[should_panic]
fn test_add_fails_on_nan_t_ref() {
let a = not_nan(std::f32::INFINITY);
let b = std::f32::NEG_INFINITY;
let a = not_nan(f32::INFINITY);
let b = f32::NEG_INFINITY;
let _c = a + &b;
}

#[test]
#[should_panic]
fn test_add_fails_on_nan_ref_t_ref() {
let a = not_nan(std::f32::INFINITY);
let b = std::f32::NEG_INFINITY;
let a = not_nan(f32::INFINITY);
let b = f32::NEG_INFINITY;
let _c = &a + &b;
}

#[test]
#[should_panic]
fn test_add_fails_on_nan_ref_t() {
let a = not_nan(std::f32::INFINITY);
let b = std::f32::NEG_INFINITY;
let a = not_nan(f32::INFINITY);
let b = f32::NEG_INFINITY;
let _c = &a + b;
}

#[test]
#[should_panic]
fn test_add_assign_fails_on_nan_ref() {
let mut a = not_nan(std::f32::INFINITY);
let b = not_nan(std::f32::NEG_INFINITY);
let mut a = not_nan(f32::INFINITY);
let b = not_nan(f32::NEG_INFINITY);
a += &b;
}

#[test]
#[should_panic]
fn test_add_assign_fails_on_nan_t_ref() {
let mut a = not_nan(std::f32::INFINITY);
let b = std::f32::NEG_INFINITY;
let mut a = not_nan(f32::INFINITY);
let b = f32::NEG_INFINITY;
a += &b;
}

#[test]
#[should_panic]
fn test_add_assign_fails_on_nan_t() {
let mut a = not_nan(std::f32::INFINITY);
let b = std::f32::NEG_INFINITY;
let mut a = not_nan(f32::INFINITY);
let b = f32::NEG_INFINITY;
a += b;
}

Expand Down Expand Up @@ -678,15 +678,15 @@ fn ordered_f64_neg() {
#[test]
#[should_panic]
fn test_sum_fails_on_nan() {
let a = not_nan(std::f32::INFINITY);
let b = not_nan(std::f32::NEG_INFINITY);
let a = not_nan(f32::INFINITY);
let b = not_nan(f32::NEG_INFINITY);
let _c: NotNan<_> = [a, b].iter().sum();
}

#[test]
#[should_panic]
fn test_product_fails_on_nan() {
let a = not_nan(std::f32::INFINITY);
let a = not_nan(f32::INFINITY);
let b = not_nan(0f32);
let _c: NotNan<_> = [a, b].iter().product();
}
Expand Down
Loading