From: Matt Joiner Date: Sat, 23 Aug 2014 20:54:14 +0000 (+1000) Subject: Add some checks to TorrentReadAt for 0 byte reads X-Git-Tag: v1.0.0~1607 X-Git-Url: http://www.git.stargrave.org/?a=commitdiff_plain;h=8f065e473f0dfa474356d0758889fd79c3ee294c;p=btrtrc.git Add some checks to TorrentReadAt for 0 byte reads --- diff --git a/client.go b/client.go index 2a20b263..86f90881 100644 --- a/client.go +++ b/client.go @@ -161,10 +161,14 @@ func (cl *Client) TorrentReadAt(ih InfoHash, off int64, p []byte) (n int, err er return } piece := t.Pieces[index] - pieceOff := pp.Integer(off % int64(t.PieceLength(0))) - high := int(t.PieceLength(index) - pieceOff) - if high < len(p) { - p = p[:high] + pieceOff := pp.Integer(off % int64(t.UsualPieceSize())) + pieceLeft := int(t.PieceLength(index) - pieceOff) + if pieceLeft <= 0 { + err = io.EOF + return + } + if len(p) > pieceLeft { + p = p[:pieceLeft] } for cs, _ := range piece.PendingChunkSpecs { chunkOff := int64(pieceOff) - int64(cs.Begin) @@ -181,6 +185,9 @@ func (cl *Client) TorrentReadAt(ih InfoHash, off int64, p []byte) (n int, err er p = p[:-chunkOff] } } + if len(p) == 0 { + panic(len(p)) + } return t.Data.ReadAt(p, off) }