From: Matt Joiner Date: Sun, 16 Nov 2014 19:05:19 +0000 (-0600) Subject: check that util.CompactPeer is unmarshaled from the correct number of bytes X-Git-Tag: v1.0.0~1549 X-Git-Url: http://www.git.stargrave.org/?a=commitdiff_plain;h=e3048403ce1c09dc022573900d82286ff7f1f215;p=btrtrc.git check that util.CompactPeer is unmarshaled from the correct number of bytes --- diff --git a/util/types.go b/util/types.go index 03dcb1fa..b3c876a5 100644 --- a/util/types.go +++ b/util/types.go @@ -4,6 +4,8 @@ import ( "bytes" "encoding" "encoding/binary" + "fmt" + "github.com/anacrolix/libtorgo/bencode" ) @@ -39,6 +41,13 @@ type CompactPeer struct { var _ encoding.BinaryUnmarshaler = &CompactPeer{} func (cp *CompactPeer) UnmarshalBinary(b []byte) (err error) { - err = binary.Read(bytes.NewReader(b), binary.BigEndian, cp) + r := bytes.NewReader(b) + err = binary.Read(r, binary.BigEndian, cp) + if err != nil { + return + } + if r.Len() != 0 { + err = fmt.Errorf("%d bytes unused", r.Len()) + } return }