Add some metainfo validation

This commit is contained in:
Matt Joiner 2015-06-03 00:17:58 +10:00
parent 07ed952834
commit af127dfd0f
2 changed files with 16 additions and 0 deletions

11
misc.go
View File

@ -6,6 +6,7 @@ import (
"fmt"
"time"
"github.com/anacrolix/torrent/metainfo"
pp "github.com/anacrolix/torrent/peer_protocol"
)
@ -107,3 +108,13 @@ func torrentRequestOffset(torrentLength, pieceSize int64, r request) (off int64)
}
return
}
func validateInfo(info *metainfo.Info) error {
if len(info.Pieces)%20 != 0 {
return errors.New("pieces has invalid length")
}
if int((info.TotalLength()+info.PieceLength-1)/info.PieceLength) != info.NumPieces() {
return errors.New("piece count and file lengths are at odds")
}
return nil
}

View File

@ -208,6 +208,11 @@ func infoPieceHashes(info *metainfo.Info) (ret []string) {
// Called when metadata for a torrent becomes available.
func (t *torrent) setMetadata(md *metainfo.Info, infoBytes []byte, eventLocker sync.Locker) (err error) {
err = validateInfo(md)
if err != nil {
err = fmt.Errorf("bad info: %s", err)
return
}
t.Info = md
t.length = 0
for _, f := range t.Info.UpvertedFiles() {