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
+9 -11
View File
@@ -16,19 +16,17 @@ func Getenv(key string) (value string, found bool) {
if err != nil {
return "", false
}
b := make([]uint16, 100)
n, e := GetEnvironmentVariable(keyp, &b[0], uint32(len(b)))
if n == 0 && e == ERROR_ENVVAR_NOT_FOUND {
return "", false
}
if n > uint32(len(b)) {
b = make([]uint16, n)
n, e = GetEnvironmentVariable(keyp, &b[0], uint32(len(b)))
if n > uint32(len(b)) {
n = 0
n := uint32(100)
for {
b := make([]uint16, n)
n, err = GetEnvironmentVariable(keyp, &b[0], uint32(len(b)))
if n == 0 && err == ERROR_ENVVAR_NOT_FOUND {
return "", false
}
if n <= uint32(len(b)) {
return string(utf16.Decode(b[:n])), true
}
}
return string(utf16.Decode(b[0:n])), true
}
func Setenv(key, value string) error {