From ca59edaa5a761e1d0ea91d6c07b063f85ef24f78 Mon Sep 17 00:00:00 2001 From: "Bryan C. Mills" Date: Wed, 3 May 2023 16:57:05 -0400 Subject: [PATCH] windows: use unsafe.Add instead of pointer arithmetic on a uintptr 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 Run-TryBot: Bryan Mills TryBot-Result: Gopher Robot Auto-Submit: Bryan Mills --- windows/env_windows.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/windows/env_windows.go b/windows/env_windows.go index 92ac05ff..b8ad1925 100644 --- a/windows/env_windows.go +++ b/windows/env_windows.go @@ -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 }