From e3048403ce1c09dc022573900d82286ff7f1f215 Mon Sep 17 00:00:00 2001 From: Matt Joiner Date: Sun, 16 Nov 2014 13:05:19 -0600 Subject: [PATCH] check that util.CompactPeer is unmarshaled from the correct number of bytes --- util/types.go | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) 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 } -- 2.44.0