windows: fix race when allocating buffer for some windows syscalls

From main repo: https://go-review.googlesource.com/#/c/4940

Change-Id: I56fe7f6aedc0fd350abb94299ad500fcb80c049a
Reviewed-on: https://go-review.googlesource.com/8604
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
This commit is contained in:
Alex Brainman
2015-04-09 00:47:30 +00:00
parent ea75526483
commit ddd1cdae39
3 changed files with 59 additions and 77 deletions
+5 -13
View File
@@ -6,8 +6,6 @@
package windows
import "syscall"
// EscapeArg rewrites command line argument s as prescribed
// in http://msdn.microsoft.com/en-us/library/ms880421.
// This function returns "" (2 double quotes) if s is empty.
@@ -85,21 +83,15 @@ func FullPath(name string) (path string, err error) {
if err != nil {
return "", err
}
buf := make([]uint16, 100)
n, err := GetFullPathName(p, uint32(len(buf)), &buf[0], nil)
if err != nil {
return "", err
}
if n > uint32(len(buf)) {
// Windows is asking for bigger buffer.
buf = make([]uint16, n)
n := uint32(100)
for {
buf := make([]uint16, n)
n, err = GetFullPathName(p, uint32(len(buf)), &buf[0], nil)
if err != nil {
return "", err
}
if n > uint32(len(buf)) {
return "", syscall.EINVAL
if n <= uint32(len(buf)) {
return UTF16ToString(buf[:n]), nil
}
}
return UTF16ToString(buf[:n]), nil
}