]> Cypherpunks repositories - gostls13.git/commitdiff
path/filepath: change IsAbs("NUL") to return true
authorAlex Brainman <alex.brainman@gmail.com>
Sun, 21 Oct 2018 03:57:58 +0000 (14:57 +1100)
committerAlex Brainman <alex.brainman@gmail.com>
Fri, 2 Nov 2018 07:24:50 +0000 (07:24 +0000)
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 <alex.brainman@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
src/cmd/go/testdata/script/test_devnull.txt [new file with mode: 0644]
src/path/filepath/path_test.go
src/path/filepath/path_windows.go

diff --git a/src/cmd/go/testdata/script/test_devnull.txt b/src/cmd/go/testdata/script/test_devnull.txt
new file mode 100644 (file)
index 0000000..c414e59
--- /dev/null
@@ -0,0 +1,13 @@
+# go test -c -o NUL
+# should work (see golang.org/issue/28035).
+cd x
+go test -o=$devnull -c
+! exists x.test$exe
+
+-- x/x_test.go --
+package x_test
+import (
+    "testing"
+)
+func TestNUL(t *testing.T) {
+}
index eddae4755be6d2066720122bc16845a2b1552ce7..3434ea2e6e77ca7909d848b94fbad68def18f2ff 100644 (file)
@@ -751,6 +751,11 @@ func TestIsAbs(t *testing.T) {
                for _, test := range isabstests {
                        tests = append(tests, IsAbsTest{"c:" + test.path, test.isAbs})
                }
+               // Test reserved names.
+               tests = append(tests, IsAbsTest{os.DevNull, true})
+               tests = append(tests, IsAbsTest{"NUL", true})
+               tests = append(tests, IsAbsTest{"nul", true})
+               tests = append(tests, IsAbsTest{"CON", true})
        } else {
                tests = isabstests
        }
index 6a144d9e0b5fae425e66cddc20f179b3c6413df9..445c868e41460456469d1459d868352fdaa3e5c2 100644 (file)
@@ -13,8 +13,34 @@ func isSlash(c uint8) bool {
        return c == '\\' || c == '/'
 }
 
+// reservedNames lists reserved Windows names. Search for PRN in
+// https://docs.microsoft.com/en-us/windows/desktop/fileio/naming-a-file
+// for details.
+var reservedNames = []string{
+       "CON", "PRN", "AUX", "NUL",
+       "COM1", "COM2", "COM3", "COM4", "COM5", "COM6", "COM7", "COM8", "COM9",
+       "LPT1", "LPT2", "LPT3", "LPT4", "LPT5", "LPT6", "LPT7", "LPT8", "LPT9",
+}
+
+// isReservedName returns true, if path is Windows reserved name.
+// See reservedNames for the full list.
+func isReservedName(path string) bool {
+       if len(path) == 0 {
+               return false
+       }
+       for _, reserved := range reservedNames {
+               if strings.EqualFold(path, reserved) {
+                       return true
+               }
+       }
+       return false
+}
+
 // IsAbs reports whether the path is absolute.
 func IsAbs(path string) (b bool) {
+       if isReservedName(path) {
+               return true
+       }
        l := volumeNameLen(path)
        if l == 0 {
                return false