Skip to content

Commit

Permalink
Fix stack overflow
Browse files Browse the repository at this point in the history
  • Loading branch information
schungx committed Jun 15, 2024
1 parent 423f21e commit 112e6e4
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Bug fixes
* Fixed stack overflow when calling closures recursively (thanks [`@MageWeiG`](https://github.com/MageWeiG) [880](https://github.com/rhaiscript/rhai/issues/880)).
* `Engine::call_fn` and `Engine::call_fn_with_options` now correctly use the `AST`'s `source` field.
* (Fuzzing) Fixed crash when using `..=` in strings.
* A recursive stack-overflow bug in `Dynamic::is_hashable` is fixed.

New features
------------
Expand Down
24 changes: 21 additions & 3 deletions src/types/dynamic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -741,7 +741,6 @@ impl fmt::Debug for Dynamic {
dict: &mut HashSet<*const Dynamic>,
) -> fmt::Result {
match value.0 {
#[cfg(not(feature = "no_closure"))]
Union::Shared(ref cell, ..) => match crate::func::locked_read(cell) {
Some(v) => {
if dict.insert(value) {
Expand Down Expand Up @@ -1267,8 +1266,27 @@ impl Dynamic {
}

#[cfg(not(feature = "no_closure"))]
Union::Shared(ref cell, ..) => {
crate::func::locked_read(cell).map_or(false, |v| v.is_hashable())
Union::Shared(..) => {
#[cfg(feature = "no_std")]
use hashbrown::HashSet;
#[cfg(not(feature = "no_std"))]
use std::collections::HashSet;

// Avoid infinite recursion for shared values in a reference loop.
fn checked_is_hashable(
value: &Dynamic,
dict: &mut HashSet<*const Dynamic>,
) -> bool {
match value.0 {
Union::Shared(ref cell, ..) => match crate::func::locked_read(cell) {
Some(v) => dict.insert(value) && checked_is_hashable(&v, dict),
_ => false,
},
_ => value.is_hashable(),
}
}

checked_is_hashable(self, &mut <_>::default())
}
}
}
Expand Down

0 comments on commit 112e6e4

Please sign in to comment.