Skip to content
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Bug fixes
* Rhai and Rhai Grain now properly enforce object map size limits in all mutation scenarios ([`#1214`](https://github.com/rhaiscript/rhai/issues/1214)).
* (Fuzzing) Fixed panic when assigning to a property of a constant object.
* Fixed undefined behavior (an aliasing violation detected by Miri) when a variable is passed to a function that does not take its first argument by `&mut`, e.g. `x.contains("a")` or `f(x)` for a script function. This was a regression in 1.27.0.
* `sort` and `sort_desc` no longer panic on an array of floating-point numbers that contains NaN. NaN now sorts after every other number (before them for `sort_desc`) (thanks [`@Rockyrok77`](https://github.com/Rockyrok77)).

Enhancements
------------
Expand Down
10 changes: 8 additions & 2 deletions src/packages/array_basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1668,7 +1668,9 @@ pub mod array_functions {
array.sort_by(|a, b| {
let a = a.as_float().unwrap();
let b = b.as_float().unwrap();
a.partial_cmp(&b).unwrap_or(Ordering::Equal)
// NaN sorts after every number, otherwise the order is not total and `sort_by` may panic.
a.partial_cmp(&b)
.unwrap_or_else(|| a.is_nan().cmp(&b.is_nan()))
});
return Ok(());
}
Expand Down Expand Up @@ -1775,7 +1777,11 @@ pub mod array_functions {
array.sort_by(|a, b| {
let a = a.as_float().unwrap();
let b = b.as_float().unwrap();
match a.partial_cmp(&b).unwrap_or(Ordering::Equal) {
// NaN sorts before every number, otherwise the order is not total and `sort_by` may panic.
match a
.partial_cmp(&b)
.unwrap_or_else(|| a.is_nan().cmp(&b.is_nan()))
{
Ordering::Less => Ordering::Greater,
Ordering::Equal => Ordering::Equal,
Ordering::Greater => Ordering::Less,
Expand Down
21 changes: 21 additions & 0 deletions tests/arrays.rs
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,27 @@ fn test_arrays_index_of() {
assert_eq!(engine.eval::<INT>(r#"index_of([1,2,3], "foo")"#).unwrap(), -1);
}

#[test]
#[cfg(not(feature = "no_float"))]
#[cfg(not(feature = "no_object"))]
fn test_arrays_sort_nan() {
let engine = Engine::new();

// 60 numbers, every fourth one NaN.
let setup = "let a = []; for i in 0..60 { a.push(if i % 4 == 0 { 0.0 / 0.0 } else { (i * 37 % 61).to_float() }); }";
let floats = |a: Array| a.iter().map(|v| v.as_float().unwrap()).collect::<Vec<_>>();

let sorted = floats(engine.eval::<Array>(&format!("{setup} a.sort(); a")).unwrap());
assert_eq!(sorted.len(), 60);
assert!(sorted[..45].windows(2).all(|w| w[0] <= w[1]));
assert!(sorted[45..].iter().all(|v| v.is_nan()));

let sorted = floats(engine.eval::<Array>(&format!("{setup} a.sort_desc(); a")).unwrap());
assert_eq!(sorted.len(), 60);
assert!(sorted[..15].iter().all(|v| v.is_nan()));
assert!(sorted[15..].windows(2).all(|w| w[0] >= w[1]));
}

#[test]
#[cfg(not(feature = "no_object"))]
#[cfg(not(feature = "no_function"))]
Expand Down
Loading