Skip to content

Parser panic for signature with multibyte char for short flag #8821

@sholderbach

Description

@sholderbach

Describe the bug

We currently accept optionally a char for the short version of flags (e.g. https://docs.rs/nu-protocol/latest/nu_protocol/struct.Signature.html#method.named) A char can be any Unicode codepoint and require more than one byte in UTF-8 encoding.

Defining a command with a non-ASCII short character succeeds without error.
Calling this command fails with a panic during some error reporting (that seems unaware of the underlying problem.

How to reproduce

Succeeds:

def nu-arg [--umlaut(-ö): int] {}

Succeeds:

nu-arg

Panics:

nu-arg -ö

Expected behavior

Either we completely deny you the option to use non-ASCII shortflags or properly handle UTF-8 encoded characters.
This means in several places we need to use char::len_utf8()

Screenshots

Error output and backtrace:

Error: nu::parser::unknown_flag

  × The `nu-arg` command doesn't have flag `-�`.
   ╭─[entry #2:1:1]
 1 │ nu-arg -ö
   · thread 'main' panicked at 'byte index 9 is not a char boundary; it is inside 'ö' (bytes 8..10) of `nu-arg -ö`', /home/stefan/.cargo/registry/src/github.com-1ecc6299db9ec823/miette-5.7.0/src/handlers/graphical.rs:625:21
stack backtrace:
   0: rust_begin_unwind
             at /rustc/90743e7298aca107ddaa0c202a4d3604e29bfeb6/library/std/src/panicking.rs:575:5
   1: core::panicking::panic_fmt
             at /rustc/90743e7298aca107ddaa0c202a4d3604e29bfeb6/library/core/src/panicking.rs:65:14
   2: core::str::slice_error_fail_rt
   3: core::str::slice_error_fail
             at /rustc/90743e7298aca107ddaa0c202a4d3604e29bfeb6/library/core/src/str/mod.rs:86:9
   4: core::str::traits::<impl core::slice::index::SliceIndex<str> for core::ops::range::RangeTo<usize>>::index
             at /rustc/90743e7298aca107ddaa0c202a4d3604e29bfeb6/library/core/src/str/traits.rs:294:21
   5: core::str::traits::<impl core::ops::index::Index<I> for str>::index
             at /rustc/90743e7298aca107ddaa0c202a4d3604e29bfeb6/library/core/src/str/traits.rs:65:9
   6: <alloc::string::String as core::ops::index::Index<core::ops::range::RangeTo<usize>>>::index
             at /rustc/90743e7298aca107ddaa0c202a4d3604e29bfeb6/library/alloc/src/string.rs:2371:10
   7: miette::handlers::graphical::GraphicalReportHandler::visual_offset
             at /home/stefan/.cargo/registry/src/github.com-1ecc6299db9ec823/miette-5.7.0/src/handlers/graphical.rs:625:21
   8: miette::handlers::graphical::GraphicalReportHandler::render_single_line_highlights::{{closure}}
             at /home/stefan/.cargo/registry/src/github.com-1ecc6299db9ec823/miette-5.7.0/src/handlers/graphical.rs:676:27
   9: core::iter::adapters::map::map_fold::{{closure}}
             at /rustc/90743e7298aca107ddaa0c202a4d3604e29bfeb6/library/core/src/iter/adapters/map.rs:84:28
  10: core::iter::traits::iterator::Iterator::fold
             at /rustc/90743e7298aca107ddaa0c202a4d3604e29bfeb6/library/core/src/iter/traits/iterator.rs:2414:21
  11: <core::iter::adapters::map::Map<I,F> as core::iter::traits::iterator::Iterator>::fold
             at /rustc/90743e7298aca107ddaa0c202a4d3604e29bfeb6/library/core/src/iter/adapters/map.rs:124:9
  12: core::iter::traits::iterator::Iterator::for_each
             at /rustc/90743e7298aca107ddaa0c202a4d3604e29bfeb6/library/core/src/iter/traits/iterator.rs:831:9
  13: <alloc::vec::Vec<T,A> as alloc::vec::spec_extend::SpecExtend<T,I>>::spec_extend
             at /rustc/90743e7298aca107ddaa0c202a4d3604e29bfeb6/library/alloc/src/vec/spec_extend.rs:40:17
  14: <alloc::vec::Vec<T> as alloc::vec::spec_from_iter_nested::SpecFromIterNested<T,I>>::from_iter
             at /rustc/90743e7298aca107ddaa0c202a4d3604e29bfeb6/library/alloc/src/vec/spec_from_iter_nested.rs:62:9
  15: <alloc::vec::Vec<T> as alloc::vec::spec_from_iter::SpecFromIter<T,I>>::from_iter
             at /rustc/90743e7298aca107ddaa0c202a4d3604e29bfeb6/library/alloc/src/vec/spec_from_iter.rs:33:9
  16: <alloc::vec::Vec<T> as core::iter::traits::collect::FromIterator<T>>::from_iter
             at /rustc/90743e7298aca107ddaa0c202a4d3604e29bfeb6/library/alloc/src/vec/mod.rs:2757:9
  17: core::iter::traits::iterator::Iterator::collect
             at /rustc/90743e7298aca107ddaa0c202a4d3604e29bfeb6/library/core/src/iter/traits/iterator.rs:1836:9
  18: miette::handlers::graphical::GraphicalReportHandler::render_single_line_highlights
             at /home/stefan/.cargo/registry/src/github.com-1ecc6299db9ec823/miette-5.7.0/src/handlers/graphical.rs:670:36
  19: miette::handlers::graphical::GraphicalReportHandler::render_context
             at /home/stefan/.cargo/registry/src/github.com-1ecc6299db9ec823/miette-5.7.0/src/handlers/graphical.rs:457:17
  20: miette::handlers::graphical::GraphicalReportHandler::render_snippets
             at /home/stefan/.cargo/registry/src/github.com-1ecc6299db9ec823/miette-5.7.0/src/handlers/graphical.rs:362:25
  21: miette::handlers::graphical::GraphicalReportHandler::render_report
             at /home/stefan/.cargo/registry/src/github.com-1ecc6299db9ec823/miette-5.7.0/src/handlers/graphical.rs:157:9
  22: <miette::handlers::graphical::GraphicalReportHandler as miette::eyreish::ReportHandler>::debug
             at /home/stefan/.cargo/registry/src/github.com-1ecc6299db9ec823/miette-5.7.0/src/handlers/graphical.rs:814:9
  23: <miette::handler::MietteHandler as miette::eyreish::ReportHandler>::debug
             at /home/stefan/.cargo/registry/src/github.com-1ecc6299db9ec823/miette-5.7.0/src/handler.rs:321:9
  24: <nu_protocol::cli_error::CliError as core::fmt::Debug>::fmt
             at ./crates/nu-protocol/src/cli_error.rs:57:17
  25: core::fmt::write
             at /rustc/90743e7298aca107ddaa0c202a4d3604e29bfeb6/library/core/src/fmt/mod.rs:1209:17
  26: std::io::Write::write_fmt
             at /rustc/90743e7298aca107ddaa0c202a4d3604e29bfeb6/library/std/src/io/mod.rs:1682:15
  27: <&std::io::stdio::Stderr as std::io::Write>::write_fmt
             at /rustc/90743e7298aca107ddaa0c202a4d3604e29bfeb6/library/std/src/io/stdio.rs:935:9
  28: <std::io::stdio::Stderr as std::io::Write>::write_fmt
             at /rustc/90743e7298aca107ddaa0c202a4d3604e29bfeb6/library/std/src/io/stdio.rs:909:9
  29: std::io::stdio::print_to
             at /rustc/90743e7298aca107ddaa0c202a4d3604e29bfeb6/library/std/src/io/stdio.rs:1008:21
  30: std::io::stdio::_eprint
             at /rustc/90743e7298aca107ddaa0c202a4d3604e29bfeb6/library/std/src/io/stdio.rs:1086:5
  31: nu_protocol::cli_error::report_error
             at ./crates/nu-protocol/src/cli_error.rs:25:5
  32: nu_cli::util::eval_source
             at ./crates/nu-cli/src/util.rs:228:13
  33: nu_cli::repl::evaluate_repl
             at ./crates/nu-cli/src/repl.rs:574:21
  34: nu::run::run_repl
             at ./src/run.rs:239:19
  35: nu::main
             at ./src/main.rs:252:9
  36: core::ops::function::FnOnce::call_once
             at /rustc/90743e7298aca107ddaa0c202a4d3604e29bfeb6/library/core/src/ops/function.rs:251:5

Configuration

key value
version 0.78.1
branch main
commit_hash 637283f
build_os linux-x86_64
build_target x86_64-unknown-linux-gnu
rust_version rustc 1.66.1 (90743e729 2023-01-10)
rust_channel 1.66.1-x86_64-unknown-linux-gnu
cargo_version cargo 1.66.1 (ad779e08b 2023-01-10)
build_time 2023-04-08 23:36:07 +02:00
build_rust_channel debug
features default, zip
installed_plugins

Additional context

Noticed this while looking to review https://github.com/nushell/nushell/pull/8808/files

Saw that we had previously made the assumption that a short flag is only one byte long.
The backtrace I was able to produce from the minimal example seems to crash at some different place.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions