diff --git a/windows/syscall_windows.go b/windows/syscall_windows.go index 4a325438..044fac4d 100644 --- a/windows/syscall_windows.go +++ b/windows/syscall_windows.go @@ -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 { diff --git a/windows/syscall_windows_test.go b/windows/syscall_windows_test.go index 1e686a4f..f81fea08 100644 --- a/windows/syscall_windows_test.go +++ b/windows/syscall_windows_test.go @@ -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) + } + } +} diff --git a/windows/types_windows.go b/windows/types_windows.go index ad67df2f..ce741c9c 100644 --- a/windows/types_windows.go +++ b/windows/types_windows.go @@ -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