From 04531357b40a37fcbcc164ddd3c38222507c3e5a Mon Sep 17 00:00:00 2001 From: pwygab <88221256+merelymyself@users.noreply.github.com> Date: Fri, 29 Mar 2024 04:40:45 +0800 Subject: [PATCH] Exposed the recursion limit value as a config option (#12308) # Description Closes #12253. Exposes the option as "recursion_limit" under config. # User-Facing Changes The config file now has a new option! # After Submitting Nothing else...? Do let me know if there's something I've missed! --- crates/nu-engine/src/eval.rs | 6 +++--- crates/nu-protocol/src/config/mod.rs | 15 +++++++++++++++ .../nu-utils/src/sample_config/default_config.nu | 1 + 3 files changed, 19 insertions(+), 3 deletions(-) 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.