aboutsummaryrefslogtreecommitdiff
path: root/src/syscall/syscall_windows.go
diff options
context:
space:
mode:
authorQi Xiao <xiaq@google.com>2023-06-11 23:29:26 +0100
committerGopher Robot <gobot@golang.org>2023-06-14 13:30:22 +0000
commitba4c6d1d6e36b9a4beee3f501fa8ad2e044d0768 (patch)
tree15ece5d11a751f9bf2f0b4bf264336f48abcfaf4 /src/syscall/syscall_windows.go
parentc3db64c0f45e8f2d75c5b59401e0fc925701b6f4 (diff)
downloadgo-ba4c6d1d6e36b9a4beee3f501fa8ad2e044d0768.tar.xz
syscall: Fix Getwd on Windows to correctly handle long paths.
Fixes #60051. Change-Id: Ia68ca0493912cb09d8c1d36a144bf0725842af1d Reviewed-on: https://go-review.googlesource.com/c/go/+/502415 Auto-Submit: Bryan Mills <bcmills@google.com> Run-TryBot: Quim Muntal <quimmuntal@gmail.com> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Bryan Mills <bcmills@google.com>
Diffstat (limited to 'src/syscall/syscall_windows.go')
-rw-r--r--src/syscall/syscall_windows.go18
1 files changed, 14 insertions, 4 deletions
diff --git a/src/syscall/syscall_windows.go b/src/syscall/syscall_windows.go
index 9f1b384de4..e348905abf 100644
--- a/src/syscall/syscall_windows.go
+++ b/src/syscall/syscall_windows.go
@@ -533,11 +533,21 @@ const ImplementsGetwd = true
func Getwd() (wd string, err error) {
b := make([]uint16, 300)
- n, e := GetCurrentDirectory(uint32(len(b)), &b[0])
- if e != nil {
- return "", e
+ // The path of the current directory may not fit in the initial 300-word
+ // buffer when long path support is enabled. The current directory may also
+ // change between subsequent calls of GetCurrentDirectory. As a result, we
+ // need to retry the call in a loop until the current directory fits, each
+ // time with a bigger buffer.
+ for {
+ n, e := GetCurrentDirectory(uint32(len(b)), &b[0])
+ if e != nil {
+ return "", e
+ }
+ if int(n) <= len(b) {
+ return UTF16ToString(b[:n]), nil
+ }
+ b = make([]uint16, n)
}
- return UTF16ToString(b[0:n]), nil
}
func Chdir(path string) (err error) {