Skip to content

Commit 6a73f91

Browse files
pjanottidjaglowski
authored andcommitted
[pkg/stanza] Pass buffer by reference so buffer changes are not lost (open-telemetry#36252)
The root of the crash reported in open-telemetry#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 open-telemetry#36179 covers it. --------- Co-authored-by: Daniel Jaglowski <[email protected]>
1 parent 70bf092 commit 6a73f91

File tree

5 files changed

+7
-18
lines changed

5 files changed

+7
-18
lines changed

pkg/stanza/operator/input/windows/bookmark.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func (b *Bookmark) Update(event Event) error {
5454
}
5555

5656
// Render will render the bookmark as xml.
57-
func (b *Bookmark) Render(buffer Buffer) (string, error) {
57+
func (b *Bookmark) Render(buffer *Buffer) (string, error) {
5858
if b.handle == 0 {
5959
return "", fmt.Errorf("bookmark handle is not open")
6060
}

pkg/stanza/operator/input/windows/buffer.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ func (b *Buffer) FirstByte() *byte {
7575
}
7676

7777
// NewBuffer creates a new buffer with the default buffer size
78-
func NewBuffer() Buffer {
79-
return Buffer{
78+
func NewBuffer() *Buffer {
79+
return &Buffer{
8080
buffer: make([]byte, defaultBufferSize),
8181
}
8282
}

pkg/stanza/operator/input/windows/buffer_test.go

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,6 @@ func TestBufferReadBytes(t *testing.T) {
2121
require.Equal(t, utf8, bytes)
2222
}
2323

24-
func TestBufferReadBytesOverflow(t *testing.T) {
25-
buffer := NewBuffer()
26-
utf8 := []byte("test")
27-
utf16, _ := unicode.UTF16(unicode.LittleEndian, unicode.UseBOM).NewEncoder().Bytes(utf8)
28-
copy(buffer.buffer, utf16)
29-
offset := uint32(len(utf16))
30-
bytes, err := buffer.ReadBytes(offset * 2)
31-
require.NoError(t, err)
32-
require.Equal(t, utf8, bytes)
33-
}
34-
3524
func TestBufferReadWideBytes(t *testing.T) {
3625
buffer := NewBuffer()
3726
utf8 := []byte("test")

pkg/stanza/operator/input/windows/event.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ type Event struct {
2727
}
2828

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

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

102102
// RenderDeep will render the event as EventXML with all available formatted info.
103-
func (e *Event) RenderDeep(buffer Buffer, publisher Publisher) (*EventXML, error) {
103+
func (e *Event) RenderDeep(buffer *Buffer, publisher Publisher) (*EventXML, error) {
104104
if e.handle == 0 {
105105
return nil, fmt.Errorf("event handle does not exist")
106106
}

pkg/stanza/operator/input/windows/input.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import (
2424
type Input struct {
2525
helper.InputOperator
2626
bookmark Bookmark
27-
buffer Buffer
27+
buffer *Buffer
2828
channel string
2929
maxReads int
3030
startAt string

0 commit comments

Comments
 (0)