Skip to content

Commit eda42f7

Browse files
committed
path/filepath: consider \\?\c: as a volume on Windows
While fixing several bugs in path handling on Windows, beginning with \\?\. Prior to #540277, VolumeName considered the first path component after the \\?\ prefix to be part of the volume name. After, it considered only the \\? prefix to be the volume name. Restore the previous behavior. Fixes #64028 Change-Id: I6523789e61776342800bd607fb3f29d496257e68 Reviewed-on: https://go-review.googlesource.com/c/go/+/541175 LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Roland Shoemaker <[email protected]>
1 parent 130baf3 commit eda42f7

File tree

2 files changed

+17
-18
lines changed

2 files changed

+17
-18
lines changed

src/path/filepath/path_test.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@ var wincleantests = []PathTest{
109109
{`//abc`, `\\abc`},
110110
{`///abc`, `\\\abc`},
111111
{`//abc//`, `\\abc\\`},
112+
{`\\?\C:\`, `\\?\C:\`},
113+
{`\\?\C:\a`, `\\?\C:\a`},
112114

113115
// Don't allow cleaning to move an element with a colon to the start of the path.
114116
{`a/../c:`, `.\c:`},
@@ -1597,10 +1599,13 @@ var volumenametests = []VolumeNameTest{
15971599
{`//.`, `\\.`},
15981600
{`//./`, `\\.\`},
15991601
{`//./NUL`, `\\.\NUL`},
1600-
{`//?/`, `\\?`},
1602+
{`//?`, `\\?`},
1603+
{`//?/`, `\\?\`},
1604+
{`//?/NUL`, `\\?\NUL`},
1605+
{`/??`, `\??`},
1606+
{`/??/`, `\??\`},
1607+
{`/??/NUL`, `\??\NUL`},
16011608
{`//./a/b`, `\\.\a`},
1602-
{`//?/`, `\\?`},
1603-
{`//?/`, `\\?`},
16041609
{`//./C:`, `\\.\C:`},
16051610
{`//./C:/`, `\\.\C:`},
16061611
{`//./C:/a/b/c`, `\\.\C:`},
@@ -1609,8 +1614,8 @@ var volumenametests = []VolumeNameTest{
16091614
{`//./UNC/host\`, `\\.\UNC\host\`},
16101615
{`//./UNC`, `\\.\UNC`},
16111616
{`//./UNC/`, `\\.\UNC\`},
1612-
{`\\?\x`, `\\?`},
1613-
{`\??\x`, `\??`},
1617+
{`\\?\x`, `\\?\x`},
1618+
{`\??\x`, `\??\x`},
16141619
}
16151620

16161621
func TestVolumeName(t *testing.T) {

src/path/filepath/path_windows.go

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -102,12 +102,14 @@ func volumeNameLen(path string) int {
102102
// \\.\unc\a\b\..\c into \\.\unc\a\c.
103103
return uncLen(path, len(`\\.\UNC\`))
104104

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

120-
case pathHasPrefixFold(path, `\\?`) || pathHasPrefixFold(path, `\??`):
121-
// Path starts with \\?\ or \??\, and is a Root Local Device path.
122-
//
123-
// While Windows usually treats / and \ as equivalent,
124-
// /??/ does not seem to be recognized as a Root Local Device path.
125-
// We treat it as one anyway here to be safe.
126-
return 3
127-
128122
case len(path) >= 2 && isSlash(path[1]):
129123
// Path starts with \\, and is a UNC path.
130124
return uncLen(path, 2)

0 commit comments

Comments
 (0)