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-22 08:26:09 -07:00
committed by Gopher Robot
parent 6a85559a3f
commit 7138967c19
3 changed files with 25 additions and 2 deletions
+20
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)
}
}
}