Skip to content

Commit 6b132e5

Browse files
authored
Merge pull request #1059 from schungx/master
Fix no-std builds
2 parents 138b1fb + 1af17ae commit 6b132e5

File tree

3 files changed

+17
-9
lines changed

3 files changed

+17
-9
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Breaking Changes
1212
Bug fixes
1313
---------
1414

15-
* (Fuzzing) Fixed panic when using `sort` on an array with a comparer function that does not implement a [total order](https://en.wikipedia.org/wiki/Total_order).
15+
* (Fuzzing) Fixed panic when using `sort` on an array with a comparer function that does not implement a [total order](https://en.wikipedia.org/wiki/Total_order) [#1058](https://github.com/rhaiscript/rhai/pull/1058).
1616

1717
Enhancements
1818
------------

src/api/deprecated.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1343,8 +1343,7 @@ pub mod deprecated_array_functions {
13431343
array: &mut Array,
13441344
comparer: &str,
13451345
) -> RhaiResultOf<()> {
1346-
sort_by(ctx, array, FnPtr::new(comparer)?);
1347-
Ok(())
1346+
sort_by(ctx, array, FnPtr::new(comparer)?)
13481347
}
13491348
/// Remove all elements in the array that returns `true` when applied a function named by `filter`
13501349
/// and return them as a new array.

src/packages/array_basic.rs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1557,9 +1557,7 @@ pub mod array_functions {
15571557
return Ok(());
15581558
}
15591559

1560-
// `slice::sort_by` may panic if the comparer function does not implement a total order.
1561-
// Catch this instead.
1562-
std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
1560+
let closure = || {
15631561
array.sort_by(|x, y| {
15641562
comparer
15651563
.call_raw(&ctx, None, [x.clone(), y.clone()])
@@ -1579,10 +1577,21 @@ pub mod array_functions {
15791577
},
15801578
)
15811579
});
1582-
}))
1583-
.map_err(|_| {
1580+
};
1581+
1582+
// `slice::sort_by` may panic if the comparer function does not implement a total order.
1583+
// Catch this instead.
1584+
#[cfg(not(feature = "no_std"))]
1585+
return std::panic::catch_unwind(std::panic::AssertUnwindSafe(closure)).map_err(|_| {
15841586
ERR::ErrorRuntime("error in comparer for sorting".into(), Position::NONE).into()
1585-
})
1587+
});
1588+
1589+
#[cfg(feature = "no_std")]
1590+
{
1591+
let mut closure = closure;
1592+
closure();
1593+
Ok(())
1594+
}
15861595
}
15871596
/// Sort the array.
15881597
///

0 commit comments

Comments
 (0)