Skip to content

Commit 8b8242f

Browse files
committed
fixup recent change to piece_size_for_req(). it's a bit more complicated; v1 (and hybrid) torrents still need the full piece size to compute the correct piece hash
1 parent aa59c7c commit 8b8242f

4 files changed

Lines changed: 17 additions & 17 deletions

File tree

include/libtorrent/torrent_handle.hpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -290,9 +290,7 @@ namespace aux {
290290
// won't return until the libtorrent thread has copied the data into its
291291
// disk write buffer. ``data`` is expected to point to a buffer of as
292292
// many bytes as the size of the specified piece.
293-
// For v2 torrents, pieces at the end of files may not be full sized.
294-
// For backwards compatibility, it's OK to pass a full sized piece as
295-
// well.
293+
// For v2-only torrents, pieces at the end of files may not be full sized.
296294
//
297295
// The data in the buffer is copied and passed on to the disk IO thread
298296
// to be written at some later point in time.

include/libtorrent/torrent_info.hpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -506,13 +506,15 @@ TORRENT_VERSION_NAMESPACE_3
506506
int piece_size(piece_index_t index) const { return m_files.piece_size(index); }
507507

508508
// returns the piece size appropriate for computing request lengths.
509-
// for v2 torrents, pieces at the end of files may be shorter than
510-
// the main piece size. This is the case for hybrid torrents as well.
509+
// for v2-only torrents, pieces at the end of files may be shorter than
510+
// the main piece size. For v1 and hybrid torrents, piece sizes must be
511+
// full (except for the last piece) in order to correctly compute the
512+
// piece hash.
511513
int piece_size_for_req(piece_index_t index) const
512514
{
513-
return v2()
514-
? m_files.piece_size2(index)
515-
: m_files.piece_size(index);
515+
return v1()
516+
? m_files.piece_size(index)
517+
: m_files.piece_size2(index);
516518
}
517519

518520
// ``hash_for_piece()`` takes a piece-index and returns the 20-bytes

src/torrent.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1371,18 +1371,19 @@ aux::vector<download_priority_t, piece_index_t> file_to_piece_prio(
13711371
{
13721372
TORRENT_ASSERT(is_single_thread());
13731373

1374+
TORRENT_ASSERT_PRECOND(piece < torrent_file().end_piece());
1375+
TORRENT_ASSERT_PRECOND(piece >= piece_index_t{0});
1376+
TORRENT_ASSERT_PRECOND(data.size() == std::size_t(m_torrent_file->piece_size_for_req(piece)));
1377+
13741378
// make sure the piece index is correct
13751379
if (piece >= torrent_file().end_piece())
13761380
return;
13771381

13781382
// make sure the piece size is correct
1379-
// we check against the v1 piece size as well, for backwards compatibility
1380-
if (data.size() == std::size_t(m_torrent_file->piece_size_for_req(piece))
1381-
|| data.size() == std::size_t(m_torrent_file->piece_size(piece)))
1382-
{
1383-
data.resize(std::size_t(m_torrent_file->piece_size_for_req(piece)));
1384-
add_piece(piece, data.data(), flags);
1385-
}
1383+
if (data.size() != std::size_t(m_torrent_file->piece_size_for_req(piece)))
1384+
return;
1385+
1386+
add_piece(piece, data.data(), flags);
13861387
}
13871388

13881389
// TODO: 3 there's some duplication between this function and

src/web_peer_connection.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -465,8 +465,7 @@ void web_peer_connection::write_request(peer_request const& r)
465465
if (info.orig_files().pad_file_at(f.file_index))
466466
{
467467
++num_pad_files;
468-
if (!info.v2())
469-
m_file_requests.push_back(file_req);
468+
m_file_requests.push_back(file_req);
470469
continue;
471470
}
472471

0 commit comments

Comments
 (0)