Skip to content

Commit de8c999

Browse files
committed
syscall: fix invalid unsafe.Pointer conversion on Windows
Fixes #58714 Change-Id: Ifa5c059ed5e358ed98aee7e83b95dd1806b535f7 Reviewed-on: https://go-review.googlesource.com/c/go/+/471335 Reviewed-by: Than McIntosh <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]> Run-TryBot: Cuong Manh Le <[email protected]> Reviewed-by: Bryan Mills <[email protected]>
1 parent 0b41b67 commit de8c999

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

src/syscall/env_windows.go

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -74,21 +74,24 @@ func Clearenv() {
7474
}
7575

7676
func Environ() []string {
77-
s, e := GetEnvironmentStrings()
77+
envp, e := GetEnvironmentStrings()
7878
if e != nil {
7979
return nil
8080
}
81-
defer FreeEnvironmentStrings(s)
81+
defer FreeEnvironmentStrings(envp)
82+
8283
r := make([]string, 0, 50) // Empty with room to grow.
83-
for from, i, p := 0, 0, (*[1 << 24]uint16)(unsafe.Pointer(s)); true; i++ {
84-
if p[i] == 0 {
85-
// empty string marks the end
86-
if i <= from {
87-
break
88-
}
89-
r = append(r, string(utf16.Decode(p[from:i])))
90-
from = i + 1
84+
const size = unsafe.Sizeof(*envp)
85+
for *envp != 0 { // environment block ends with empty string
86+
// find NUL terminator
87+
end := unsafe.Pointer(envp)
88+
for *(*uint16)(end) != 0 {
89+
end = unsafe.Add(end, size)
9190
}
91+
92+
entry := unsafe.Slice(envp, (uintptr(end)-uintptr(unsafe.Pointer(envp)))/size)
93+
r = append(r, string(utf16.Decode(entry)))
94+
envp = (*uint16)(unsafe.Add(end, size))
9295
}
9396
return r
9497
}

0 commit comments

Comments
 (0)