Skip to content

Commit 5a62062

Browse files
committed
[feature]
only check separator in relation to colon if a colon is found
1 parent 8fa7127 commit 5a62062

2 files changed

Lines changed: 28 additions & 17 deletions

File tree

src/System.Management.Automation/namespaces/LocationGlobber.cs

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1649,7 +1649,6 @@ internal static bool IsAbsolutePath(string path)
16491649
}
16501650

16511651
int index = path.IndexOf(":", StringComparison.Ordinal);
1652-
int separator = path.IndexOf(StringLiterals.DefaultPathSeparatorString, StringComparison.Ordinal);
16531652

16541653
if (index == -1)
16551654
{
@@ -1664,11 +1663,18 @@ internal static bool IsAbsolutePath(string path)
16641663
// must assume that it is part of the path, and not
16651664
// delimiting the drive name.
16661665

1667-
if (index > 0 && index < separator)
1666+
if (index > 0)
16681667
{
1669-
// We must have a drive specified
1670-
1671-
result = true;
1668+
int separator = path.IndexOf(StringLiterals.DefaultPathSeparator, 0, index);
1669+
if (separator == -1)
1670+
{
1671+
separator = path.IndexOf(StringLiterals.AlternatePathSeparator, 0, index);
1672+
}
1673+
if (separator == -1 || index < separator)
1674+
{
1675+
// We must have a drive specified
1676+
result = true;
1677+
}
16721678
}
16731679
} while (false);
16741680

@@ -3408,19 +3414,24 @@ private static string RemoveDriveQualifier(string path)
34083414
// Find the drive separator only if it's before a path separator
34093415

34103416
int index = path.IndexOf(":", StringComparison.Ordinal);
3411-
int separator = path.IndexOf(StringLiterals.DefaultPathSeparatorString, StringComparison.Ordinal);
3412-
3413-
if (index != -1 && index < separator)
3417+
if (index != -1)
34143418
{
3415-
// Remove the \ or / if it follows the drive indicator
3416-
3417-
if (path[index + 1] == '\\' ||
3418-
path[index + 1] == '/')
3419+
int separator = path.IndexOf(StringLiterals.DefaultPathSeparator, 0, index);
3420+
if (separator == -1)
34193421
{
3420-
++index;
3422+
separator = path.IndexOf(StringLiterals.AlternatePathSeparator, 0, index);
34213423
}
3424+
if (separator == -1 || index < separator)
3425+
{
3426+
// Remove the \ or / if it follows the drive indicator
3427+
if (path[index + 1] == '\\' ||
3428+
path[index + 1] == '/')
3429+
{
3430+
++index;
3431+
}
34223432

3423-
result = path.Substring(index + 1);
3433+
result = path.Substring(index + 1);
3434+
}
34243435
}
34253436

34263437
return result;

test/powershell/Modules/Microsoft.PowerShell.Management/FileSystem.Tests.ps1

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -201,16 +201,16 @@ Describe "Basic FileSystem Provider Tests" -Tags "CI" {
201201
}
202202

203203
It "Set-Location on Unix succeeds with folder with colon" -Skip:($IsWindows) {
204-
New-Item -Path "$testdrive/hello:world" -ItemType Directory > $null
204+
New-Item -Path "$testdrive\hello:world" -ItemType Directory > $null
205205
Set-Location "$testdrive"
206-
Set-Location "./hello:world"
206+
Set-Location ".\hello:world"
207207
(Get-Location).Path | Should Be "$testdrive/hello:world"
208208
}
209209

210210
It "Get-Content on Unix succeeds with folder and file with colon" -Skip:($IsWindows) {
211211
$testPath = "$testdrive/hello:world"
212212
New-Item -Path $testPath -ItemType Directory > $null
213-
Set-Content -Path "$testPath/foo:bar.txt" -Value "Hello"
213+
Set-Content -Path "$testPath\foo:bar.txt" -Value "Hello"
214214
$files = Get-ChildItem "$testPath"
215215
$files.Count | Should Be 1
216216
$files[0].Name | Should BeExactly "foo:bar.txt"

0 commit comments

Comments
 (0)