Skip to content

Commit

Permalink
fix(decoder): prevent race
Browse files Browse the repository at this point in the history
  • Loading branch information
rgmz committed Sep 26, 2024
1 parent 1a0cf04 commit 6b7104f
Showing 1 changed file with 21 additions and 8 deletions.
29 changes: 21 additions & 8 deletions pkg/decoders/escaped_unicode.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package decoders

import (
"bytes"
"regexp"
"strconv"
"unicode/utf8"
Expand Down Expand Up @@ -33,22 +34,34 @@ func (d *EscapedUnicode) FromChunk(chunk *sources.Chunk) *DecodableChunk {
return nil
}

matched := false
if codePointPat.Match(chunk.Data) {
var (
// Necessary to avoid data races.
chunkData = bytes.Clone(chunk.Data)
matched = false
)
if codePointPat.Match(chunkData) {
matched = true
chunk.Data = decodeCodePoint(chunk.Data)
chunkData = decodeCodePoint(chunkData)
}
if escapePat.Match(chunk.Data) {
if escapePat.Match(chunkData) {
matched = true
chunk.Data = decodeEscaped(chunk.Data)
chunkData = decodeEscaped(chunkData)
}

if matched {
decodableChunk := &DecodableChunk{
return &DecodableChunk{
DecoderType: d.Type(),
Chunk: chunk,
Chunk: &sources.Chunk{
Data: chunkData,
SourceName: chunk.SourceName,
SourceID: chunk.SourceID,
JobID: chunk.JobID,
SecretID: chunk.SecretID,
SourceMetadata: chunk.SourceMetadata,
SourceType: chunk.SourceType,
Verify: chunk.Verify,
},
}
return decodableChunk
} else {
return nil
}
Expand Down

0 comments on commit 6b7104f

Please sign in to comment.