Skip to content

Commit

Permalink
Merge pull request cloudflare#68 from netsampler/bugfix/enricher-2
Browse files Browse the repository at this point in the history
Bugfix: issues when reading a partial chunk of protobuf from stdin
  • Loading branch information
lspgn authored Jan 25, 2022
2 parents 20e8e56 + 7acb848 commit 8d59905
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions cmd/enricher/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,26 +147,28 @@ func main() {
rdr := bufio.NewReader(os.Stdin)

msg := &flowmessage.FlowMessageExt{}
msgLen := make([]byte, binary.MaxVarintLen64)
lenBufSize := len(msgLen)
lenBufSize := binary.MaxVarintLen64
for {
n, err := rdr.Read(msgLen)
msgLen, err := rdr.Peek(lenBufSize)
if err != nil && err != io.EOF {
log.Error(err)
continue
}

len, vn := proto.DecodeVarint(msgLen[0:n])
if len == 0 {
l, vn := proto.DecodeVarint(msgLen)
if l == 0 {
continue
}

line := make([]byte, len)
if vn < lenBufSize {
copy(line[0:lenBufSize-vn], msgLen[vn:lenBufSize])
_, err = rdr.Discard(vn)
if err != nil {
log.Error(err)
continue
}

n, err = io.ReadFull(rdr, line[lenBufSize-vn:])
line := make([]byte, l)

_, err = io.ReadFull(rdr, line)
if err != nil && err != io.EOF {
log.Error(err)
continue
Expand Down

0 comments on commit 8d59905

Please sign in to comment.