From e8802e32159c040a389d280ae1f59bc27be60f2d Mon Sep 17 00:00:00 2001 From: Dan Sparks Date: Mon, 29 Sep 2014 18:01:50 -0700 Subject: [PATCH] Check if buffer read is complete before decoding --- worker/agent.go | 4 +++- worker/inpack.go | 2 ++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/worker/agent.go b/worker/agent.go index 2236173..b6af74c 100644 --- a/worker/agent.go +++ b/worker/agent.go @@ -2,6 +2,7 @@ package worker import ( "bufio" + "encoding/binary" "net" "sync" ) @@ -77,7 +78,8 @@ func (a *agent) work() { if len(leftdata) > 0 { // some data left for processing data = append(leftdata, data...) } - if len(data) < minPacketLength { // not enough data + length := len(data) - minPacketLength + if length < 0 || length < int(binary.BigEndian.Uint32(data[8:12])) { leftdata = data continue } diff --git a/worker/inpack.go b/worker/inpack.go index fc4717a..c777b80 100644 --- a/worker/inpack.go +++ b/worker/inpack.go @@ -86,6 +86,8 @@ func (inpack *inPack) UpdateStatus(numerator, denominator int) { // Decode job from byte slice func decodeInPack(data []byte) (inpack *inPack, l int, err error) { + // The next three checks should be completely unnecessary, as they are checked in + // agent.work. if len(data) < minPacketLength { // valid package should not less 12 bytes err = fmt.Errorf("Invalid data: %v", data) return