diff --git a/crates/nu-engine/src/eval.rs b/crates/nu-engine/src/eval.rs index 0541125ed7..c56cc8d223 100644 --- a/crates/nu-engine/src/eval.rs +++ b/crates/nu-engine/src/eval.rs @@ -47,12 +47,12 @@ pub fn eval_call( // To prevent a stack overflow in user code from crashing the shell, // we limit the recursion depth of function calls. // Picked 50 arbitrarily, should work on all architectures. - const MAXIMUM_CALL_STACK_DEPTH: u64 = 50; + let maximum_call_stack_depth: u64 = engine_state.config.recursion_limit as u64; callee_stack.recursion_count += 1; - if callee_stack.recursion_count > MAXIMUM_CALL_STACK_DEPTH { + if callee_stack.recursion_count > maximum_call_stack_depth { callee_stack.recursion_count = 0; return Err(ShellError::RecursionLimitReached { - recursion_limit: MAXIMUM_CALL_STACK_DEPTH, + recursion_limit: maximum_call_stack_depth, span: block.span, }); } diff --git a/crates/nu-protocol/src/config/mod.rs b/crates/nu-protocol/src/config/mod.rs index 72c1c9a7c8..59c24fac0b 100644 --- a/crates/nu-protocol/src/config/mod.rs +++ b/crates/nu-protocol/src/config/mod.rs @@ -61,6 +61,7 @@ pub struct Config { pub footer_mode: FooterMode, pub float_precision: i64, pub max_external_completion_results: i64, + pub recursion_limit: i64, pub filesize_format: String, pub use_ansi_coloring: bool, pub quick_completions: bool, @@ -133,6 +134,7 @@ impl Default for Config { completion_algorithm: CompletionAlgorithm::default(), enable_external_completion: true, max_external_completion_results: 100, + recursion_limit: 50, external_completer: None, use_ls_colors_completions: true, @@ -753,6 +755,19 @@ impl Value { value, &mut errors); } + "recursion_limit" => { + if let Value::Int { val, internal_span } = value { + if val > &mut 1 { + config.recursion_limit = *val; + } else { + report_invalid_value("should be a integer greater than 1", span, &mut errors); + *value = Value::Int { val: 50, internal_span: *internal_span }; + } + } else { + report_invalid_value("should be a integer greater than 1", span, &mut errors); + *value = Value::Int { val: 50, internal_span: value.span() }; + } + } // Catch all _ => { report_invalid_key(&[key], span, &mut errors); diff --git a/crates/nu-utils/src/sample_config/default_config.nu b/crates/nu-utils/src/sample_config/default_config.nu index 411825e775..5286f8b53a 100644 --- a/crates/nu-utils/src/sample_config/default_config.nu +++ b/crates/nu-utils/src/sample_config/default_config.nu @@ -238,6 +238,7 @@ $env.config = { render_right_prompt_on_last_line: false # true or false to enable or disable right prompt to be rendered on last line of the prompt. use_kitty_protocol: false # enables keyboard enhancement protocol implemented by kitty console, only if your terminal support this. highlight_resolved_externals: false # true enables highlighting of external commands in the repl resolved by which. + recursion_limit: 50 # the maximum number of times nushell allows recursion before stopping it plugins: {} # Per-plugin configuration. See https://www.nushell.sh/contributor-book/plugins.html#configuration.