Skip to content

Correction parser panicked with index out of bounds - #8469

Closed
tcoratger wants to merge 2 commits into
nushell:mainfrom
tcoratger:test_parser_index
Closed

tcoratger wants to merge 2 commits into
nushell:mainfrom
tcoratger:test_parser_index

Conversation

@tcoratger

Copy link
Copy Markdown
Contributor

Description

Issue #8385 reported a panicked problem using the def define custom command and wrong uses that should not result in a panick exit anyway.

Definition

def nuu [ one: list two: list three: list four: list five: list six: list ] { }

Usages leading to panick output

  • nuu [] [] [] [] [] []
  • nuu 0 0 0 0
  • nuu 00 00 00 00 00

Coding bug

The bug was in the crates/nu-parser/src/parser.rs file and more precisely in the calculate_end_span function. Part of this function was initially:

if signature.num_positionals_after(positional_idx) == 0 {
    spans.len()
} else if positional_idx < signature.required_positional.len()
    && spans.len() > (signature.required_positional.len() - positional_idx)
{
    spans.len() - (signature.required_positional.len() - positional_idx - 1)
} else {
    spans_idx + 1
}

And here, the positional_idx < signature.required_positional.len() && spans.len() > (signature.required_positional.len() - positional_idx) condition is well adapted to situations like if 2 < 3 { 3 } but in our case we should never enter this else if statement. As this is linked to the Category of the problem, the fix was just to add a conditional limitation there to avoid any Category::Default going into this condition, resulting in the following:

if signature.num_positionals_after(positional_idx) == 0 {
      spans.len()
  } else if positional_idx < signature.required_positional.len()
      && spans.len() > (signature.required_positional.len() - positional_idx)
      && signature.category != Category::Default
  {
      spans.len() - (signature.required_positional.len() - positional_idx - 1)
  } else {
      spans_idx + 1
  }

Tests

Tests are added in the src/tests/test_parser.rs file to avoid this panick issue appearing again.

#[test]
fn def_list_six_elements_empty_lists_call() -> TestResult {
    run_test(
        "def nuu [
        one: list
        two: list
        three: list
        four: list
        five: list
        six: list
        ] { }; nuu [] [] [] [] [] [];",
        "",
    )
}

#[test]
fn def_list_six_elements_four_zero_call() -> TestResult {
    fail_test(
        "def nuu [
        one: list
        two: list
        three: list
        four: list
        five: list
        six: list
        ] { }; nuu 0 0 0 0;",
        "parse_mismatch",
    )
}

#[test]
fn def_list_six_elements_five_double_zero_call() -> TestResult {
    fail_test(
        "def nuu [
        one: list
        two: list
        three: list
        four: list
        five: list
        six: list
        ] { }; nuu 00 00 00 00 00;",
        "parse_mismatch",
    )
}

@codecov

codecov Bot commented Mar 16, 2023

Copy link
Copy Markdown

Codecov Report

Merging #8469 (0446f70) into main (aa876ce) will increase coverage by 0.00%.
The diff coverage is 100.00%.

Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff           @@
##             main    #8469   +/-   ##
=======================================
  Coverage   68.06%   68.06%           
=======================================
  Files         621      621           
  Lines       99885    99886    +1     
=======================================
+ Hits        67988    67991    +3     
+ Misses      31897    31895    -2     
Impacted Files Coverage Δ
crates/nu-parser/src/parser.rs 83.28% <100.00%> (+0.08%) ⬆️

... and 2 files with indirect coverage changes

@rgwood

rgwood commented Mar 16, 2023

Copy link
Copy Markdown
Contributor

Thanks for the PR!

The Category::Default check surprised me. I don't know why the signature category should be used here; can you help me understand that?

@tcoratger

Copy link
Copy Markdown
Contributor Author

@rgwood Maybe I'm wrong there but here is my reasoning. The problem here is the calculation of the end element (let end = calculate_end_span(working_set, &signature, spans, spans_idx, positional_idx);) that was completely false in the nuu 0 0 0 0 case, giving 2 instead of 4 that is expected.

This is because, in this precise scenario, the code entered the positional_idx < signature.required_positional.len() && spans.len() > (signature.required_positional.len() - positional_idx) statement and spans.len() - (signature.required_positional.len() - positional_idx - 1) was applied to calculate end element.

  • If you run if 2 < 3 {3} , then there is a need to enter the positional_idx < signature.required_positional.len() && spans.len() > (signature.required_positional.len() - positional_idx) statement in order to apply spans.len() - (signature.required_positional.len() - positional_idx - 1).

In this case, the signature is:

Signature { name: "if", usage: "", extra_usage: "", search_terms: [], required_positional: [PositionalArg { name: "cond", desc: "condition to check", shape: Expression, var_id: None, default_value: None }, PositionalArg { name: "then_block", desc: "block to run if check succeeds", shape: Block, var_id: None, default_value: None }], optional_positional: [PositionalArg { name: "else_expression", desc: "expression or block to run if check fails", shape: Keyword([101, 108, 115, 101], OneOf([Block, Expression])), var_id: None, default_value: None }], rest_positional: None, vectorizes_over_list: false, named: [Flag { long: "help", short: Some('h'), arg: None, required: false, desc: "Display the help message for this command", var_id: None, default_value: None }], input_type: Any, output_type: Any, input_output_types: [(Any, Any)], allow_variants_without_examples: false, is_filter: false, creates_scope: false, allows_unknown_args: false, category: Core }

Including, signature.category = Core.

  • If you run nuu 0 0 0 0, this statement is a danger and there is no need to go there, instead, spans_idx + 1 statement should be applied. In this case the signature is:

Signature { name: "nuu", usage: "", extra_usage: "", search_terms: [], required_positional: [PositionalArg { name: "one", desc: "", shape: List(Any), var_id: Some(37), default_value: None }, PositionalArg { name: "two", desc: "", shape: List(Any), var_id: Some(38), default_value: None }, PositionalArg { name: "three", desc: "", shape: List(Any), var_id: Some(39), default_value: None }, PositionalArg { name: "four", desc: "", shape: List(Any), var_id: Some(40), default_value: None }, PositionalArg { name: "five", desc: "", shape: List(Any), var_id: Some(41), default_value: None }, PositionalArg { name: "six", desc: "", shape: List(Any), var_id: Some(42), default_value: None }], optional_positional: [], rest_positional: None, vectorizes_over_list: false, named: [Flag { long: "help", short: Some('h'), arg: None, required: false, desc: "Display the help message for this command", var_id: None, default_value: None }], input_type: Any, output_type: Any, input_output_types: [], allow_variants_without_examples: false, is_filter: false, creates_scope: false, allows_unknown_args: false, category: Default }

A notable difference between the two calls (if 2 < 3 {3} and nuu 0 0 0 0) is their category (Core vs Default). To avoid our nuu 0 0 0 0 command to go in the positional_idx < signature.required_positional.len() && spans.len() > (signature.required_positional.len() - positional_idx) statement and directly going into the else in order to apply spans_idx + 1, an idea I had to avoid breaking compatibility with order command is to add the
&& signature.category != Category::Default conditional statement to prevent nuu 0 0 0 0 entering the statement, WDT?

@rgwood

rgwood commented Mar 17, 2023

Copy link
Copy Markdown
Contributor

Thanks. I'm still kind of skeptical that the category is the right thing to check here, but hopefully someone with more parser experience can weigh in.

@tcoratger

Copy link
Copy Markdown
Contributor Author

@rgwood Sure, the major problem was this condition positional_idx < signature.required_positional.len() && spans.len() > (signature.required_positional.len() - positional_idx) that is too specific and not applicable for all of the specific cases.

spans.len()
} else if positional_idx < signature.required_positional.len()
&& spans.len() > (signature.required_positional.len() - positional_idx)
&& signature.category != Category::Default

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we allow custom commands to change their category in the future, this might break. Is there a way we can narrow it down to where the command parsing is being impacted? or improve the logic so we test that we have enough spans ahead of time?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jntrnr Let us take this statement as an example if 2 < 3 { 3 }.

  • This comes with this signature: signature Signature { name: "if", usage: "", extra_usage: "", search_terms: [], required_positional: [PositionalArg { name: "cond", desc: "condition to check", shape: Expression, var_id: None, default_value: None }, PositionalArg { name: "thenOf([Block, Expression])), var_id: None, default_value: None }], rest_positional: None, vectorizes_over_list: false, named: [Flag { long: "help", short: Some('h'), arg: None, required: false, desc: "Display the help message for this command", var_id: None, default_value: None }], input_type: Any, output_type: Any, input_output_types: [(Any, Any)], allow_variants_without_examples: false, is_filter: false, creates_scope: false, allows_unknown_args: false, category: Core }
  • And this span array: [Span { start: 21046, end: 21047 }, Span { start: 21048, end: 21049 }, Span { start: 21050, end: 21051 }, Span { start: 21052, end: 21057 }]

The first positional argument corresponds to 2 < 3 and so it includes 3 span elements but the second is a block { 3 } which contains just 1 span element. And this is what we should be able to know: the number of span elements corresponding to each positional argument, knowing that this can vary as a function of the input statement from the user. But I couldn’t find any function or utility that links the two, do you have an idea on the issue?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if I follow what the issue is. If the issue is that the arguments number isn't being properly counted, changing the end span calculation isn't where you'd want to do the fix.

I'm mostly saying this also because that part of the parser is also pretty nasty to get right. If the fix can be done elsewhere, that'll be a lot easier to test and a lot easier to review.

@amtoine amtoine linked an issue Jun 18, 2023 that may be closed by this pull request
@amtoine

amtoine commented Jun 18, 2023

Copy link
Copy Markdown
Member

@tcoratger
hello there 😋

have you been able to investigate another strategy maybe more robust than relying on the category? 😇

@tcoratger

Copy link
Copy Markdown
Contributor Author

Hi @amtoine

Sorry I've been very busy since, haven't had time to watch again, feel free to take this if you have time :)

@sholderbach

Copy link
Copy Markdown
Member

Thank you for giving this a try @tcoratger

Will close this PR for now

For those that might want to pick this up: We can not rely on the purely metadata property of the command Category to steer core behavior, as we may want to recategorize commands at will.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

parser panicked with index out of bounds

5 participants