diff --git a/pkg/stanza/operator/input/windows/buffer.go b/pkg/stanza/operator/input/windows/buffer.go index 149eade2b4dd..3d083bb76693 100644 --- a/pkg/stanza/operator/input/windows/buffer.go +++ b/pkg/stanza/operator/input/windows/buffer.go @@ -23,6 +23,9 @@ type Buffer struct { // ReadBytes will read UTF-8 bytes from the buffer, where offset is the number of bytes to be read func (b *Buffer) ReadBytes(offset uint32) ([]byte, error) { + if offset > uint32(len(b.buffer)) { + offset = uint32(len(b.buffer)) + } utf16 := b.buffer[:offset] utf8, err := unicode.UTF16(unicode.LittleEndian, unicode.UseBOM).NewDecoder().Bytes(utf16) if err != nil { diff --git a/pkg/stanza/operator/input/windows/buffer_test.go b/pkg/stanza/operator/input/windows/buffer_test.go index f8376706ec78..78f654742495 100644 --- a/pkg/stanza/operator/input/windows/buffer_test.go +++ b/pkg/stanza/operator/input/windows/buffer_test.go @@ -21,6 +21,17 @@ func TestBufferReadBytes(t *testing.T) { require.Equal(t, utf8, bytes) } +func TestBufferReadBytesOverflow(t *testing.T) { + buffer := NewBuffer() + utf8 := []byte("test") + utf16, _ := unicode.UTF16(unicode.LittleEndian, unicode.UseBOM).NewEncoder().Bytes(utf8) + copy(buffer.buffer, utf16) + offset := uint32(len(utf16)) + bytes, err := buffer.ReadBytes(offset * 2) + require.NoError(t, err) + require.Equal(t, utf8, bytes) +} + func TestBufferReadWideBytes(t *testing.T) { buffer := NewBuffer() utf8 := []byte("test")