]> Sergey Matveev's repositories - btrtrc.git/commitdiff
Add some metainfo validation
authorMatt Joiner <anacrolix@gmail.com>
Tue, 2 Jun 2015 14:17:58 +0000 (00:17 +1000)
committerMatt Joiner <anacrolix@gmail.com>
Tue, 2 Jun 2015 14:17:58 +0000 (00:17 +1000)
misc.go
torrent.go

diff --git a/misc.go b/misc.go
index c567f68d5631d320732873890a7c8372d4d9c486..3cf1b8ee75b1dba2d78dca0de4d9a43c0d9d07d3 100644 (file)
--- a/misc.go
+++ b/misc.go
@@ -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
+}
index ef82e992f604f7f0f6e08221ecd5e7b1a987427d..e32175e9429a4ab74159334aa95e326cc92da89c 100644 (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() {