From 6ed619efe7afeda9642c00495a34acd5ae153cbb Mon Sep 17 00:00:00 2001 From: MartinGC94 Date: Mon, 24 Apr 2023 17:49:10 +0200 Subject: [PATCH 1/4] Fix incorrect string to type conversion --- src/System.Management.Automation/engine/parser/Parser.cs | 2 +- test/powershell/Language/Parser/Conversions.Tests.ps1 | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/src/System.Management.Automation/engine/parser/Parser.cs b/src/System.Management.Automation/engine/parser/Parser.cs index e1788d5996f..a059e1f1aa3 100644 --- a/src/System.Management.Automation/engine/parser/Parser.cs +++ b/src/System.Management.Automation/engine/parser/Parser.cs @@ -280,7 +280,7 @@ internal static ITypeName ScanType(string typename, bool ignoreErrors) var result = parser.TypeNameRule(allowAssemblyQualifiedNames: true, firstTypeNameToken: out _); SemanticChecks.CheckArrayTypeNameDepth(result, PositionUtilities.EmptyExtent, parser); - if (!ignoreErrors && parser.ErrorList.Count > 0) + if (!ignoreErrors && result is not null && (parser.ErrorList.Count > 0 || !result.FullName.Equals(typename, StringComparison.OrdinalIgnoreCase))) { result = null; } diff --git a/test/powershell/Language/Parser/Conversions.Tests.ps1 b/test/powershell/Language/Parser/Conversions.Tests.ps1 index 119f74d9a8a..5e55af68cae 100644 --- a/test/powershell/Language/Parser/Conversions.Tests.ps1 +++ b/test/powershell/Language/Parser/Conversions.Tests.ps1 @@ -78,6 +78,10 @@ Describe 'conversion syntax' -Tags "CI" { $result -join ";" | Should -Be ($Elements -join ";") } } + + It 'Should not convert invalid strings to type name' { + 'int]whatever' -as [type] | Should -Be $null + } } Describe "Type resolution should prefer assemblies in powershell assembly cache" -Tags "Feature" { From 1bca09ceac66bb567365a89e331c6c3f42e60d22 Mon Sep 17 00:00:00 2001 From: MartinGC94 Date: Mon, 24 Apr 2023 18:29:06 +0200 Subject: [PATCH 2/4] Use extent, rather than the resolved typename in check. --- src/System.Management.Automation/engine/parser/Parser.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/System.Management.Automation/engine/parser/Parser.cs b/src/System.Management.Automation/engine/parser/Parser.cs index a059e1f1aa3..72d6dbbd276 100644 --- a/src/System.Management.Automation/engine/parser/Parser.cs +++ b/src/System.Management.Automation/engine/parser/Parser.cs @@ -280,7 +280,7 @@ internal static ITypeName ScanType(string typename, bool ignoreErrors) var result = parser.TypeNameRule(allowAssemblyQualifiedNames: true, firstTypeNameToken: out _); SemanticChecks.CheckArrayTypeNameDepth(result, PositionUtilities.EmptyExtent, parser); - if (!ignoreErrors && result is not null && (parser.ErrorList.Count > 0 || !result.FullName.Equals(typename, StringComparison.OrdinalIgnoreCase))) + if (!ignoreErrors && result is not null && (parser.ErrorList.Count > 0 || !result.Extent.Text.Equals(typename, StringComparison.OrdinalIgnoreCase))) { result = null; } From ff978efec3b37a7c3ed93fa992b0a88f0ea29624 Mon Sep 17 00:00:00 2001 From: MartinGC94 <42123497+MartinGC94@users.noreply.github.com> Date: Thu, 25 May 2023 11:57:47 +0200 Subject: [PATCH 3/4] Apply suggestions from code review Co-authored-by: Andy Jordan <2226434+andschwa@users.noreply.github.com> --- test/powershell/Language/Parser/Conversions.Tests.ps1 | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/test/powershell/Language/Parser/Conversions.Tests.ps1 b/test/powershell/Language/Parser/Conversions.Tests.ps1 index 5e55af68cae..d216f9fd2aa 100644 --- a/test/powershell/Language/Parser/Conversions.Tests.ps1 +++ b/test/powershell/Language/Parser/Conversions.Tests.ps1 @@ -79,9 +79,13 @@ Describe 'conversion syntax' -Tags "CI" { } } - It 'Should not convert invalid strings to type name' { + It 'Should not convert invalid strings to type name using -as operator' { 'int]whatever' -as [type] | Should -Be $null } + + It 'Should not convert invalid strings to type name using left-hand side operator' { + [Type] 'int]whatever' | Should -Throw + } } Describe "Type resolution should prefer assemblies in powershell assembly cache" -Tags "Feature" { From 3edd6fcb685e0e4795d6661b1101c174ec67076e Mon Sep 17 00:00:00 2001 From: MartinGC94 Date: Mon, 29 May 2023 10:36:46 +0200 Subject: [PATCH 4/4] Fix test --- test/powershell/Language/Parser/Conversions.Tests.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/powershell/Language/Parser/Conversions.Tests.ps1 b/test/powershell/Language/Parser/Conversions.Tests.ps1 index d216f9fd2aa..f6874b1db93 100644 --- a/test/powershell/Language/Parser/Conversions.Tests.ps1 +++ b/test/powershell/Language/Parser/Conversions.Tests.ps1 @@ -84,7 +84,7 @@ Describe 'conversion syntax' -Tags "CI" { } It 'Should not convert invalid strings to type name using left-hand side operator' { - [Type] 'int]whatever' | Should -Throw + {[Type] 'int]whatever'} | Should -Throw } }