From b98c4d82584cc8415ffc02139286be8a0690dd03 Mon Sep 17 00:00:00 2001 From: Ilya Date: Sat, 15 Aug 2020 19:40:33 +0500 Subject: [PATCH 1/4] Add null check for args in CommandLineParser --- .../host/msh/CommandLineParameterParser.cs | 8 ++++++++ test/xUnit/csharp/test_CommandLineParser.cs | 9 +++++++++ 2 files changed, 17 insertions(+) diff --git a/src/Microsoft.PowerShell.ConsoleHost/host/msh/CommandLineParameterParser.cs b/src/Microsoft.PowerShell.ConsoleHost/host/msh/CommandLineParameterParser.cs index 11de41ce95f..c71cf640623 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/host/msh/CommandLineParameterParser.cs +++ b/src/Microsoft.PowerShell.ConsoleHost/host/msh/CommandLineParameterParser.cs @@ -700,6 +700,14 @@ internal void Parse(string[] args) 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("The 'args' array contains a null element."); + } + } + // 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; diff --git a/test/xUnit/csharp/test_CommandLineParser.cs b/test/xUnit/csharp/test_CommandLineParser.cs index d4b722b7f4a..3e25c84c1bc 100644 --- a/test/xUnit/csharp/test_CommandLineParser.cs +++ b/test/xUnit/csharp/test_CommandLineParser.cs @@ -65,6 +65,15 @@ public static void Test_Throws_On_Reuse() Assert.Throws(() => 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(() => cpp.Parse(commandLine)); + } + [Theory] [InlineData("noexistfilename")] public static void TestDefaultParameterIsFileName_Not_Exist(params string[] commandLine) From 3dcb60832942515e11e2df0d505404d15f106980 Mon Sep 17 00:00:00 2001 From: Ilya Date: Thu, 20 Aug 2020 16:33:18 +0500 Subject: [PATCH 2/4] Address feedback --- .../host/msh/CommandLineParameterParser.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Microsoft.PowerShell.ConsoleHost/host/msh/CommandLineParameterParser.cs b/src/Microsoft.PowerShell.ConsoleHost/host/msh/CommandLineParameterParser.cs index c71cf640623..ab59ae2e89c 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/host/msh/CommandLineParameterParser.cs +++ b/src/Microsoft.PowerShell.ConsoleHost/host/msh/CommandLineParameterParser.cs @@ -704,7 +704,7 @@ internal void Parse(string[] args) { if (args[i] is null) { - throw new ArgumentNullException("The 'args' array contains a null element."); + throw new ArgumentNullException(nameof(args), "The specified arguments should not contain null elements."); } } From f5ee4a10b3b1b7f464b873deb3ad50f8fc767ac4 Mon Sep 17 00:00:00 2001 From: Ilya Date: Fri, 28 Aug 2020 10:48:06 +0500 Subject: [PATCH 3/4] Add localized string --- .../host/msh/CommandLineParameterParser.cs | 2 +- .../resources/CommandLineParameterParserStrings.resx | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/src/Microsoft.PowerShell.ConsoleHost/host/msh/CommandLineParameterParser.cs b/src/Microsoft.PowerShell.ConsoleHost/host/msh/CommandLineParameterParser.cs index ab59ae2e89c..279d182292d 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/host/msh/CommandLineParameterParser.cs +++ b/src/Microsoft.PowerShell.ConsoleHost/host/msh/CommandLineParameterParser.cs @@ -704,7 +704,7 @@ internal void Parse(string[] args) { if (args[i] is null) { - throw new ArgumentNullException(nameof(args), "The specified arguments should not contain null elements."); + throw new ArgumentNullException(nameof(args), CommandLineParameterParserStrings.NullElementInArgs); } } diff --git a/src/Microsoft.PowerShell.ConsoleHost/resources/CommandLineParameterParserStrings.resx b/src/Microsoft.PowerShell.ConsoleHost/resources/CommandLineParameterParserStrings.resx index 9703420c254..48d07113a0e 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/resources/CommandLineParameterParserStrings.resx +++ b/src/Microsoft.PowerShell.ConsoleHost/resources/CommandLineParameterParserStrings.resx @@ -219,4 +219,7 @@ Valid formats are: Parameter -STA is not supported on this platform. + + The specified arguments should not contain null elements. + From 3208121c3fef32d0a0aa930da96aca4b7d7e923f Mon Sep 17 00:00:00 2001 From: Ilya Date: Fri, 28 Aug 2020 22:38:29 +0500 Subject: [PATCH 4/4] Update src/Microsoft.PowerShell.ConsoleHost/resources/CommandLineParameterParserStrings.resx Co-authored-by: Robert Holt --- .../resources/CommandLineParameterParserStrings.resx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Microsoft.PowerShell.ConsoleHost/resources/CommandLineParameterParserStrings.resx b/src/Microsoft.PowerShell.ConsoleHost/resources/CommandLineParameterParserStrings.resx index 48d07113a0e..ab893b91a0a 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/resources/CommandLineParameterParserStrings.resx +++ b/src/Microsoft.PowerShell.ConsoleHost/resources/CommandLineParameterParserStrings.resx @@ -220,6 +220,6 @@ Valid formats are: Parameter -STA is not supported on this platform. - The specified arguments should not contain null elements. + The specified arguments must not contain null elements.