windows: fix slicing of NTUnicodeString values

We were slicing using a count of bytes, not a count of uint16s.

Fixes golang/go#73460

Change-Id: If0fd19e795078c01fda5b976e3c34af115b25dcc
Reviewed-on: https://go-review.googlesource.com/c/sys/+/667235
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Junyang Shao <shaojunyang@google.com>
Reviewed-by: Quim Muntal <quimmuntal@gmail.com>
Auto-Submit: Keith Randall <khr@google.com>
Reviewed-by: Keith Randall <khr@google.com>
This commit is contained in:
Keith Randall
2025-04-21 18:28:27 -07:00
committed by Gopher Robot
parent 6a85559a3f
commit 7138967c19
3 changed files with 25 additions and 2 deletions

View File

@@ -1698,8 +1698,9 @@ func NewNTUnicodeString(s string) (*NTUnicodeString, error) {
// Slice returns a uint16 slice that aliases the data in the NTUnicodeString.
func (s *NTUnicodeString) Slice() []uint16 {
slice := unsafe.Slice(s.Buffer, s.MaximumLength)
return slice[:s.Length]
// Note: this rounds the length down, if it happens
// to (incorrectly) be odd. Probably safer than rounding up.
return unsafe.Slice(s.Buffer, s.MaximumLength/2)[:s.Length/2]
}
func (s *NTUnicodeString) String() string {

View File

@@ -1473,3 +1473,23 @@ func TestToUnicodeEx(t *testing.T) {
t.Errorf("UnloadKeyboardLayout failed: %v", err)
}
}
func TestRoundtripNTUnicodeString(t *testing.T) {
for _, s := range []string{
"",
"hello",
"Ƀ",
strings.Repeat("*", 32000), // NTUnicodeString works up to 2^16 byte lengths == 32768 uint16s.
// TODO: various encoding errors?
} {
ntus, err := windows.NewNTUnicodeString(s)
if err != nil {
t.Errorf("encoding %q failed: %v", s, err)
continue
}
s2 := ntus.String()
if s != s2 {
t.Errorf("round trip of %q = %q, wanted original", s, s2)
}
}
}

View File

@@ -2700,6 +2700,8 @@ type CommTimeouts struct {
// NTUnicodeString is a UTF-16 string for NT native APIs, corresponding to UNICODE_STRING.
type NTUnicodeString struct {
// Note: Length and MaximumLength are in *bytes*, not uint16s.
// They should always be even.
Length uint16
MaximumLength uint16
Buffer *uint16