Bound the Windows clipboard read by the block size instead of scanning for a NUL

This commit is contained in:
Viktor Liu
2026-09-23 12:37:05 +02:00
parent 8f158a0827
commit e42fe6dfc0
2 changed files with 39 additions and 1 deletions
@@ -0,0 +1,19 @@
//go:build windows
package server
import (
"testing"
"github.com/stretchr/testify/assert"
)
// Clipboard text is set by whichever local process owns the clipboard, and
// nothing makes it end in a NUL. Decoding must stop at the block's end, not
// run on looking for a terminator.
func TestUTF16UpToNUL(t *testing.T) {
assert.Equal(t, "hi", utf16UpToNUL([]uint16{'h', 'i', 0, 'x'}), "stops at the first NUL")
assert.Equal(t, "hi", utf16UpToNUL([]uint16{'h', 'i'}), "an unterminated block decodes to its end and no further")
assert.Equal(t, "", utf16UpToNUL([]uint16{0, 'x'}), "a leading NUL is an empty string")
assert.Equal(t, "", utf16UpToNUL(nil))
}