Add null check for args in CommandLineParser (#13451)

This commit is contained in:
Ilya
2020-08-28 12:03:30 -07:00
committed by GitHub
parent 4e2b6e9540
commit 3b83a686a1
3 changed files with 20 additions and 0 deletions
@@ -712,6 +712,14 @@ namespace Microsoft.PowerShell
throw new InvalidOperationException("This instance has already been used. Create a new instance.");
}
for (int i = 0; i < args.Length; i++)
{
if (args[i] is null)
{
throw new ArgumentNullException(nameof(args), CommandLineParameterParserStrings.NullElementInArgs);
}
}
// Indicates that we've called this method on this instance, and that when it's done, the state variables
// will reflect the parse.
_dirty = true;
@@ -219,4 +219,7 @@ Valid formats are:
<data name="STANotImplemented" xml:space="preserve">
<value>Parameter -STA is not supported on this platform.</value>
</data>
<data name="NullElementInArgs" xml:space="preserve">
<value>The specified arguments must not contain null elements.</value>
</data>
</root>
@@ -65,6 +65,15 @@ namespace PSTests.Parallel
Assert.Throws<System.InvalidOperationException>(() => cpp.Parse(new string[0]));
}
[Theory]
[InlineData("arg1", null, "arg3")]
public static void Test_ARGS_With_Null(params string[] commandLine)
{
var cpp = new CommandLineParameterParser();
Assert.Throws<System.ArgumentNullException>(() => cpp.Parse(commandLine));
}
[Theory]
[InlineData("noexistfilename")]
public static void TestDefaultParameterIsFileName_Not_Exist(params string[] commandLine)