-
Notifications
You must be signed in to change notification settings - Fork 292
Avoid IndexOutOfRangeException in command-line parsing #7648
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,6 +23,28 @@ public sealed class ArgumentArityTests | |
| new ExtensionCommandLineProviderMockOptionsWithDifferentArity() | ||
| ]; | ||
|
|
||
| [TestMethod] | ||
| public async Task ParseAndValidate_EmptyArgument_ShouldNotThrowException() | ||
| { | ||
| // Arrange | ||
| string[] args = [string.Empty]; | ||
| CommandLineParseResult parseResult = CommandLineParser.Parse(args, new SystemEnvironment()); | ||
|
|
||
| // Act | ||
| ValidationResult result = await CommandLineOptionsValidator.ValidateAsync(parseResult, _systemCommandLineOptionsProviders, | ||
| _extensionCommandLineOptionsProviders, new Mock<ICommandLineOptions>().Object); | ||
|
|
||
| // Assert | ||
| Assert.IsFalse(result.IsValid); | ||
| #pragma warning disable SA1027 // Use tabs correctly | ||
|
Comment on lines
+38
to
+39
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Disabling SA1027 to embed a literal tab in the raw string is a bit of code smell in a test. If you do keep the exact-match assertion, you can avoid the pragma by constructing the expected string with var expected = string.Join(
Environment.NewLine,
"Invalid command line arguments:",
"\t- Unexpected argument ",
string.Empty);
result.ErrorMessage.Should().Be(expected);Though as noted above, a |
||
| Assert.AreEqual( | ||
| """ | ||
| Invalid command line arguments: | ||
| - Unexpected argument | ||
| """, result.ErrorMessage, StringComparer.Ordinal); | ||
|
Comment on lines
+34
to
+44
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Per repo conventions in
The new test uses result.IsValid.Should().BeFalse();
result.ErrorMessage.Should().Contain("Unexpected argument");Using (The existing tests in the same file also use |
||
| #pragma warning restore SA1027 // Use tabs correctly | ||
|
Youssef1313 marked this conversation as resolved.
|
||
| } | ||
|
|
||
| [TestMethod] | ||
| public async Task ParseAndValidate_WhenOptionWithArityZeroIsCalledWithOneArgument_ReturnsFalse() | ||
| { | ||
|
Youssef1313 marked this conversation as resolved.
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The production fix is correct and minimal — this was the only unguarded indexing on
currentArgin the method. TheStartsWith('@')above is safe on empty strings, and the length checks on lines 68-69 (Length > 1,Length > 2) already had proper guards. 👍