From: Matt Joiner Date: Fri, 22 Aug 2014 07:39:18 +0000 (+1000) Subject: Clarify EOF in message decoding X-Git-Tag: v1.0.0~1617 X-Git-Url: http://www.git.stargrave.org/?a=commitdiff_plain;h=a2fabedfa2354aac39a5fb1260a906c6574cc7ee;p=btrtrc.git Clarify EOF in message decoding --- diff --git a/peer_protocol/protocol.go b/peer_protocol/protocol.go index 87cf38d8..db32ac59 100644 --- a/peer_protocol/protocol.go +++ b/peer_protocol/protocol.go @@ -109,10 +109,14 @@ type Decoder struct { MaxLength Integer // TODO: Should this include the length header or not? } +// io.EOF is returned if the source terminates cleanly on a message boundary. func (d *Decoder) Decode(msg *Message) (err error) { var length Integer err = binary.Read(d.R, binary.BigEndian, &length) if err != nil { + if err != io.EOF { + err = fmt.Errorf("error reading message length: %s", err) + } return } if length > d.MaxLength { @@ -125,10 +129,13 @@ func (d *Decoder) Decode(msg *Message) (err error) { msg.Keepalive = false b := make([]byte, length) _, err = io.ReadFull(d.R, b) - if err == io.EOF { - err = io.ErrUnexpectedEOF - } if err != nil { + if err == io.EOF { + err = io.ErrUnexpectedEOF + } + if err != io.ErrUnexpectedEOF { + err = fmt.Errorf("error reading message: %s", err) + } return } r := bytes.NewReader(b) diff --git a/peer_protocol/protocol_test.go b/peer_protocol/protocol_test.go index 807f1dd8..d2fc8d4b 100644 --- a/peer_protocol/protocol_test.go +++ b/peer_protocol/protocol_test.go @@ -3,7 +3,6 @@ package peer_protocol import ( "bufio" "bytes" - "io" "strings" "testing" ) @@ -103,8 +102,8 @@ func TestUnexpectedEOF(t *testing.T) { MaxLength: 42, } err := dec.Decode(msg) - if err != io.ErrUnexpectedEOF { - t.Fatalf("expected ErrUnexpectedEOF decoding %q, got %s", stream, err) + if err == nil { + t.Fatalf("expected an error decoding %q", stream) } } }