diff options
author | Sam Truzjan <pxqr.sta@gmail.com> | 2014-01-05 20:19:01 +0400 |
---|---|---|
committer | Sam Truzjan <pxqr.sta@gmail.com> | 2014-01-05 20:19:01 +0400 |
commit | f1d79ea2180e46545f442985a85e5bb1cce90436 (patch) | |
tree | 1fc753798ee3fc228c60b94c286a337b4972eed9 /src/System/Torrent | |
parent | 3eaa3003d9ece714595dffbf07202ee90336d05f (diff) |
Check size of last piece properly (in writePiece)
Diffstat (limited to 'src/System/Torrent')
-rw-r--r-- | src/System/Torrent/Storage.hs | 20 |
1 files changed, 16 insertions, 4 deletions
diff --git a/src/System/Torrent/Storage.hs b/src/System/Torrent/Storage.hs index 3bb229a4..8aa1aa99 100644 --- a/src/System/Torrent/Storage.hs +++ b/src/System/Torrent/Storage.hs | |||
@@ -96,11 +96,23 @@ isValidIx i s = 0 <= i && i < totalPieces s | |||
96 | 96 | ||
97 | writePiece :: Piece BL.ByteString -> Storage -> IO () | 97 | writePiece :: Piece BL.ByteString -> Storage -> IO () |
98 | writePiece p @ Piece {..} s @ Storage {..} | 98 | writePiece p @ Piece {..} s @ Storage {..} |
99 | | mode == ReadOnly = throwIO StorageIsRO | 99 | | mode == ReadOnly = throwIO StorageIsRO |
100 | | pieceSize p /= pieceLen = throwIO (InvalidSize (pieceSize p)) | 100 | | isNotValidIx pieceIndex = throwIO (InvalidIndex pieceIndex) |
101 | | not (isValidIx pieceIndex s) = throwIO (InvalidIndex pieceIndex) | 101 | | isNotValidSize pieceIndex (pieceSize p) |
102 | | otherwise = writeBytes offset pieceData fileMap | 102 | = throwIO (InvalidSize (pieceSize p)) |
103 | | otherwise = writeBytes offset pieceData fileMap | ||
103 | where | 104 | where |
105 | isNotValidSize pix psize | ||
106 | | succ pix == pcount = psize /= lastPieceLen -- last piece may be shorter | ||
107 | | otherwise = psize /= pieceLen | ||
108 | where | ||
109 | lastPieceLen = fromIntegral (FM.size fileMap `rem` fromIntegral pieceLen) | ||
110 | {-# INLINE isNotValidSize #-} | ||
111 | |||
112 | isNotValidIx i = i < 0 || i >= pcount | ||
113 | {-# INLINE isNotValidIx #-} | ||
114 | |||
115 | pcount = totalPieces s | ||
104 | offset = fromIntegral pieceIndex * fromIntegral pieceLen | 116 | offset = fromIntegral pieceIndex * fromIntegral pieceLen |
105 | 117 | ||
106 | readPiece :: PieceIx -> Storage -> IO (Piece BL.ByteString) | 118 | readPiece :: PieceIx -> Storage -> IO (Piece BL.ByteString) |