Skip to content

Commit 40522e0

Browse files
committed
Fix memory corruption in io.Writer interface
When hooked up to logrus and running multiple log.Info() calls (for example), the bytearray being passed to Write() is reused in the successive calls. As this reference was being passed down through the channel, it resulted in corrupted data and JSON serialization errors. We fix that here by always making a copy before sending to the channel.
1 parent 49a10eb commit 40522e0

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

splunk/v2/writer.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,13 @@ func (w *Writer) Write(b []byte) (int, error) {
5050
w.errors = make(chan error, bufferSize)
5151
go w.listen()
5252
})
53+
// Make a local copy of the bytearray so it doesn't get overwritten by
54+
// the next call to Write()
55+
var b2 = make([]byte, len(b))
56+
copy(b2, b)
5357
// Send the data to the channel
5458
w.dataChan <- &message{
55-
data: b,
59+
data: b2,
5660
writtenAt: time.Now(),
5761
}
5862
// We don't know if we've hit any errors yet, so just say we're good

0 commit comments

Comments
 (0)