Skip to content

Parser crashes when variable name in let expression is missing #10380

Closed

Description

@anka-213

Describe the bug

I get index out of bounds crash when typing a program like this:

let = a | b

I ran into this when trying to rename a variable

How to reproduce

  1. Launch nu
  2. Write let = a | b in the terminal
  3. nu immediately crashes

Expected behavior

I expect the parser to not be partial or at least that any panic from the parser would be caught instead of crashing the full shell.

Screenshots

No response

Configuration

key value
version 0.83.1
branch
commit_hash
build_os macos-x86_64
build_target x86_64-apple-darwin
rust_version rustc 1.71.1 (eb26296b5 2023-08-03) (built from a source tarball)
cargo_version cargo 1.71.1
build_time 1980-01-01 00:00:00 +00:00
build_rust_channel release
allocator standard
features default, sqlite, trash, which, zip
installed_plugins

Additional context

Stack trace:

thread 'main' panicked at 'index out of bounds: the len is 0 but the index is 0', crates/nu-parser/src/parser.rs:3068:47                                                             09/15/2023 10:50:32 AM
stack backtrace:
   0: _rust_begin_unwind
   1: core::panicking::panic_fmt
   2: core::panicking::panic_bounds_check
   3: nu_parser::parser::parse_var_with_opt_type
   4: nu_parser::parse_keywords::parse_let
   5: nu_parser::parser::parse_builtin_commands
   6: nu_parser::parser::parse_block
   7: nu_parser::parser::parse
   8: <nu_cli::syntax_highlight::NuHighlighter as reedline::highlighter::Highlighter>::highlight
   9: reedline::engine::Reedline::repaint
  10: reedline::engine::Reedline::read_line
  11: nu_cli::repl::evaluate_repl
  12: nu::run::run_repl
  13: nu::main
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

Activity

  1. added
    status:needs-triage
    on Sep 15, 2023
  2. anka-213 commented on Sep 15, 2023

    @anka-213
    ContributorAuthor

    Here is the line that the stack trace mentions:

    let bytes = working_set.get_span_contents(spans[*spans_idx]).to_vec();

    It does indeed seem to index an array without checking that it's non-empty first.

  3. anka-213 commented on Sep 15, 2023

    @anka-213
    ContributorAuthor

    Oh, I see now that it has already been fixed in master, thanks to #9851.

  4. anka-213 commented on Sep 15, 2023

    @anka-213
    ContributorAuthor

    It would still be nice to do something systematic to prevent this kinds of errors from popping up though

  5. sholderbach commented on Sep 15, 2023

    @sholderbach
    Member

    Yeah more testing and also fuzzing of the parser would definitely be appreciated.

    #9417 (comment) proposed a fuzzing target, we should probably set up.

  6. anka-213 commented on Sep 15, 2023

    @anka-213
    ContributorAuthor

    @sholderbach I'm currently working on another way of making the parser code more robust by using primitives that cannot crash instead of sprinkling things like spans[*span_idx] all over the code. See #10381 for my current draft.

  7. tokatoka commented on Sep 15, 2023

    @tokatoka
    Contributor

    #9417 (comment) proposed a fuzzing target, we should probably set up.

    i'll add it in #10376

  8. sholderbach commented on Sep 15, 2023

    @sholderbach
    Member

    Awesome thanks!

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    status:needs-triage

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions