From 2fc3c30064005095b93c9cdf646e9c05c43b8652 Mon Sep 17 00:00:00 2001 From: Krishna Yalavarthi Date: Fri, 20 Mar 2020 15:58:45 -0700 Subject: [PATCH 1/7] "Path is not parsed correctly if '[' is present in the path" --- .../engine/regex.cs | 35 +++++++++++++++++-- 1 file changed, 32 insertions(+), 3 deletions(-) diff --git a/src/System.Management.Automation/engine/regex.cs b/src/System.Management.Automation/engine/regex.cs index 2c4ade7cb5f..e005ef5c08f 100644 --- a/src/System.Management.Automation/engine/regex.cs +++ b/src/System.Management.Automation/engine/regex.cs @@ -282,7 +282,7 @@ public static string Escape(string pattern) /// /// True if the string has wild card chars, false otherwise.. /// - /// Currently { '*', '?', '[' } are considered wild card chars and + /// Currently { '*', '?', '[', ']' } are considered wild card chars and /// '`' is the escape character. /// public static bool ContainsWildcardCharacters(string pattern) @@ -293,24 +293,43 @@ public static bool ContainsWildcardCharacters(string pattern) } bool result = false; + bool leftBracket = false; for (int index = 0; index < pattern.Length; ++index) { if (IsWildcardChar(pattern[index])) { + // If '[' or ']' is present alone, then it should not be treated as wildcard chars + // + // Check if both the '[' and the ']' brackets are present. + // If present they should be in order, first '[' and then ']' + // Otherwise, just treat ('[' or ']') as normal literal + + // Check if we got '[' + if ( IsLeftBracket(pattern[index]) ) + { + // got '['. + leftBracket = true; + continue; + } + // got the ']' without the '[' + else if ( (leftBracket == false) && IsRightBracket(pattern[index]) ) + { + // Ignore the ']' and continue + continue; + } + result = true; break; } // If it is an escape character then advance past // the next character - if (pattern[index] == escapeChar) { ++index; } } - return result; } @@ -403,6 +422,16 @@ private static bool IsWildcardChar(char ch) return (ch == '*') || (ch == '?') || (ch == '[') || (ch == ']'); } + private static bool IsLeftBracket(char ch) + { + return (ch == '['); + } + + private static bool IsRightBracket(char ch) + { + return (ch == ']'); + } + /// /// Converts this wildcard to a string that can be used as a right-hand-side operand of the LIKE operator of WQL. /// For example: "a*" will be converted to "a%". From 248d271044341ca899a51522a7a47982e42db230 Mon Sep 17 00:00:00 2001 From: krishnayalavarthi Date: Fri, 20 Mar 2020 19:42:49 -0700 Subject: [PATCH 2/7] Update src/System.Management.Automation/engine/regex.cs Co-Authored-By: Robert Holt --- src/System.Management.Automation/engine/regex.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/System.Management.Automation/engine/regex.cs b/src/System.Management.Automation/engine/regex.cs index e005ef5c08f..9f3079eaa0b 100644 --- a/src/System.Management.Automation/engine/regex.cs +++ b/src/System.Management.Automation/engine/regex.cs @@ -306,7 +306,7 @@ public static bool ContainsWildcardCharacters(string pattern) // Otherwise, just treat ('[' or ']') as normal literal // Check if we got '[' - if ( IsLeftBracket(pattern[index]) ) + if (IsLeftBracket(pattern[index])) { // got '['. leftBracket = true; @@ -1376,4 +1376,3 @@ internal static string Parse(WildcardPattern wildcardPattern) } } } - From 79c21da801f46ab53dae49f619d458606ccd36b1 Mon Sep 17 00:00:00 2001 From: krishnayalavarthi Date: Fri, 20 Mar 2020 19:43:36 -0700 Subject: [PATCH 3/7] Update src/System.Management.Automation/engine/regex.cs Co-Authored-By: Robert Holt --- src/System.Management.Automation/engine/regex.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/System.Management.Automation/engine/regex.cs b/src/System.Management.Automation/engine/regex.cs index 9f3079eaa0b..540749efa21 100644 --- a/src/System.Management.Automation/engine/regex.cs +++ b/src/System.Management.Automation/engine/regex.cs @@ -313,7 +313,7 @@ public static bool ContainsWildcardCharacters(string pattern) continue; } // got the ']' without the '[' - else if ( (leftBracket == false) && IsRightBracket(pattern[index]) ) + else if (!leftBracket && IsRightBracket(pattern[index])) { // Ignore the ']' and continue continue; From 42b07737dbd4088dc44b64d612ba26207ca1b85d Mon Sep 17 00:00:00 2001 From: krishnayalavarthi Date: Fri, 20 Mar 2020 20:21:56 -0700 Subject: [PATCH 4/7] Removed the extra functions and some code hygiene --- .../engine/regex.cs | 22 +++++-------------- 1 file changed, 6 insertions(+), 16 deletions(-) diff --git a/src/System.Management.Automation/engine/regex.cs b/src/System.Management.Automation/engine/regex.cs index 540749efa21..c048006a486 100644 --- a/src/System.Management.Automation/engine/regex.cs +++ b/src/System.Management.Automation/engine/regex.cs @@ -305,17 +305,16 @@ public static bool ContainsWildcardCharacters(string pattern) // If present they should be in order, first '[' and then ']' // Otherwise, just treat ('[' or ']') as normal literal - // Check if we got '[' - if (IsLeftBracket(pattern[index])) + // Check if we got '[' + if (ch == '[') { - // got '['. + // got '['. leftBracket = true; continue; } - // got the ']' without the '[' - else if (!leftBracket && IsRightBracket(pattern[index])) + else if (!leftBracket && ch == ']') { - // Ignore the ']' and continue + // got the ']' without the '['. Ignore the ']' and continue continue; } @@ -330,6 +329,7 @@ public static bool ContainsWildcardCharacters(string pattern) ++index; } } + return result; } @@ -422,16 +422,6 @@ private static bool IsWildcardChar(char ch) return (ch == '*') || (ch == '?') || (ch == '[') || (ch == ']'); } - private static bool IsLeftBracket(char ch) - { - return (ch == '['); - } - - private static bool IsRightBracket(char ch) - { - return (ch == ']'); - } - /// /// Converts this wildcard to a string that can be used as a right-hand-side operand of the LIKE operator of WQL. /// For example: "a*" will be converted to "a%". From ec9fb64e31c3ef316fe8a9ff9fda191c3e393fe5 Mon Sep 17 00:00:00 2001 From: krishnayalavarthi Date: Sat, 21 Mar 2020 00:49:33 -0700 Subject: [PATCH 5/7] Testcases for issue number #12170 --- .../Test-Path.Tests.ps1 | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Management/Test-Path.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Management/Test-Path.Tests.ps1 index 48a80d8e0b8..b8dd92f0e77 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Management/Test-Path.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Management/Test-Path.Tests.ps1 @@ -2,6 +2,32 @@ # Licensed under the MIT License. Describe "Test-Path" -Tags "CI" { BeforeAll { + $testDirnames = @( + 'WildCardCommandA' + 'WildCardCommand[B]' + '[' + 'goose]' + 'du[[ck' + 'du][ck' + 'duck]]' + ) + + $testFailDirnames = @( + 'WildCardCommand' + ']' + 'goos]e' + 'du[ck' + 'du]ck' + 'du]]ck' + 'duc]k]' + ) + + foreach($currentDir in $testDirNames) + { + $expandedFile = Join-Path $TestDrive -ChildPath $currentDir + New-Item -Path $expandedFile -ItemType Directory + } + $testdirectory = $TestDrive $testfilename = New-Item -path $testdirectory -Name testfile.txt -ItemType file -Value 1 -force @@ -141,4 +167,17 @@ Describe "Test-Path" -Tags "CI" { Test-Path Env:\PATH | Should -BeTrue } + It "Validate Test-Path scenario where DestinationPath has incomplete brackets" { + foreach($currentDir in $testDirNames) + { + $expandedFile = Join-Path $TestDrive -ChildPath $currentDir + Test-Path -Path $expandedFile | Should Be $True + } + + foreach($currentDir in $testFailDirnames) + { + $expandedFile = Join-Path $TestDrive -ChildPath $currentDir + Test-Path -Path $expandedFile | Should Be $False + } + } } From d4495e45c4af1770dc6a4a273890c8e1f0b5c092 Mon Sep 17 00:00:00 2001 From: krishnayalavarthi Date: Sat, 21 Mar 2020 01:04:27 -0700 Subject: [PATCH 6/7] Compilation error corrected --- src/System.Management.Automation/engine/regex.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/System.Management.Automation/engine/regex.cs b/src/System.Management.Automation/engine/regex.cs index c048006a486..df418d2f22c 100644 --- a/src/System.Management.Automation/engine/regex.cs +++ b/src/System.Management.Automation/engine/regex.cs @@ -306,13 +306,13 @@ public static bool ContainsWildcardCharacters(string pattern) // Otherwise, just treat ('[' or ']') as normal literal // Check if we got '[' - if (ch == '[') + if (pattern[index] == '[') { // got '['. leftBracket = true; continue; } - else if (!leftBracket && ch == ']') + else if (!leftBracket && pattern[index] == ']') { // got the ']' without the '['. Ignore the ']' and continue continue; From 043006d5d13fcf245f8255396b0698e959a5faa9 Mon Sep 17 00:00:00 2001 From: krishnayalavarthi Date: Sat, 21 Mar 2020 01:39:14 -0700 Subject: [PATCH 7/7] Unsupported case removed --- .../Modules/Microsoft.PowerShell.Management/Test-Path.Tests.ps1 | 1 - 1 file changed, 1 deletion(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Management/Test-Path.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Management/Test-Path.Tests.ps1 index b8dd92f0e77..be051b221dc 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Management/Test-Path.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Management/Test-Path.Tests.ps1 @@ -4,7 +4,6 @@ Describe "Test-Path" -Tags "CI" { BeforeAll { $testDirnames = @( 'WildCardCommandA' - 'WildCardCommand[B]' '[' 'goose]' 'du[[ck'