connection.go | 9 +++++---- diff --git a/connection.go b/connection.go index 397be949c00c3e6bb0b8163c38ab2623be85be42..8a137e959128ecaecdc90b1d5de894b4cc816a3d 100644 --- a/connection.go +++ b/connection.go @@ -1025,10 +1025,6 @@ } 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 @@ -1055,6 +1051,11 @@ // from our storage, and can't communicate this to peers // except by reconnecting. 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)