Skip to content

Commit d154ef6

Browse files
committed
path/filepath: change IsAbs("NUL") to return true
This CL changes IsAbs to return true for "NUL" and other Windows reserved filenames (search https://docs.microsoft.com/en-us/windows/desktop/fileio/naming-a-file for NUL for details). os.Open("NUL") and os.Stat("NUL") work regardless of what current directory is, and it is mistake to join "NUL" with current directory when building full path. Changing IsAbs("NUL") to return true fixes that mistake. Fixes #28035 Change-Id: Ife8f8aee48400702613ede8fc6834fd43e6e0f03 Reviewed-on: https://go-review.googlesource.com/c/145220 Run-TryBot: Alex Brainman <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]>
1 parent a70a2a8 commit d154ef6

File tree

3 files changed

+44
-0
lines changed

3 files changed

+44
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# go test -c -o NUL
2+
# should work (see golang.org/issue/28035).
3+
cd x
4+
go test -o=$devnull -c
5+
! exists x.test$exe
6+
7+
-- x/x_test.go --
8+
package x_test
9+
import (
10+
"testing"
11+
)
12+
func TestNUL(t *testing.T) {
13+
}

src/path/filepath/path_test.go

+5
Original file line numberDiff line numberDiff line change
@@ -751,6 +751,11 @@ func TestIsAbs(t *testing.T) {
751751
for _, test := range isabstests {
752752
tests = append(tests, IsAbsTest{"c:" + test.path, test.isAbs})
753753
}
754+
// Test reserved names.
755+
tests = append(tests, IsAbsTest{os.DevNull, true})
756+
tests = append(tests, IsAbsTest{"NUL", true})
757+
tests = append(tests, IsAbsTest{"nul", true})
758+
tests = append(tests, IsAbsTest{"CON", true})
754759
} else {
755760
tests = isabstests
756761
}

src/path/filepath/path_windows.go

+26
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,34 @@ func isSlash(c uint8) bool {
1313
return c == '\\' || c == '/'
1414
}
1515

16+
// reservedNames lists reserved Windows names. Search for PRN in
17+
// https://docs.microsoft.com/en-us/windows/desktop/fileio/naming-a-file
18+
// for details.
19+
var reservedNames = []string{
20+
"CON", "PRN", "AUX", "NUL",
21+
"COM1", "COM2", "COM3", "COM4", "COM5", "COM6", "COM7", "COM8", "COM9",
22+
"LPT1", "LPT2", "LPT3", "LPT4", "LPT5", "LPT6", "LPT7", "LPT8", "LPT9",
23+
}
24+
25+
// isReservedName returns true, if path is Windows reserved name.
26+
// See reservedNames for the full list.
27+
func isReservedName(path string) bool {
28+
if len(path) == 0 {
29+
return false
30+
}
31+
for _, reserved := range reservedNames {
32+
if strings.EqualFold(path, reserved) {
33+
return true
34+
}
35+
}
36+
return false
37+
}
38+
1639
// IsAbs reports whether the path is absolute.
1740
func IsAbs(path string) (b bool) {
41+
if isReservedName(path) {
42+
return true
43+
}
1844
l := volumeNameLen(path)
1945
if l == 0 {
2046
return false

0 commit comments

Comments
 (0)