From dae1b9a996e7670d534df17ff2a6c5788e53a2e9 Mon Sep 17 00:00:00 2001 From: pwygab <88221256+merelymyself@users.noreply.github.com> Date: Mon, 19 Dec 2022 20:10:02 +0800 Subject: [PATCH] prevent panic with format command (#7522) Related: #7211. Prevents numeric underflow in span. Co-authored-by: sholderbach --- .../nu-command/src/strings/format/command.rs | 21 ++++++++++++++++--- crates/nu-command/tests/commands/format.rs | 13 ++++++++++++ 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/crates/nu-command/src/strings/format/command.rs b/crates/nu-command/src/strings/format/command.rs index 0bd2703092..06776ca468 100644 --- a/crates/nu-command/src/strings/format/command.rs +++ b/crates/nu-command/src/strings/format/command.rs @@ -55,7 +55,11 @@ impl Command for Format { let string_span = pattern.span()?; // the string span is start as `"`, we don't need the character // to generate proper span for sub expression. - let ops = extract_formatting_operations(string_pattern, string_span.start + 1); + let ops = extract_formatting_operations( + string_pattern, + call.head, + string_span.start + 1, + )?; format( input_val, @@ -112,7 +116,11 @@ enum FormatOperation { /// formatted according to the input pattern. /// FormatOperation::ValueNeedEval contains expression which need to eval, it has the following form: /// "$it.column1.column2" or "$variable" -fn extract_formatting_operations(input: String, span_start: usize) -> Vec { +fn extract_formatting_operations( + input: String, + error_span: Span, + span_start: usize, +) -> Result, ShellError> { let mut output = vec![]; let mut characters = input.char_indices(); @@ -148,6 +156,13 @@ fn extract_formatting_operations(input: String, span_start: usize) -> Vec Vec