mirror of https://github.com/golang/go.git
[release-branch.go1.19] syscall: fix invalid unsafe.Pointer conversion on Windows
This cherry-pick CL 471335 without using unsafe.{Add,Slice}.
Fixes #58773
Change-Id: Ifa5c059ed5e358ed98aee7e83b95dd1806b535f7
Reviewed-on: https://go-review.googlesource.com/c/go/+/471335
Reviewed-by: Than McIntosh <thanm@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Run-TryBot: Cuong Manh Le <cuong.manhle.vn@gmail.com>
Reviewed-by: Bryan Mills <bcmills@google.com>
Reviewed-on: https://go-review.googlesource.com/c/go/+/471935
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
Auto-Submit: Dmitri Shuralyov <dmitshur@google.com>
This commit is contained in:
parent
8417168d6f
commit
600d372f58
|
|
@ -68,21 +68,25 @@ func Clearenv() {
|
|||
}
|
||||
|
||||
func Environ() []string {
|
||||
s, e := GetEnvironmentStrings()
|
||||
envp, e := GetEnvironmentStrings()
|
||||
if e != nil {
|
||||
return nil
|
||||
}
|
||||
defer FreeEnvironmentStrings(s)
|
||||
defer FreeEnvironmentStrings(envp)
|
||||
|
||||
r := make([]string, 0, 50) // Empty with room to grow.
|
||||
for from, i, p := 0, 0, (*[1 << 24]uint16)(unsafe.Pointer(s)); true; i++ {
|
||||
if p[i] == 0 {
|
||||
// empty string marks the end
|
||||
if i <= from {
|
||||
break
|
||||
}
|
||||
r = append(r, string(utf16.Decode(p[from:i])))
|
||||
from = i + 1
|
||||
const size = unsafe.Sizeof(*envp)
|
||||
for *envp != 0 { // environment block ends with empty string
|
||||
// find NUL terminator
|
||||
end := unsafe.Pointer(envp)
|
||||
for *(*uint16)(end) != 0 {
|
||||
end = unsafe.Pointer(uintptr(end) + size)
|
||||
}
|
||||
|
||||
n := (uintptr(end) - uintptr(unsafe.Pointer(envp))) / size
|
||||
entry := (*[(1 << 30) - 1]uint16)(unsafe.Pointer(envp))[:n:n]
|
||||
r = append(r, string(utf16.Decode(entry)))
|
||||
envp = (*uint16)(unsafe.Pointer(uintptr(end) + size))
|
||||
}
|
||||
return r
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue