From e77106cc54af58d3eedbb134310668c6993474c7 Mon Sep 17 00:00:00 2001 From: Yasuhiro Matsumoto Date: Wed, 13 Nov 2019 10:02:17 +0900 Subject: [PATCH] os: handle backslash and slash both in the path on Windows Fixes #35492 Change-Id: I00dce8fd1228f809e0c61013ac4de7a5953cbbf9 Reviewed-on: https://go-review.googlesource.com/c/go/+/206997 Run-TryBot: Ian Lance Taylor TryBot-Result: Gobot Gobot Reviewed-by: Ian Lance Taylor --- src/os/file_windows.go | 11 +++++++++-- src/os/path_windows_test.go | 15 +++++++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/src/os/file_windows.go b/src/os/file_windows.go index 1e78f4e867..96f934d039 100644 --- a/src/os/file_windows.go +++ b/src/os/file_windows.go @@ -111,10 +111,17 @@ func openDir(name string) (file *File, err error) { path := fixLongPath(name) - if len(path) == 2 && path[1] == ':' || (len(path) > 0 && path[len(path)-1] == '\\') { // it is a drive letter, like C: + if len(path) == 2 && path[1] == ':' { // it is a drive letter, like C: mask = path + `*` + } else if len(path) > 0 { + lc := path[len(path)-1] + if lc == '/' || lc == '\\' { + mask = path + `*` + } else { + mask = path + `\*` + } } else { - mask = path + `\*` + mask = `\*` } maskp, e := syscall.UTF16PtrFromString(mask) if e != nil { diff --git a/src/os/path_windows_test.go b/src/os/path_windows_test.go index f1745ad132..862b404362 100644 --- a/src/os/path_windows_test.go +++ b/src/os/path_windows_test.go @@ -74,3 +74,18 @@ func TestMkdirAllExtendedLength(t *testing.T) { t.Fatalf("MkdirAll(%q) should have failed, but did not", path) } } + +func TestOpenRootSlash(t *testing.T) { + tests := []string{ + `/`, + `\`, + } + + for _, test := range tests { + dir, err := os.Open(test) + if err != nil { + t.Fatalf("Open(%q) failed: %v", test, err) + } + dir.Close() + } +}