Skip to content

Commit 68f05d6

Browse files
committed
fix some warnings and integer overflow with torrents that are too big
1 parent c66f4cc commit 68f05d6

9 files changed

+28
-21
lines changed

Jamfile

+4-3
Original file line numberDiff line numberDiff line change
@@ -393,12 +393,13 @@ rule tag ( name : type ? : property-set )
393393
feature ipv6 : on off : composite propagated link-incompatible ;
394394
feature.compose <ipv6>off : <define>TORRENT_USE_IPV6=0 ;
395395

396-
feature sanitize : off address bounds undefined thread rtc : composite propagated link-incompatible ;
396+
feature sanitize : off address memory bounds undefined thread rtc : composite propagated link-incompatible ;
397397
# sanitize is a clang and GCC feature
398-
feature.compose <sanitize>bounds : <cflags>-fsanitize=bounds <cflags>-fsanitize-undefined-trap-on-error <linkflags>-fsanitize=bounds <linkflags>-fsanitize-undefined-trap-on-error ;
399-
feature.compose <sanitize>undefined : <cflags>-fsanitize=undefined <linkflags>-fsanitize=undefined ;
398+
feature.compose <sanitize>bounds : <cflags>-fsanitize=bounds <cflags>-fsanitize-undefined-trap-on-error <linkflags>-fsanitize-undefined-trap-on-error ;
399+
feature.compose <sanitize>undefined : <cflags>-fsanitize=undefined <cflags>-fsanitize-undefined-trap-on-error <linkflags>-fsanitize=undefined <linkflags>-fsanitize-undefined-trap-on-error ;
400400
feature.compose <sanitize>thread : <cflags>-fsanitize=thread <linkflags>-fsanitize=thread ;
401401
feature.compose <sanitize>address : <cflags>-fsanitize=address <linkflags>-fsanitize=address ;
402+
feature.compose <sanitize>memory : <cflags>-fsanitize=memory <linkflags>-fsanitize=memory ;
402403
# RTC (runtime check) is an msvc feature
403404
feature.compose <sanitize>rtc : <cflags>/RTCc <cflags>/RTCsu ;
404405

include/libtorrent/assert.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ extern char const* libtorrent_assert_log;
101101
#endif
102102

103103
#define TORRENT_ASSERT_FAIL() \
104-
assert_fail("<unconditional>", __LINE__, __FILE__, TORRENT_FUNCTION, 0, 0)
104+
assert_fail("<unconditional>", __LINE__, __FILE__, TORRENT_FUNCTION, nullptr, 0)
105105

106106
#else
107107
#include <cassert>

include/libtorrent/peer_connection.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ namespace aux {
282282

283283
void set_peer_info(torrent_peer* pi) override
284284
{
285-
TORRENT_ASSERT(m_peer_info == 0 || pi == 0 );
285+
TORRENT_ASSERT(m_peer_info == nullptr || pi == nullptr );
286286
TORRENT_ASSERT(pi != nullptr || m_disconnect_started);
287287
m_peer_info = pi;
288288
}

include/libtorrent/piece_picker.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,7 @@ namespace libtorrent {
408408

409409
void check_peer_invariant(typed_bitfield<piece_index_t> const& have
410410
, torrent_peer const* p) const;
411-
void check_invariant(const torrent* t = 0) const;
411+
void check_invariant(const torrent* t = nullptr) const;
412412
#endif
413413

414414
// functor that compares indices on downloading_pieces

src/disk_io_thread.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,10 @@ namespace libtorrent {
7575
#if TORRENT_USE_ASSERTS
7676

7777
#define TORRENT_PIECE_ASSERT(cond, piece) \
78-
do { if (!(cond)) { assert_print_piece(piece); assert_fail(#cond, __LINE__, __FILE__, TORRENT_FUNCTION, 0); } } TORRENT_WHILE_0
78+
do { if (!(cond)) { assert_print_piece(piece); assert_fail(#cond, __LINE__, __FILE__, TORRENT_FUNCTION, nullptr); } } TORRENT_WHILE_0
7979

8080
#define TORRENT_PIECE_ASSERT_FAIL(piece) \
81-
do { assert_print_piece(piece); assert_fail("<unconditional>", __LINE__, __FILE__, TORRENT_FUNCTION, 0); } TORRENT_WHILE_0
81+
do { assert_print_piece(piece); assert_fail("<unconditional>", __LINE__, __FILE__, TORRENT_FUNCTION, nullptr); } TORRENT_WHILE_0
8282

8383
#else
8484
#define TORRENT_PIECE_ASSERT(cond, piece) do {} TORRENT_WHILE_0

src/torrent_info.cpp

+10-8
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ namespace libtorrent {
9999
static const char invalid_chars[] = "/\\";
100100
#endif
101101
if (c > 127) return false;
102-
return std::strchr(invalid_chars, static_cast<char>(c)) != NULL;
102+
return std::strchr(invalid_chars, static_cast<char>(c)) != nullptr;
103103
}
104104

105105
} // anonymous namespace
@@ -1014,6 +1014,15 @@ namespace {
10141014
return false;
10151015
}
10161016

1017+
// we expect the piece hashes to be < 2 GB in size
1018+
if (files.num_pieces() >= std::numeric_limits<int>::max() / 20)
1019+
{
1020+
ec = errors::too_many_pieces_in_torrent;
1021+
// mark the torrent as invalid
1022+
m_files.set_piece_length(0);
1023+
return false;
1024+
}
1025+
10171026
if (pieces)
10181027
{
10191028
if (pieces.string_length() != files.num_pieces() * 20)
@@ -1038,13 +1047,6 @@ namespace {
10381047
m_files.set_piece_length(0);
10391048
return false;
10401049
}
1041-
if (files.num_pieces() >= std::numeric_limits<int>::max() / 2)
1042-
{
1043-
ec = errors::too_many_pieces_in_torrent;
1044-
// mark the torrent as invalid
1045-
m_files.set_piece_length(0);
1046-
return false;
1047-
}
10481050
int const num_leafs = merkle_num_leafs(files.num_pieces());
10491051
int const num_nodes = merkle_num_nodes(num_leafs);
10501052
m_merkle_first_leaf = num_nodes - num_leafs;

test/setup_transfer.hpp

+7-5
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ EXPORT lt::alert const* wait_for_alert(
7676
EXPORT void print_ses_rate(float time
7777
, lt::torrent_status const* st1
7878
, lt::torrent_status const* st2
79-
, lt::torrent_status const* st3 = NULL);
79+
, lt::torrent_status const* st3 = nullptr);
8080

8181
EXPORT bool print_alerts(lt::session& ses, char const* name
8282
, bool allow_no_torrents = false
@@ -96,7 +96,7 @@ EXPORT std::shared_ptr<lt::torrent_info> make_torrent(const int file_sizes[]
9696
EXPORT void create_random_files(std::string const& path, const int file_sizes[]
9797
, int num_files, libtorrent::file_storage* fs = nullptr);
9898

99-
EXPORT std::shared_ptr<lt::torrent_info> create_torrent(std::ostream* file = 0
99+
EXPORT std::shared_ptr<lt::torrent_info> create_torrent(std::ostream* file = nullptr
100100
, char const* name = "temporary", int piece_size = 16 * 1024, int num_pieces = 13
101101
, bool add_tracker = true, std::string ssl_certificate = "");
102102

@@ -106,9 +106,11 @@ EXPORT std::tuple<lt::torrent_handle
106106
setup_transfer(lt::session* ses1, lt::session* ses2
107107
, lt::session* ses3, bool clear_files, bool use_metadata_transfer = true
108108
, bool connect = true, std::string suffix = "", int piece_size = 16 * 1024
109-
, std::shared_ptr<lt::torrent_info>* torrent = 0, bool super_seeding = false
110-
, lt::add_torrent_params const* p = 0, bool stop_lsd = true, bool use_ssl_ports = false
111-
, std::shared_ptr<lt::torrent_info>* torrent2 = 0);
109+
, std::shared_ptr<lt::torrent_info>* torrent = nullptr
110+
, bool super_seeding = false
111+
, lt::add_torrent_params const* p = nullptr
112+
, bool stop_lsd = true, bool use_ssl_ports = false
113+
, std::shared_ptr<lt::torrent_info>* torrent2 = nullptr);
112114

113115
EXPORT int start_web_server(bool ssl = false, bool chunked = false
114116
, bool keepalive = true, int min_interval = 30);

test/test_torrent_info.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ test_failing_torrent_t test_error_torrents[] =
153153
{ "invalid_root_hash2.torrent", errors::torrent_missing_pieces },
154154
{ "invalid_file_size.torrent", errors::torrent_invalid_length },
155155
{ "invalid_symlink.torrent", errors::torrent_invalid_name },
156+
{ "many_pieces.torrent", errors::too_many_pieces_in_torrent },
156157
};
157158

158159
// TODO: test remap_files
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
d10:created by10:libtorrent13:creation datei1359599503e4:infod6:lengthi1759218597889e4:name4:temp12:piece lengthi16384e6:pieces20:����&��JW�}�A4u,����ee

0 commit comments

Comments
 (0)