Skip to content

Commit

Permalink
Merge pull request #881 from schungx/master
Browse files Browse the repository at this point in the history
Fix stack overflow bug.
  • Loading branch information
schungx authored May 23, 2024
2 parents 7c85d3c + dc43481 commit 308d07a
Showing 3 changed files with 26 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -13,6 +13,7 @@ Bug fixes
* The `sync` feature now works properly in `no-std` builds (thanks [`@misssonder`](https://github.com/misssonder) [874](https://github.com/rhaiscript/rhai/pull/874)).
* More data-race conditions are caught and returned as errors instead of panicking.
* Missing `min` and `max` functions where both operands are floats or `Decimal` are added.
* Fixed stack overflow when calling closures recursively (thanks [`@MageWeiG`](https://github.com/MageWeiG) [880](https://github.com/rhaiscript/rhai/issues/880)).

New features
------------
8 changes: 8 additions & 0 deletions src/func/call.rs
Original file line number Diff line number Diff line change
@@ -754,6 +754,8 @@ impl Engine {
let scope = &mut Scope::new();
let environ = fn_ptr.environ.as_ref().map(<_>::as_ref);

defer! { let orig_level = global.level; global.level += 1 }

self.call_script_fn(
global, caches, scope, None, environ, fn_def, args, true, pos,
)
@@ -832,6 +834,8 @@ impl Engine {
let this_ptr = Some(target.as_mut());
let environ = environ.as_deref();

defer! { let orig_level = global.level; global.level += 1 }

self.call_script_fn(
global, caches, scope, this_ptr, environ, &fn_def, args, true, pos,
)
@@ -973,6 +977,8 @@ impl Engine {
let this_ptr = Some(target.as_mut());
let args = &mut call_args.iter_mut().collect::<FnArgsVec<_>>();

defer! { let orig_level = global.level; global.level += 1 }

self.call_script_fn(
global, caches, scope, this_ptr, environ, &fn_def, args, true, pos,
)
@@ -1083,6 +1089,8 @@ impl Engine {
let scope = &mut Scope::new();
let environ = environ.as_deref();

defer! { let orig_level = global.level; global.level += 1 }

return self.call_script_fn(
global, caches, scope, None, environ, &fn_def, args, true, pos,
);
17 changes: 17 additions & 0 deletions tests/stack.rs
Original file line number Diff line number Diff line change
@@ -32,6 +32,23 @@ fn test_stack_overflow_fn_calls() {
.unwrap_err(),
EvalAltResult::ErrorStackOverflow(..)
));

#[cfg(not(feature = "no_function"))]
#[cfg(not(feature = "no_object"))]
assert!(matches!(
*engine
.run(
"
let obj1 = #{
action: || this.action(),
update: |x| this.action()
};
obj1.update(1)
"
)
.unwrap_err(),
EvalAltResult::ErrorStackOverflow(..)
));
}

#[test]

0 comments on commit 308d07a

Please sign in to comment.