fix(decoder): prevent race (#3031)

This commit is contained in:
Richard Gomez
2024-09-26 09:44:59 -07:00
committed by GitHub
parent 0ba3fa11ba
commit 6d022e7d8e
+21 -8
View File
@@ -1,6 +1,7 @@
package decoders package decoders
import ( import (
"bytes"
"regexp" "regexp"
"strconv" "strconv"
"unicode/utf8" "unicode/utf8"
@@ -33,22 +34,34 @@ func (d *EscapedUnicode) FromChunk(chunk *sources.Chunk) *DecodableChunk {
return nil return nil
} }
matched := false var (
if codePointPat.Match(chunk.Data) { // Necessary to avoid data races.
chunkData = bytes.Clone(chunk.Data)
matched = false
)
if codePointPat.Match(chunkData) {
matched = true matched = true
chunk.Data = decodeCodePoint(chunk.Data) chunkData = decodeCodePoint(chunkData)
} }
if escapePat.Match(chunk.Data) { if escapePat.Match(chunkData) {
matched = true matched = true
chunk.Data = decodeEscaped(chunk.Data) chunkData = decodeEscaped(chunkData)
} }
if matched { if matched {
decodableChunk := &DecodableChunk{ return &DecodableChunk{
DecoderType: d.Type(), 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 { } else {
return nil return nil
} }