Skip to content

Do not panic when sorting a float array that contains NaN - #1216

Merged
schungx merged 2 commits into
rhaiscript:mainfrom
Rockyrok77:sort-float-nan
Nov 16, 2026
Merged

schungx merged 2 commits into
rhaiscript:mainfrom
Rockyrok77:sort-float-nan

Conversation

@Rockyrok77

Copy link
Copy Markdown
Contributor

Sorting an array of floats that contains NaN panics:

let a = [];
for i in 0..60 { a.push(if i % 4 == 0 { 0.0 / 0.0 } else { (i * 37 % 61).to_float() }); }
a.sort();   // panics with "user-provided comparison function does not correctly implement a total order"

The built-in float comparison in sort_with_builtin and sort_with_builtin_desc is partial_cmp(..).unwrap_or(Ordering::Equal), so NaN is equal to every number while those numbers are not equal to each other. Since Rust 1.81, slice::sort_by panics when it detects an order like that. sort_by with a custom comparer is already wrapped in catch_unwind for this reason, but the built-in path is not, so sort or sort_desc on a float array holding a NaN (0.0 / 0.0, sqrt(-1.0), parse_float("NaN")) can take down the host with no engine limit in the way. On random arrays of up to 61 floats, a quarter of them NaN, about two thirds of calls panic for each method.

When partial_cmp returns None the comparison now falls back to is_nan(), so NaN sorts after every number for sort and before every number for sort_desc. Everything that is not NaN compares exactly as before, so -0.0 and 0.0 still compare equal and keep their order. I did not use total_cmp because it orders NaN by its sign bit, and the NaN that 0.0 / 0.0 produces is negative on x86 and positive on ARM, so NaN would land at different ends on different machines.

Added test_arrays_sort_nan to tests/arrays.rs. Without the fix it panics on both sort and sort_desc; with it, it passes. cargo test --tests passes with default features and with the f32_float, unchecked, sync, f32_float,only_i32, no_float,decimal and no_index rows of the CI matrix. cargo fmt --check, the CI clippy invocation and cargo +1.66.0 check against Cargo.msrv.lock are clean. CHANGELOG line under 1.27.1's bug fixes.

@schungx

schungx commented Nov 16, 2026

Copy link
Copy Markdown
Collaborator

There is a failed test in CI possibly due to , no_object. Can you fix it?

@schungx schungx added the bug label Nov 16, 2026
@Rockyrok77

Copy link
Copy Markdown
Contributor Author

Fixed. The test script uses method-call syntax (a.push, a.sort), which doesn't parse under no_object, so test_arrays_sort_nan is now gated on not(no_object) like the other method-based tests in tests/arrays.rs. Checked it locally with the testing-environ,no_object,serde,metadata,internals,debugging,grain row. The codegen job failures are a separate thing: stable just moved to rustc 1.100.0 and the trybuild ui_tests stderr no longer matches, and this PR doesn't touch codegen.

@schungx
schungx merged commit 1762feb into rhaiscript:main Nov 16, 2026
54 of 56 checks passed
@Rockyrok77

Copy link
Copy Markdown
Contributor Author

Thanks for merging, glad the NaN sort panic is gone.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants