From: Matt Joiner Date: Fri, 23 Aug 2019 02:19:28 +0000 (+1000) Subject: Fix a crash when receiving a request when we don't yet have the torrent info X-Git-Tag: v1.7.1^0 X-Git-Url: http://www.git.stargrave.org/?a=commitdiff_plain;h=95a521bad6f5cc17d6c40f888add262a6c40fc20;p=btrtrc.git Fix a crash when receiving a request when we don't yet have the torrent info --- diff --git a/connection.go b/connection.go index 397be949..8a137e95 100644 --- a/connection.go +++ b/connection.go @@ -1025,10 +1025,6 @@ func (c *connection) reject(r request) { func (c *connection) onReadRequest(r request) error { requestedChunkLengths.Add(strconv.FormatUint(r.Length.Uint64(), 10), 1) - if r.Begin+r.Length > c.t.pieceLength(pieceIndex(r.Index)) { - torrent.Add("bad requests received", 1) - return errors.New("bad request") - } if _, ok := c.PeerRequests[r]; ok { torrent.Add("duplicate requests received", 1) return nil @@ -1056,6 +1052,11 @@ func (c *connection) onReadRequest(r request) error { requestsReceivedForMissingPieces.Add(1) return fmt.Errorf("peer requested piece we don't have: %v", r.Index.Int()) } + // Check this after we know we have the piece, so that the piece length will be known. + if r.Begin+r.Length > c.t.pieceLength(pieceIndex(r.Index)) { + torrent.Add("bad requests received", 1) + return errors.New("bad request") + } if c.PeerRequests == nil { c.PeerRequests = make(map[request]struct{}, maxRequests) }