]> Cypherpunks repositories - gostls13.git/commitdiff
os: os: make Stat("*.txt") fail on windows
authorYasuhiro Matsumoto <mattn.jp@gmail.com>
Mon, 23 Apr 2018 04:03:44 +0000 (13:03 +0900)
committerAlex Brainman <alex.brainman@gmail.com>
Fri, 27 Apr 2018 10:04:48 +0000 (10:04 +0000)
Fixes #24999

Change-Id: Ie0bb6a6e0fa3992cdd272d42347af65ae7c95463
Reviewed-on: https://go-review.googlesource.com/108755
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Alex Brainman <alex.brainman@gmail.com>
src/internal/syscall/windows/syscall_windows.go
src/os/os_windows_test.go
src/os/types_windows.go

index 518af26d7244fb16d1bd7092eeb8179cef52f12c..66fe9324c0ae1d5e58f4d2d4a00d287a68f45b33 100644 (file)
@@ -12,6 +12,7 @@ import (
 
 const (
        ERROR_SHARING_VIOLATION      syscall.Errno = 32
+       ERROR_INVALID_NAME           syscall.Errno = 123
        ERROR_NO_UNICODE_TRANSLATION syscall.Errno = 1113
 )
 
index e28f0f4fa5891e1512c76ea27956d7235bb04011..8984dd2c6623dba179ec56c0975660f3d6cc0807 100644 (file)
@@ -1055,3 +1055,11 @@ func isWindowsDeveloperModeActive() bool {
 
        return val != 0
 }
+
+// TestStatOfInvalidName is regression test for issue #24999.
+func TestStatOfInvalidName(t *testing.T) {
+       _, err := os.Stat("*.go")
+       if err == nil {
+               t.Fatal(`os.Stat("*.go") unexpectedly succeeded`)
+       }
+}
index 235b9f1182b641c8c68df4fba95e2d622524fed4..f3297c033856a8bdc9e1fce24117936b74605d7f 100644 (file)
@@ -99,6 +99,14 @@ func newFileStatFromGetFileAttributesExOrFindFirstFile(path string, pathp *uint1
                        FileSizeLow:    fa.FileSizeLow,
                }, nil
        }
+       // GetFileAttributesEx returns ERROR_INVALID_NAME if called
+       // for invalid file name like "*.txt". Do not attempt to call
+       // FindFirstFile with "*.txt", because FindFirstFile will
+       // succeed. So just return ERROR_INVALID_NAME instead.
+       // see https://golang.org/issue/24999 for details.
+       if errno, _ := err.(syscall.Errno); errno == windows.ERROR_INVALID_NAME {
+               return nil, &PathError{"GetFileAttributesEx", path, err}
+       }
        // We might have symlink here. But some directories also have
        // FileAttributes FILE_ATTRIBUTE_REPARSE_POINT bit set.
        // For example, OneDrive directory is like that