]> Sergey Matveev's repositories - btrtrc.git/commitdiff
Fix a few crashes
authorMatt Joiner <anacrolix@gmail.com>
Mon, 25 Aug 2014 12:15:45 +0000 (22:15 +1000)
committerMatt Joiner <anacrolix@gmail.com>
Mon, 25 Aug 2014 12:15:45 +0000 (22:15 +1000)
torrent.go

index 4c2a476ebe1ec7f7452653f50a17b4d6ac813fe7..0585a5f1dd4835750d7d34d1128b8a3fd39c3939 100644 (file)
@@ -124,7 +124,7 @@ func (t *torrent) HaveMetadataPiece(piece int) bool {
        if t.haveInfo() {
                return (1<<14)*piece < len(t.MetaData)
        } else {
-               return t.metadataHave[piece]
+               return piece < len(t.metadataHave) && t.metadataHave[piece]
        }
 }
 
@@ -252,7 +252,13 @@ func (t *torrent) NewMetadataExtensionMessage(c *connection, msgType int, piece
 
 func (t *torrent) WriteStatus(w io.Writer) {
        fmt.Fprintf(w, "Infohash: %x\n", t.InfoHash)
-       fmt.Fprintf(w, "Piece length: %d\n", t.UsualPieceSize())
+       fmt.Fprintf(w, "Piece length: %s\n", func() string {
+               if t.haveInfo() {
+                       return fmt.Sprint(t.UsualPieceSize())
+               } else {
+                       return "?"
+               }
+       }())
        fmt.Fprint(w, "Pieces: ")
        for index := range t.Pieces {
                fmt.Fprintf(w, "%c", t.pieceStatusChar(index))