From e656aebb5b9e0437e050c3050e50a658c0244777 Mon Sep 17 00:00:00 2001 From: Yasuhiro Matsumoto Date: Mon, 23 Apr 2018 13:03:44 +0900 Subject: [PATCH] os: os: make Stat("*.txt") fail on windows Fixes #24999 Change-Id: Ie0bb6a6e0fa3992cdd272d42347af65ae7c95463 Reviewed-on: https://go-review.googlesource.com/108755 TryBot-Result: Gobot Gobot Reviewed-by: Alex Brainman --- src/internal/syscall/windows/syscall_windows.go | 1 + src/os/os_windows_test.go | 8 ++++++++ src/os/types_windows.go | 8 ++++++++ 3 files changed, 17 insertions(+) diff --git a/src/internal/syscall/windows/syscall_windows.go b/src/internal/syscall/windows/syscall_windows.go index 518af26d72..66fe9324c0 100644 --- a/src/internal/syscall/windows/syscall_windows.go +++ b/src/internal/syscall/windows/syscall_windows.go @@ -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 ) diff --git a/src/os/os_windows_test.go b/src/os/os_windows_test.go index e28f0f4fa5..8984dd2c66 100644 --- a/src/os/os_windows_test.go +++ b/src/os/os_windows_test.go @@ -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`) + } +} diff --git a/src/os/types_windows.go b/src/os/types_windows.go index 235b9f1182..f3297c0338 100644 --- a/src/os/types_windows.go +++ b/src/os/types_windows.go @@ -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