Skip to content

[release-branch.go1.20] path/filepath: consider \\?\c: as a volume on Windows #64091

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

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
15 changes: 10 additions & 5 deletions src/path/filepath/path_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ var wincleantests = []PathTest{
{`//abc`, `\\abc`},
{`///abc`, `\\\abc`},
{`//abc//`, `\\abc\\`},
{`\\?\C:\`, `\\?\C:\`},
{`\\?\C:\a`, `\\?\C:\a`},

// Don't allow cleaning to move an element with a colon to the start of the path.
{`a/../c:`, `.\c:`},
Expand Down Expand Up @@ -1472,10 +1474,13 @@ var volumenametests = []VolumeNameTest{
{`//.`, `\\.`},
{`//./`, `\\.\`},
{`//./NUL`, `\\.\NUL`},
{`//?/`, `\\?`},
{`//?`, `\\?`},
{`//?/`, `\\?\`},
{`//?/NUL`, `\\?\NUL`},
{`/??`, `\??`},
{`/??/`, `\??\`},
{`/??/NUL`, `\??\NUL`},
{`//./a/b`, `\\.\a`},
{`//?/`, `\\?`},
{`//?/`, `\\?`},
{`//./C:`, `\\.\C:`},
{`//./C:/`, `\\.\C:`},
{`//./C:/a/b/c`, `\\.\C:`},
Expand All @@ -1484,8 +1489,8 @@ var volumenametests = []VolumeNameTest{
{`//./UNC/host\`, `\\.\UNC\host\`},
{`//./UNC`, `\\.\UNC`},
{`//./UNC/`, `\\.\UNC\`},
{`\\?\x`, `\\?`},
{`\??\x`, `\??`},
{`\\?\x`, `\\?\x`},
{`\??\x`, `\??\x`},
}

func TestVolumeName(t *testing.T) {
Expand Down
20 changes: 7 additions & 13 deletions src/path/filepath/path_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,14 @@ func volumeNameLen(path string) int {
// \\.\unc\a\b\..\c into \\.\unc\a\c.
return uncLen(path, len(`\\.\UNC\`))

case pathHasPrefixFold(path, `\\.`):
// Path starts with \\., and is a Local Device path.
case pathHasPrefixFold(path, `\\.`) ||
pathHasPrefixFold(path, `\\?`) || pathHasPrefixFold(path, `\??`):
// Path starts with \\.\, and is a Local Device path; or
// path starts with \\?\ or \??\ and is a Root Local Device path.
//
// We currently treat the next component after the \\.\ prefix
// as part of the volume name, although there doesn't seem to be
// a principled reason to do this.
// We treat the next component after the \\.\ prefix as
// part of the volume name, which means Clean(`\\?\c:\`)
// won't remove the trailing \. (See #64028.)
if len(path) == 3 {
return 3 // exactly \\.
}
Expand All @@ -117,14 +119,6 @@ func volumeNameLen(path string) int {
}
return len(path) - len(rest) - 1

case pathHasPrefixFold(path, `\\?`) || pathHasPrefixFold(path, `\??`):
// Path starts with \\?\ or \??\, and is a Root Local Device path.
//
// While Windows usually treats / and \ as equivalent,
// /??/ does not seem to be recognized as a Root Local Device path.
// We treat it as one anyway here to be safe.
return 3

case len(path) >= 2 && isSlash(path[1]):
// Path starts with \\, and is a UNC path.
return uncLen(path, 2)
Expand Down