Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 002bc94

Browse files
committed
internal/syscall/execenv: refactor handling env variables
Discover while working on CL 471335. Change-Id: I006077a5aa93cafb7be47813ab0c4714bb00d774 Reviewed-on: https://go-review.googlesource.com/c/go/+/471435 Reviewed-by: Than McIntosh <thanm@google.com> Reviewed-by: Ian Lance Taylor <iant@golang.org> Reviewed-by: Dmitri Shuralyov <dmitshur@google.com> Run-TryBot: Cuong Manh Le <cuong.manhle.vn@gmail.com> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Bryan Mills <bcmills@google.com>
1 parent de8c999 commit 002bc94

File tree

1 file changed

+9
-15
lines changed

1 file changed

+9
-15
lines changed

‎src/internal/syscall/execenv/execenv_windows.go‎

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,30 +25,24 @@ func Default(sys *syscall.SysProcAttr) (env []string, err error) {
2525
if sys == nil || sys.Token == 0 {
2626
return syscall.Environ(), nil
2727
}
28-
var block *uint16
29-
err = windows.CreateEnvironmentBlock(&block, sys.Token, false)
28+
var blockp *uint16
29+
err = windows.CreateEnvironmentBlock(&blockp, sys.Token, false)
3030
if err != nil {
3131
return nil, err
3232
}
33-
defer windows.DestroyEnvironmentBlock(block)
34-
blockp := uintptr(unsafe.Pointer(block))
35-
for {
33+
defer windows.DestroyEnvironmentBlock(blockp)
3634

35+
const size = unsafe.Sizeof(*blockp)
36+
for *blockp != 0 { // environment block ends with empty string
3737
// find NUL terminator
38-
end := unsafe.Pointer(blockp)
38+
end := unsafe.Add(unsafe.Pointer(blockp), size)
3939
for *(*uint16)(end) != 0 {
40-
end = unsafe.Pointer(uintptr(end) +2)
40+
end = unsafe.Add(end, size)
4141
}
4242

43-
n := (uintptr(end) - uintptr(unsafe.Pointer(blockp))) / 2
44-
if n == 0 {
45-
// environment block ends with empty string
46-
break
47-
}
48-
49-
entry := (*[(1 << 30) - 1]uint16)(unsafe.Pointer(blockp))[:n:n]
43+
entry := unsafe.Slice(blockp, (uintptr(end)-uintptr(unsafe.Pointer(blockp)))/2)
5044
env = append(env, string(utf16.Decode(entry)))
51-
blockp +=2* (uintptr(len(entry)) +1)
45+
blockp = (*uint16)(unsafe.Add(end, size))
5246
}
5347
return
5448
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /