Skip to content

Commit

Permalink
windows: use unsafe.Add instead of pointer arithmetic on a uintptr
Browse files Browse the repository at this point in the history
The existing uintptr arithmetic is arguably valid because the
environment block is not located within the Go heap
(see golang/go#58625).

However, unsafe.Add (added in Go 1.17) expresses the same logic with
fewer conversions, and in addition avoids triggering the unsafeptr
vet check.

For golang/go#41205.

Change-Id: Ifc509279a13fd707be570908ec779d8518b4f75b
Reviewed-on: https://go-review.googlesource.com/c/sys/+/492415
Reviewed-by: Ian Lance Taylor <iant@google.com>
Run-TryBot: Bryan Mills <bcmills@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Auto-Submit: Bryan Mills <bcmills@google.com>
  • Loading branch information
Bryan C. Mills authored and gopherbot committed May 3, 2023
1 parent 6c52899 commit ca59eda
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions windows/env_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,14 @@ func (token Token) Environ(inheritExisting bool) (env []string, err error) {
return nil, err
}
defer DestroyEnvironmentBlock(block)
blockp := uintptr(unsafe.Pointer(block))
blockp := unsafe.Pointer(block)
for {
entry := UTF16PtrToString((*uint16)(unsafe.Pointer(blockp)))
entry := UTF16PtrToString((*uint16)(blockp))
if len(entry) == 0 {
break
}
env = append(env, entry)
blockp += 2 * (uintptr(len(entry)) + 1)
blockp = unsafe.Add(blockp, 2*(len(entry)+1))
}
return env, nil
}
Expand Down

0 comments on commit ca59eda

Please sign in to comment.