Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -877,8 +877,6 @@ private bool ValidateOrRemoveAutoMountedDrive(PSDriveInfo drive, SessionStateSco
/// <returns>Absence of mounted drive for FileSystem provider or False for other provider types.</returns>
private bool IsAStaleVhdMountedDrive(PSDriveInfo drive)
{
bool result = false;

// check that drive's provider type is FileSystem
if ((drive.Provider != null) && (!drive.Provider.NameEquals(this.ExecutionContext.ProviderNames.FileSystem)))
{
Expand All @@ -894,21 +892,19 @@ private bool IsAStaleVhdMountedDrive(PSDriveInfo drive)
// 3. Unmount the VHD in session 'A'.
// The drive pointing to VHD in session 'B' gets detected as DriveType.NoRootDirectory
// after the VHD is removed in session 'A'.
if (drive != null && !string.IsNullOrEmpty(drive.Name) && drive.Name.Length == 1)
if (drive is { Name.Length: 1 })
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer to avoid using patterns since we haven't nullability check enabled.
I guess something classic like drive?.Name?.Length == 1 would work here.

{
try
{
char driveChar = Convert.ToChar(drive.Name, CultureInfo.InvariantCulture);

if (char.ToUpperInvariant(driveChar) >= 'A' && char.ToUpperInvariant(driveChar) <= 'Z')
if (char.IsAsciiLetter(drive.Name[0]))
{
DriveInfo systemDriveInfo = new DriveInfo(drive.Name);

if (systemDriveInfo.DriveType == DriveType.NoRootDirectory)
{
if (!Directory.Exists(drive.Root))
{
result = true;
return true;
}
}
}
Expand All @@ -919,7 +915,7 @@ private bool IsAStaleVhdMountedDrive(PSDriveInfo drive)
}
}

return result;
return false;
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2023,11 +2023,14 @@ public static string Match(string text)
for (int i = 0; i < text.Length; i++)
{
uint h = text[i];
if (h >= 'A' && h <= 'Z')
if (h == '-')
{
}
else if (char.IsAsciiLetter((char)h))
{
h |= 0x20; // ToLower
}
else if (!((h >= 'a' && h <= 'z') || h == '-'))
else
{
// If the character isn't in any of our patterns,
// don't bother hashing and reset the running length.
Expand Down Expand Up @@ -2070,11 +2073,11 @@ public static string Match(string text)
// This code can be used when adding a new pattern.
internal static uint HashNewPattern(string pattern)
{
char ToLower(char c)
uint ToLower(uint c)
{
if (c >= 'A' && c <= 'Z')
if (char.IsAsciiLetterUpper((char)c))
{
c = (char) (c | 0x20);
c |= 0x20;
Copy link
Copy Markdown
Collaborator

@iSazonov iSazonov Oct 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't change non-compiled code.

}

return c;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -732,18 +732,7 @@ protected override PSDriveInfo RemoveDrive(PSDriveInfo drive)
/// <returns>True if the drive can be persisted or else false.</returns>
private static bool IsSupportedDriveForPersistence(PSDriveInfo drive)
{
bool isSupportedDriveForPersistence = false;
if (drive != null && !string.IsNullOrEmpty(drive.Name) && drive.Name.Length == 1)
{
char driveChar = Convert.ToChar(drive.Name, CultureInfo.InvariantCulture);

if (char.ToUpperInvariant(driveChar) >= 'A' && char.ToUpperInvariant(driveChar) <= 'Z')
{
isSupportedDriveForPersistence = true;
}
}

return isSupportedDriveForPersistence;
return drive is { Name.Length: 1 } && char.IsAsciiLetter(drive.Name[0]);
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The same about pattern.

}

/// <summary>
Expand Down
10 changes: 1 addition & 9 deletions src/System.Management.Automation/utils/PathUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -794,14 +794,6 @@ private static bool EndsWithPeriodOrSpace(string? path)
return c == ' ' || c == '.';
}

/// <summary>
/// Returns true if the given character is a valid drive letter
/// </summary>
private static bool IsValidDriveChar(char value)
{
return ((value >= 'A' && value <= 'Z') || (value >= 'a' && value <= 'z'));
}

private static bool IsDevice(string path)
{
return IsExtended(path)
Expand Down Expand Up @@ -859,7 +851,7 @@ private static bool IsPartiallyQualified(string path)
&& IsDirectorySeparator(path[2])
// To match old behavior we'll check the drive character for validity as the path is technically
// not qualified if you don't have a valid drive. "=:\" is the "=" file's default data stream.
&& IsValidDriveChar(path[0]));
&& char.IsAsciiLetter(path[0]));
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please keep old method name for readability but update implementation.

}
/// <summary>
/// True if the given character is a directory separator.
Expand Down