check that util.CompactPeer is unmarshaled from the correct number of bytes

This commit is contained in:
Matt Joiner 2014-11-16 13:05:19 -06:00
parent 7caf3b32eb
commit e3048403ce
1 changed files with 10 additions and 1 deletions

View 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
}