From 7dd932bd5a24522d5ddd2500091c1a11ec6926f6 Mon Sep 17 00:00:00 2001 From: Matt Joiner Date: Mon, 25 Aug 2014 22:15:45 +1000 Subject: [PATCH] Fix a few crashes --- torrent.go | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/torrent.go b/torrent.go index 4c2a476e..0585a5f1 100644 --- a/torrent.go +++ b/torrent.go @@ -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)) -- 2.48.1