From 7acb84835b42d4d3c5484536f74f24071c0d10c3 Mon Sep 17 00:00:00 2001 From: lspgn Date: Sat, 8 Jan 2022 00:22:22 +0100 Subject: [PATCH] Bugfix: issues when reading a partial chunk of protobuf from stdin --- cmd/enricher/main.go | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/cmd/enricher/main.go b/cmd/enricher/main.go index 55b0a7f..fa43004 100644 --- a/cmd/enricher/main.go +++ b/cmd/enricher/main.go @@ -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