Skip to content

Commit 9b429f8

Browse files
committed
Add a probe call to determine the needed buffer size on Windows
1 parent 567094a commit 9b429f8

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

internal/resolve_path_windows.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,23 @@ func ResolvePath(filename string) (string, error) {
1717
}
1818
defer f.Close()
1919

20+
// Get the Windows handle
2021
handle := windows.Handle(f.Fd())
21-
buf := make([]uint16, syscall.MAX_PATH)
22-
_, err = windows.GetFinalPathNameByHandle(handle, &buf[0], uint32(len(buf)), 0)
22+
23+
// Probe call to determine the needed buffer size
24+
bufSize, err := windows.GetFinalPathNameByHandle(handle, nil, 0, 0)
25+
if err != nil {
26+
return "", err
27+
}
28+
29+
buf := make([]uint16, bufSize)
30+
n, err := windows.GetFinalPathNameByHandle(handle, &buf[0], uint32(len(buf)), 0)
2331
if err != nil {
2432
return "", err
2533
}
26-
final := syscall.UTF16ToString(buf)
34+
35+
// Convert the buffer to a string
36+
final := syscall.UTF16ToString(buf[:n])
2737

2838
// Strip possible "\\?\" prefix
2939
final = strings.TrimPrefix(final, `\\?\`)

0 commit comments

Comments
 (0)