]> Cypherpunks repositories - gostls13.git/commitdiff
[release-branch.go1.19] syscall: fix invalid unsafe.Pointer conversion on Windows
authorCuong Manh Le <cuong.manhle.vn@gmail.com>
Sat, 25 Feb 2023 09:32:15 +0000 (16:32 +0700)
committerGopher Robot <gobot@golang.org>
Wed, 1 Mar 2023 18:06:22 +0000 (18:06 +0000)
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>

src/syscall/env_windows.go

index 74b154ec1519e3c10b5e0fed46f8ca5698ccd261..c311706316dd6bb67701aa626108b654652660a5 100644 (file)
@@ -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
 }