Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix duplicated path.Contains check and other checks in Path.GetFullPath #55373

Merged
merged 9 commits into from
Jul 24, 2021
31 changes: 18 additions & 13 deletions src/libraries/System.Private.CoreLib/src/System/IO/Path.Windows.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,8 @@ public static string GetFullPath(string path)
// unpredictable results.
if (path.Contains('\0'))
steveberdy marked this conversation as resolved.
Show resolved Hide resolved
throw new ArgumentException(SR.Argument_InvalidPathChars, nameof(path));

if (PathInternal.IsExtended(path.AsSpan()))
{
// \\?\ paths are considered normalized by definition. Windows doesn't normalize \\?\
// paths and neither should we. Even if we wanted to GetFullPathName does not work
// properly with device paths. If one wants to pass a \\?\ path through normalization
// one can chop off the prefix, pass it to GetFullPath and add it again.
return path;
}

return PathHelper.Normalize(path);

return GetFullQualifiedPath(path);
jeffhandley marked this conversation as resolved.
Show resolved Hide resolved
}

public static string GetFullPath(string path, string basePath)
Expand All @@ -77,7 +68,7 @@ public static string GetFullPath(string path, string basePath)
throw new ArgumentException(SR.Argument_InvalidPathChars);

if (IsPathFullyQualified(path))
return GetFullPath(path);
return GetFullQualifiedPath(path);
jeffhandley marked this conversation as resolved.
Show resolved Hide resolved

if (PathInternal.IsEffectivelyEmpty(path.AsSpan()))
return basePath;
Expand Down Expand Up @@ -129,7 +120,21 @@ public static string GetFullPath(string path, string basePath)

return PathInternal.IsDevice(combinedPath.AsSpan())
? PathInternal.RemoveRelativeSegments(combinedPath, PathInternal.GetRootLength(combinedPath.AsSpan()))
: GetFullPath(combinedPath);
: GetFullQualifiedPath(combinedPath);
jeffhandley marked this conversation as resolved.
Show resolved Hide resolved
}

private static string GetFullQualifiedPath(string path)
{
if (PathInternal.IsExtended(path.AsSpan()))
{
// \\?\ paths are considered normalized by definition. Windows doesn't normalize \\?\
// paths and neither should we. Even if we wanted to GetFullPathName does not work
// properly with device paths. If one wants to pass a \\?\ path through normalization
// one can chop off the prefix, pass it to GetFullPath and add it again.
return path;
}

return PathHelper.Normalize(path);
}

public static string GetTempPath()
Expand Down