Skip to content

Commit

Permalink
fix piece size check when loading torrents
Browse files Browse the repository at this point in the history
  • Loading branch information
arvidn committed Oct 1, 2024
1 parent 0ca20a3 commit 8ad05eb
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 8 deletions.
1 change: 1 addition & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
* fix piece size check when loading torrents
* back-port fix last_upload and last_download resume data fields to use posix time
* back-port treat CGNAT address range as local IPs

Expand Down
4 changes: 2 additions & 2 deletions include/libtorrent/piece_picker.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,9 @@ namespace libtorrent {
// priority factor
prio_factor = 3,
// max blocks per piece
// there are counters in downloading_piece that only have 15 bits to
// there are counters in downloading_piece that only have 14 bits to
// count blocks per piece, that's restricting this
max_blocks_per_piece = (1 << 15) - 1
max_blocks_per_piece = (1 << 14) - 1
};

struct block_info
Expand Down
9 changes: 8 additions & 1 deletion src/torrent_info.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/announce_entry.hpp"
#include "libtorrent/hex.hpp" // to_hex
#include "libtorrent/aux_/numeric_cast.hpp"
#include "libtorrent/piece_picker.hpp"
#include "libtorrent/disk_interface.hpp" // for default_block_size

#if TORRENT_ABI_VERSION == 1
#include "libtorrent/lazy_entry.hpp"
Expand Down Expand Up @@ -1027,11 +1029,16 @@ namespace {

// extract piece length
std::int64_t piece_length = info.dict_find_int_value("piece length", -1);
if (piece_length <= 0 || piece_length > std::numeric_limits<int>::max())
if (piece_length <= 0)
{
ec = errors::torrent_missing_piece_length;
return false;
}
if (piece_length > piece_picker::max_blocks_per_piece * default_block_size)
{
ec = errors::invalid_piece_size;
return false;
}
file_storage files;
files.set_piece_length(static_cast<int>(piece_length));

Expand Down
21 changes: 16 additions & 5 deletions test/test_torrent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ void test_running_torrent(std::shared_ptr<torrent_info> info, std::int64_t file_
TEST_CHECK(h.get_file_priorities() == prio);
}

void test_large_piece_size(int const size)
void test_large_piece_size(std::int64_t const size)
{
entry torrent;
entry& info = torrent["info"];
Expand All @@ -179,7 +179,15 @@ void test_large_piece_size(int const size)
std::vector<char> buf;
bencode(std::back_inserter(buf), torrent);
add_torrent_params atp;
atp.ti = std::make_shared<torrent_info>(buf, from_span);
try
{
atp.ti = std::make_shared<torrent_info>(buf, from_span);
}
catch (lt::system_error const& e)
{
TEST_CHECK(e.code() == error_code(lt::errors::invalid_piece_size));
return;
}
atp.save_path = ".";

lt::session ses;
Expand Down Expand Up @@ -212,9 +220,12 @@ TORRENT_TEST(long_names)

TORRENT_TEST(large_piece_size)
{
test_large_piece_size(32768 * 16 * 1024);
test_large_piece_size(65536 * 16 * 1024);
test_large_piece_size(65537 * 16 * 1024);
test_large_piece_size(0xfffc000 + 0x4000);
test_large_piece_size(0x10000000);
test_large_piece_size(0x20000000);
test_large_piece_size(0x40000000);
test_large_piece_size(0x80000000);
test_large_piece_size(0x100000000);
}

TORRENT_TEST(total_wanted)
Expand Down

0 comments on commit 8ad05eb

Please sign in to comment.