Skip to content

IndexOutOfRange exception when parsing empty argument #1533

Closed

Description

@baronfel

I ran into this while updating the SDK to the latest version. For the input dotnet add "" (note the explicitly empty argument) the tokenizer throws an IndexOutOfRangeException:

System.IndexOutOfRangeException: Index was outside the bounds of the array.
   at System.String.get_Chars(Int32 index)
   at System.CommandLine.Parsing.StringExtensions.GetPrefixLength(String alias)
   at System.CommandLine.Parsing.StringExtensions.Tokenize(IReadOnlyList`1 args, CommandLineConfiguration configuration)
   at System.CommandLine.Parsing.Parser.Parse(IReadOnlyList`1 arguments, String rawInput)
   at Microsoft.DotNet.Cli.Program.ProcessArgs(String[] args, TimeSpan startupTime, ITelemetry telemetryClient) in C:\Users\chethusk\OSS\dotnet-sdk\src\Cli\dotnet\Program.cs:line 130
   at Microsoft.DotNet.Cli.Program.Main(String[] args) in C:\Users\chethusk\OSS\dotnet-sdk\src\Cli\dotnet\Program.cs:line 76

What's happening here is that in the tokenization loop S.CL eventually creates an Token of type Argument with the empty string as the value, and in the constructor of the Token type GetPrefixLength is called on that value, which indexes into the value directly without checks.

I feel like I could be holding it wrong by passing empty arguments in, but this test passed prior to this update (it tests that a 'missing required argument' message is shown). Should this method be made more defensive, or should the tokenization loop remove the empty token entirely, or some other resolution?

Activity

  1. self-assigned this
    on Dec 16, 2021
  2. added this to the 2.0 GA milestone on Dec 16, 2021
  3. added a commit that references this issue on Dec 16, 2021
    260a559
  4. added a commit that references this issue on Mar 6, 2024
    0ab81a9
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Area-Parser and Binderbug

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions