Skip to content

Commit

Permalink
[receiver/windowseventlog] Fix panic when rendering excessively long …
Browse files Browse the repository at this point in the history
…offset
  • Loading branch information
djaglowski committed Nov 4, 2024
1 parent ba20b05 commit a46e350
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
3 changes: 3 additions & 0 deletions pkg/stanza/operator/input/windows/buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
11 changes: 11 additions & 0 deletions pkg/stanza/operator/input/windows/buffer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit a46e350

Please sign in to comment.