Skip to content

Commit 3b83a68

Browse files
authored
Add null check for args in CommandLineParser (PowerShell#13451)
1 parent 4e2b6e9 commit 3b83a68

3 files changed

Lines changed: 20 additions & 0 deletions

File tree

src/Microsoft.PowerShell.ConsoleHost/host/msh/CommandLineParameterParser.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -712,6 +712,14 @@ internal void Parse(string[] args)
712712
throw new InvalidOperationException("This instance has already been used. Create a new instance.");
713713
}
714714

715+
for (int i = 0; i < args.Length; i++)
716+
{
717+
if (args[i] is null)
718+
{
719+
throw new ArgumentNullException(nameof(args), CommandLineParameterParserStrings.NullElementInArgs);
720+
}
721+
}
722+
715723
// Indicates that we've called this method on this instance, and that when it's done, the state variables
716724
// will reflect the parse.
717725
_dirty = true;

src/Microsoft.PowerShell.ConsoleHost/resources/CommandLineParameterParserStrings.resx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,4 +219,7 @@ Valid formats are:
219219
<data name="STANotImplemented" xml:space="preserve">
220220
<value>Parameter -STA is not supported on this platform.</value>
221221
</data>
222+
<data name="NullElementInArgs" xml:space="preserve">
223+
<value>The specified arguments must not contain null elements.</value>
224+
</data>
222225
</root>

test/xUnit/csharp/test_CommandLineParser.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,15 @@ public static void Test_Throws_On_Reuse()
6565
Assert.Throws<System.InvalidOperationException>(() => cpp.Parse(new string[0]));
6666
}
6767

68+
[Theory]
69+
[InlineData("arg1", null, "arg3")]
70+
public static void Test_ARGS_With_Null(params string[] commandLine)
71+
{
72+
var cpp = new CommandLineParameterParser();
73+
74+
Assert.Throws<System.ArgumentNullException>(() => cpp.Parse(commandLine));
75+
}
76+
6877
[Theory]
6978
[InlineData("noexistfilename")]
7079
public static void TestDefaultParameterIsFileName_Not_Exist(params string[] commandLine)

0 commit comments

Comments
 (0)