]> Sergey Matveev's repositories - btrtrc.git/commitdiff
check that util.CompactPeer is unmarshaled from the correct number of bytes
authorMatt Joiner <anacrolix@gmail.com>
Sun, 16 Nov 2014 19:05:19 +0000 (13:05 -0600)
committerMatt Joiner <anacrolix@gmail.com>
Sun, 16 Nov 2014 19:05:19 +0000 (13:05 -0600)
util/types.go

index 03dcb1fabd0291dea638106cbab74818867ac8dd..b3c876a50a0fdb07d33bfe9205e6da77c53dd014 100644 (file)
@@ -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
 }