mirror of
https://github.com/rwinkhart/sys.git
synced 2026-09-03 15:47:35 -04:00
windows: fix parsing of non-ASCII entries in token.Environ
Fixes golang/go#65055, the unexpected behavior of token.Environ in parsing entries containing runes larger than 2 bytes in size Change-Id: I753d2c605e3a2d7a1d90cd18601d6b918f0d3f7a Reviewed-on: https://go-review.googlesource.com/c/sys/+/556895 Auto-Submit: Bryan Mills <bcmills@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Cherry Mui <cherryyz@google.com> Reviewed-by: Bryan Mills <bcmills@google.com> Reviewed-by: Quim Muntal <quimmuntal@gmail.com>
This commit is contained in:
committed by
Gopher Robot
parent
f69d32aa92
commit
c3fa2b8b41
+10
-7
@@ -37,14 +37,17 @@ func (token Token) Environ(inheritExisting bool) (env []string, err error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
defer DestroyEnvironmentBlock(block)
|
defer DestroyEnvironmentBlock(block)
|
||||||
blockp := unsafe.Pointer(block)
|
size := unsafe.Sizeof(*block)
|
||||||
for {
|
for *block != 0 {
|
||||||
entry := UTF16PtrToString((*uint16)(blockp))
|
// find NUL terminator
|
||||||
if len(entry) == 0 {
|
end := unsafe.Pointer(block)
|
||||||
break
|
for *(*uint16)(end) != 0 {
|
||||||
|
end = unsafe.Add(end, size)
|
||||||
}
|
}
|
||||||
env = append(env, entry)
|
|
||||||
blockp = unsafe.Add(blockp, 2*(len(entry)+1))
|
entry := unsafe.Slice(block, (uintptr(end)-uintptr(unsafe.Pointer(block)))/size)
|
||||||
|
env = append(env, UTF16ToString(entry))
|
||||||
|
block = (*uint16)(unsafe.Add(end, size))
|
||||||
}
|
}
|
||||||
return env, nil
|
return env, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,40 @@
|
|||||||
|
// Copyright 2024 The Go Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package windows_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"slices"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"golang.org/x/sys/windows"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestEnvironUTF8(t *testing.T) {
|
||||||
|
testEnvVariable1Key := "__GO_X_SYS_WINDOWS_ENV_WINDOWS_TEST_VAR_BEAVER"
|
||||||
|
testEnvVariable1Val := "🦫"
|
||||||
|
t.Setenv(testEnvVariable1Key, testEnvVariable1Val)
|
||||||
|
|
||||||
|
testEnvVariable2Key := "__GO_X_SYS_WINDOWS_ENV_WINDOWS_TEST_VAR_WHALE"
|
||||||
|
testEnvVariable2Val := "🐳"
|
||||||
|
t.Setenv(testEnvVariable2Key, testEnvVariable2Val)
|
||||||
|
|
||||||
|
var userToken windows.Token
|
||||||
|
|
||||||
|
env, err := userToken.Environ(true)
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
testEnvVariable1 := fmt.Sprintf("%s=%s", testEnvVariable1Key, testEnvVariable1Val)
|
||||||
|
if !slices.Contains(env, testEnvVariable1) {
|
||||||
|
t.Fatalf("expected to find %s in env", testEnvVariable1)
|
||||||
|
}
|
||||||
|
|
||||||
|
testEnvVariable2 := fmt.Sprintf("%s=%s", testEnvVariable2Key, testEnvVariable2Val)
|
||||||
|
if !slices.Contains(env, testEnvVariable2) {
|
||||||
|
t.Fatalf("expected to find %s in env", testEnvVariable2)
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user