From c249922e03dc36414feccd0850ebd29d12925b67 Mon Sep 17 00:00:00 2001 From: Devyn Cairns Date: Fri, 5 Jul 2024 23:56:10 -0700 Subject: [PATCH] move recursion_count check to the top of eval_ir_block --- crates/nu-engine/src/eval_ir.rs | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/crates/nu-engine/src/eval_ir.rs b/crates/nu-engine/src/eval_ir.rs index 33212e0925..ddac2d45c9 100644 --- a/crates/nu-engine/src/eval_ir.rs +++ b/crates/nu-engine/src/eval_ir.rs @@ -19,6 +19,18 @@ pub fn eval_ir_block( block: &Block, input: PipelineData, ) -> Result { + // Rust does not check recursion limits outside of const evaluation. + // But nu programs run in the same process as the shell. + // To prevent a stack overflow in user code from crashing the shell, + // we limit the recursion depth of function calls. + let maximum_call_stack_depth: u64 = engine_state.config.recursion_limit as u64; + if stack.recursion_count > maximum_call_stack_depth { + return Err(ShellError::RecursionLimitReached { + recursion_limit: maximum_call_stack_depth, + span: block.span, + }); + } + if let Some(ir_block) = &block.ir_block { D::enter_block(engine_state, block); @@ -843,18 +855,6 @@ fn eval_call( // what to put where. let block = engine_state.get_block(block_id); - // Rust does not check recursion limits outside of const evaluation. - // But nu programs run in the same process as the shell. - // To prevent a stack overflow in user code from crashing the shell, - // we limit the recursion depth of function calls. - let maximum_call_stack_depth: u64 = engine_state.config.recursion_limit as u64; - if stack.recursion_count > maximum_call_stack_depth { - return Err(ShellError::RecursionLimitReached { - recursion_limit: maximum_call_stack_depth, - span: *ctx.block_span, - }); - } - result = eval_block_with_early_return::(engine_state, &mut stack, block, input); drop(stack);