Skip to content

Commit

Permalink
[pkg/stanza] Pass buffer by reference so buffer changes are not lost (#…
Browse files Browse the repository at this point in the history
…36252)

The root of the crash reported in #36179 was the fact that the `Buffer`
struct being passed by value in recursive calls made it allocate the
needed amount, but, after the return of the recursive call it attempted
to read a buffer that was larger than the allocated buffer on the
recursive call.

The crash was going to be hit whenever an XML was larger than the
default size of the buffer (16KiB).

The code is a bit hard to test because its fully usage actually happens
on the receiver where the buffer and its components are not visible.
I'll look to add a test in a follow-up.

cc @djaglowski 

Skipping changelog since #36179 covers it.

---------

Co-authored-by: Daniel Jaglowski <[email protected]>
  • Loading branch information
pjanotti and djaglowski authored Nov 8, 2024
1 parent 9c382d9 commit 234b5a7
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 18 deletions.
2 changes: 1 addition & 1 deletion pkg/stanza/operator/input/windows/bookmark.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func (b *Bookmark) Update(event Event) error {
}

// Render will render the bookmark as xml.
func (b *Bookmark) Render(buffer Buffer) (string, error) {
func (b *Bookmark) Render(buffer *Buffer) (string, error) {
if b.handle == 0 {
return "", fmt.Errorf("bookmark handle is not open")
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/stanza/operator/input/windows/buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ func (b *Buffer) FirstByte() *byte {
}

// NewBuffer creates a new buffer with the default buffer size
func NewBuffer() Buffer {
return Buffer{
func NewBuffer() *Buffer {
return &Buffer{
buffer: make([]byte, defaultBufferSize),
}
}
11 changes: 0 additions & 11 deletions pkg/stanza/operator/input/windows/buffer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,6 @@ 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
6 changes: 3 additions & 3 deletions pkg/stanza/operator/input/windows/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type Event struct {
}

// GetPublisherName will get the publisher name of the event.
func (e *Event) GetPublisherName(buffer Buffer) (string, error) {
func (e *Event) GetPublisherName(buffer *Buffer) (string, error) {
if e.handle == 0 {
return "", fmt.Errorf("event handle does not exist")
}
Expand Down Expand Up @@ -77,7 +77,7 @@ func NewEvent(handle uintptr) Event {
}

// RenderSimple will render the event as EventXML without formatted info.
func (e *Event) RenderSimple(buffer Buffer) (*EventXML, error) {
func (e *Event) RenderSimple(buffer *Buffer) (*EventXML, error) {
if e.handle == 0 {
return nil, fmt.Errorf("event handle does not exist")
}
Expand All @@ -100,7 +100,7 @@ func (e *Event) RenderSimple(buffer Buffer) (*EventXML, error) {
}

// RenderDeep will render the event as EventXML with all available formatted info.
func (e *Event) RenderDeep(buffer Buffer, publisher Publisher) (*EventXML, error) {
func (e *Event) RenderDeep(buffer *Buffer, publisher Publisher) (*EventXML, error) {
if e.handle == 0 {
return nil, fmt.Errorf("event handle does not exist")
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/stanza/operator/input/windows/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
type Input struct {
helper.InputOperator
bookmark Bookmark
buffer Buffer
buffer *Buffer
channel string
maxReads int
startAt string
Expand Down

0 comments on commit 234b5a7

Please sign in to comment.