From 65fef972a285d8403e934f0b61a9218746b41561 Mon Sep 17 00:00:00 2001 From: arvidn Date: Thu, 11 Jan 2018 01:35:15 +0100 Subject: [PATCH] apply some c++11 modernization --- include/libtorrent/assert.hpp | 4 +- include/libtorrent/aux_/alloca.hpp | 12 +-- include/libtorrent/aux_/disk_job_fence.hpp | 5 +- include/libtorrent/aux_/file_progress.hpp | 2 +- include/libtorrent/broadcast_socket.hpp | 4 +- include/libtorrent/i2p_stream.hpp | 2 + include/libtorrent/kademlia/dht_storage.hpp | 8 +- include/libtorrent/timestamp_history.hpp | 11 +-- include/libtorrent/upnp.hpp | 2 +- src/add_torrent_params.cpp | 2 +- src/alert.cpp | 2 +- src/bdecode.cpp | 4 +- src/block_cache.cpp | 7 +- src/broadcast_socket.cpp | 4 +- src/crc32c.cpp | 2 +- src/create_torrent.cpp | 7 +- src/disk_io_thread_pool.cpp | 4 +- src/disk_job_fence.cpp | 4 +- src/enum_net.cpp | 4 +- src/ffs.cpp | 8 +- src/file.cpp | 12 +-- src/file_progress.cpp | 4 - src/file_storage.cpp | 4 +- src/gzip.cpp | 6 +- src/hasher512.cpp | 6 +- src/http_seed_connection.cpp | 3 +- src/http_tracker_connection.cpp | 2 +- src/i2p_stream.cpp | 16 ++-- src/identify_client.cpp | 15 ++-- src/ip_notifier.cpp | 12 ++- src/ip_voter.cpp | 4 +- src/kademlia/dht_state.cpp | 6 +- src/kademlia/dht_storage.cpp | 12 +-- src/kademlia/dht_tracker.cpp | 5 +- src/kademlia/dos_blocker.cpp | 6 +- src/kademlia/get_item.cpp | 4 +- src/kademlia/node.cpp | 23 +++-- src/kademlia/node_id.cpp | 4 +- src/kademlia/put_data.cpp | 2 +- src/kademlia/routing_table.cpp | 23 +++-- src/lazy_bdecode.cpp | 9 +- src/magnet_uri.cpp | 2 +- src/part_file.cpp | 2 +- src/path.cpp | 2 +- src/pe_crypto.cpp | 13 ++- src/peer_connection.cpp | 6 +- src/peer_connection_handle.cpp | 6 +- src/piece_picker.cpp | 93 +++++++++------------ src/platform_util.cpp | 4 +- src/puff.cpp | 18 ++-- src/request_blocks.cpp | 25 +++--- src/session.cpp | 4 +- src/session_handle.cpp | 6 +- src/session_impl.cpp | 32 ++++--- src/smart_ban.cpp | 20 ++--- src/socks5_stream.cpp | 6 +- src/stack_allocator.cpp | 4 +- src/string_util.cpp | 2 +- src/timestamp_history.cpp | 15 ++-- src/torrent.cpp | 34 ++++---- src/torrent_handle.cpp | 10 +-- src/udp_socket.cpp | 12 +-- src/udp_tracker_connection.cpp | 6 +- src/upnp.cpp | 28 +++---- src/ut_metadata.cpp | 13 +-- src/utf8.cpp | 6 +- src/utp_socket_manager.cpp | 4 +- src/utp_stream.cpp | 16 ++-- src/web_peer_connection.cpp | 11 ++- test/test_upnp.cpp | 2 +- 70 files changed, 328 insertions(+), 350 deletions(-) diff --git a/include/libtorrent/assert.hpp b/include/libtorrent/assert.hpp index 92efb0d4a..b2b7adf53 100644 --- a/include/libtorrent/assert.hpp +++ b/include/libtorrent/assert.hpp @@ -50,10 +50,10 @@ TORRENT_EXPORT void print_backtrace(char* out, int len, int max_depth = 0, void* #define TORRENT_WHILE_0 \ __pragma( warning(push) ) \ __pragma( warning(disable:4127) ) \ - while (0) \ + while (false) \ __pragma( warning(pop) ) #else -#define TORRENT_WHILE_0 while (0) +#define TORRENT_WHILE_0 while (false) #endif diff --git a/include/libtorrent/aux_/alloca.hpp b/include/libtorrent/aux_/alloca.hpp index a789e6a8f..fedceb0e0 100644 --- a/include/libtorrent/aux_/alloca.hpp +++ b/include/libtorrent/aux_/alloca.hpp @@ -77,8 +77,8 @@ struct alloca_destructor #include #define TORRENT_ALLOCA(v, t, n) ::libtorrent::aux::typed_span v; { \ - std::size_t TORRENT_ALLOCA_size = ::libtorrent::aux::numeric_cast(n); \ - t* TORRENT_ALLOCA_tmp = static_cast(_alloca(sizeof(t) * TORRENT_ALLOCA_size)); \ + auto TORRENT_ALLOCA_size = ::libtorrent::aux::numeric_cast(n); \ + auto* TORRENT_ALLOCA_tmp = static_cast(_alloca(sizeof(t) * TORRENT_ALLOCA_size)); \ v = ::libtorrent::aux::typed_span(TORRENT_ALLOCA_tmp, TORRENT_ALLOCA_size); \ ::libtorrent::aux::uninitialized_default_construct(v.begin(), v.end()); \ } \ @@ -88,8 +88,8 @@ struct alloca_destructor #include #define TORRENT_ALLOCA(v, t, n) ::libtorrent::aux::typed_span v; { \ - std::size_t TORRENT_ALLOCA_size = ::libtorrent::aux::numeric_cast(n); \ - t* TORRENT_ALLOCA_tmp = static_cast(alloca(sizeof(t) * TORRENT_ALLOCA_size)); \ + auto TORRENT_ALLOCA_size = ::libtorrent::aux::numeric_cast(n); \ + auto* TORRENT_ALLOCA_tmp = static_cast(alloca(sizeof(t) * TORRENT_ALLOCA_size)); \ v = ::libtorrent::aux::typed_span(TORRENT_ALLOCA_tmp, TORRENT_ALLOCA_size); \ ::libtorrent::aux::uninitialized_default_construct(v.begin(), v.end()); \ } \ @@ -99,8 +99,8 @@ struct alloca_destructor #include #define TORRENT_ALLOCA(v, t, n) ::libtorrent::aux::typed_span v; { \ - std::size_t TORRENT_ALLOCA_size = ::libtorrent::aux::numeric_cast(n); \ - t* TORRENT_ALLOCA_tmp = static_cast(alloca(sizeof(t) * TORRENT_ALLOCA_size)); \ + auto TORRENT_ALLOCA_size = ::libtorrent::aux::numeric_cast(n); \ + auto* TORRENT_ALLOCA_tmp = static_cast(alloca(sizeof(t) * TORRENT_ALLOCA_size)); \ v = ::libtorrent::aux::typed_span(TORRENT_ALLOCA_tmp, TORRENT_ALLOCA_size); \ ::libtorrent::aux::uninitialized_default_construct(v.begin(), v.end()); \ } \ diff --git a/include/libtorrent/aux_/disk_job_fence.hpp b/include/libtorrent/aux_/disk_job_fence.hpp index 0a6211280..365c26364 100644 --- a/include/libtorrent/aux_/disk_job_fence.hpp +++ b/include/libtorrent/aux_/disk_job_fence.hpp @@ -55,12 +55,15 @@ namespace aux { // accrued while the fence was up struct TORRENT_EXPORT disk_job_fence { - disk_job_fence(); + disk_job_fence() = default; + +#if TORRENT_USE_ASSERTS ~disk_job_fence() { TORRENT_ASSERT(int(m_outstanding_jobs) == 0); TORRENT_ASSERT(m_blocked_jobs.size() == 0); } +#endif // returns one of the fence_* enums. // if there are no outstanding jobs on the diff --git a/include/libtorrent/aux_/file_progress.hpp b/include/libtorrent/aux_/file_progress.hpp index f8b32ee4a..6ec8bb6a1 100644 --- a/include/libtorrent/aux_/file_progress.hpp +++ b/include/libtorrent/aux_/file_progress.hpp @@ -61,7 +61,7 @@ namespace aux { struct TORRENT_EXTRA_EXPORT file_progress { - file_progress(); + file_progress() = default; void init(piece_picker const& picker , file_storage const& fs); diff --git a/include/libtorrent/broadcast_socket.hpp b/include/libtorrent/broadcast_socket.hpp index bdf141b6e..91201379d 100644 --- a/include/libtorrent/broadcast_socket.hpp +++ b/include/libtorrent/broadcast_socket.hpp @@ -57,7 +57,7 @@ namespace libtorrent { address ensure_v6(address const& a); typedef std::function receive_handler_t; + , char const* buffer, int size)> receive_handler_t; class TORRENT_EXTRA_EXPORT broadcast_socket { @@ -65,7 +65,7 @@ namespace libtorrent { explicit broadcast_socket(udp::endpoint const& multicast_endpoint); ~broadcast_socket() { close(); } - void open(receive_handler_t const& handler, io_service& ios + void open(receive_handler_t handler, io_service& ios , error_code& ec, bool loopback = true); enum flags_t { flag_broadcast = 1 }; diff --git a/include/libtorrent/i2p_stream.hpp b/include/libtorrent/i2p_stream.hpp index 6ce0a1e4f..29777aa1e 100644 --- a/include/libtorrent/i2p_stream.hpp +++ b/include/libtorrent/i2p_stream.hpp @@ -84,7 +84,9 @@ class i2p_stream : public proxy_base public: explicit i2p_stream(io_service& io_service); +#if TORRENT_USE_ASSERTS ~i2p_stream(); +#endif enum command_t { diff --git a/include/libtorrent/kademlia/dht_storage.hpp b/include/libtorrent/kademlia/dht_storage.hpp index e2f1f6156..8b4fa4403 100644 --- a/include/libtorrent/kademlia/dht_storage.hpp +++ b/include/libtorrent/kademlia/dht_storage.hpp @@ -54,10 +54,10 @@ namespace libtorrent { namespace dht { // This structure hold the relevant counters for the storage struct TORRENT_EXPORT dht_storage_counters { - std::int32_t torrents; - std::int32_t peers; - std::int32_t immutable_data; - std::int32_t mutable_data; + std::int32_t torrents = 0; + std::int32_t peers = 0; + std::int32_t immutable_data = 0; + std::int32_t mutable_data = 0; // This member function set the counters to zero. void reset(); diff --git a/include/libtorrent/timestamp_history.hpp b/include/libtorrent/timestamp_history.hpp index ff784b1aa..4b92fdcfa 100644 --- a/include/libtorrent/timestamp_history.hpp +++ b/include/libtorrent/timestamp_history.hpp @@ -34,6 +34,7 @@ POSSIBILITY OF SUCH DAMAGE. #define TIMESTAMP_HISTORY_HPP #include +#include #include "libtorrent/config.hpp" #include "libtorrent/assert.hpp" @@ -46,7 +47,7 @@ struct TORRENT_EXTRA_EXPORT timestamp_history { static constexpr int history_size = 20; - timestamp_history() : m_base(0), m_index(0), m_num_samples(not_initialized) {} + timestamp_history() = default; bool initialized() const { return m_num_samples != not_initialized; } // add a sample to the timestamp history. If step is true, it's been @@ -58,15 +59,15 @@ struct TORRENT_EXTRA_EXPORT timestamp_history private: // this is a circular buffer - std::uint32_t m_history[history_size]; + std::array m_history; // this is the lowest sample seen in the // last 'history_size' minutes - std::uint32_t m_base; + std::uint32_t m_base = 0; // and this is the index we're currently at // in the circular buffer - std::uint16_t m_index; + std::uint16_t m_index = 0; static constexpr std::uint16_t not_initialized = 0xffff; @@ -76,7 +77,7 @@ private: // if this is set to 'not_initialized' we // have bit seen any samples at all yet // and m_base is not initialized yet - std::uint16_t m_num_samples; + std::uint16_t m_num_samples = not_initialized; }; } diff --git a/include/libtorrent/upnp.hpp b/include/libtorrent/upnp.hpp index 04fa47244..8a5188820 100644 --- a/include/libtorrent/upnp.hpp +++ b/include/libtorrent/upnp.hpp @@ -203,7 +203,7 @@ private: void discover_device_impl(); void resend_request(error_code const& e); - void on_reply(udp::endpoint const& from, char* buffer + void on_reply(udp::endpoint const& from, char const* buffer , std::size_t bytes_transferred); struct rootdevice; diff --git a/src/add_torrent_params.cpp b/src/add_torrent_params.cpp index 8e4872ce9..e213ab01e 100644 --- a/src/add_torrent_params.cpp +++ b/src/add_torrent_params.cpp @@ -35,7 +35,7 @@ POSSIBILITY OF SUCH DAMAGE. namespace libtorrent { add_torrent_params::add_torrent_params(storage_constructor_type sc) - : storage(storage_constructor_type(sc)) {} + : storage(std::move(sc)) {} add_torrent_params::add_torrent_params(add_torrent_params&&) noexcept = default; add_torrent_params& add_torrent_params::operator=(add_torrent_params&&) = default; add_torrent_params::add_torrent_params(add_torrent_params const&) = default; diff --git a/src/alert.cpp b/src/alert.cpp index 0825c7f19..9782c1769 100644 --- a/src/alert.cpp +++ b/src/alert.cpp @@ -2336,7 +2336,7 @@ namespace { #endif } - return nodes_slot(v4_num_nodes, v4_nodes_idx, v6_num_nodes, v6_nodes_idx); + return nodes_slot{v4_num_nodes, v4_nodes_idx, v6_num_nodes, v6_nodes_idx}; } std::vector> read_nodes( diff --git a/src/bdecode.cpp b/src/bdecode.cpp index 301f22acd..7fcb51e44 100644 --- a/src/bdecode.cpp +++ b/src/bdecode.cpp @@ -162,7 +162,7 @@ namespace { std::string message(int ev) const override; boost::system::error_condition default_error_condition( int ev) const BOOST_SYSTEM_NOEXCEPT override - { return boost::system::error_condition(ev, *this); } + { return {ev, *this}; } }; const char* bdecode_error_category::name() const BOOST_SYSTEM_NOEXCEPT @@ -198,7 +198,7 @@ namespace { { boost::system::error_code make_error_code(error_code_enum e) { - return boost::system::error_code(e, bdecode_category()); + return {e, bdecode_category()}; } } diff --git a/src/block_cache.cpp b/src/block_cache.cpp index 4b7eeb4ee..67ac22fd6 100644 --- a/src/block_cache.cpp +++ b/src/block_cache.cpp @@ -1165,7 +1165,7 @@ void block_cache::clear(tailqueue& jobs) for (auto const& p : m_pieces) { - cached_piece_entry& pe = const_cast(p); + auto& pe = const_cast(p); #if TORRENT_USE_ASSERTS for (tailqueue_iterator i = pe.jobs.iterate(); i.get(); i.next()) TORRENT_PIECE_ASSERT((static_cast(i.get()))->piece == pe.piece, &pe); @@ -1182,13 +1182,12 @@ void block_cache::clear(tailqueue& jobs) if (!bufs.empty()) free_multiple_buffers(bufs); // clear lru lists - for (int i = 0; i < cached_piece_entry::num_lrus; ++i) - m_lru[i].get_all(); + for (auto& l : m_lru) l.get_all(); // it's not ok to erase pieces with a refcount > 0 // since we're cancelling all jobs though, it shouldn't be too bad // to let the jobs already running complete. - for (cache_t::iterator i = m_pieces.begin(); i != m_pieces.end();) + for (auto i = m_pieces.begin(); i != m_pieces.end();) { if (i->refcount == 0 && i->piece_refcount == 0) { diff --git a/src/broadcast_socket.cpp b/src/broadcast_socket.cpp index cfdff6746..541663a68 100644 --- a/src/broadcast_socket.cpp +++ b/src/broadcast_socket.cpp @@ -181,10 +181,10 @@ namespace libtorrent { TORRENT_ASSERT(m_multicast_endpoint.address().is_multicast()); } - void broadcast_socket::open(receive_handler_t const& handler + void broadcast_socket::open(receive_handler_t handler , io_service& ios, error_code& ec, bool loopback) { - m_on_receive = handler; + m_on_receive = std::move(handler); std::vector interfaces = enum_net_interfaces(ios, ec); diff --git a/src/crc32c.cpp b/src/crc32c.cpp index 9ce553290..c821a7345 100644 --- a/src/crc32c.cpp +++ b/src/crc32c.cpp @@ -142,7 +142,7 @@ namespace libtorrent { #endif boost::crc_optimal<32, 0x1EDC6F41, 0xFFFFFFFF, 0xFFFFFFFF, true, true> crc; - crc.process_bytes(buf, std::size_t(num_words * 8)); + crc.process_bytes(buf, std::size_t(num_words) * 8); return crc.checksum(); } } diff --git a/src/create_torrent.cpp b/src/create_torrent.cpp index ef0fed1be..63a68e05d 100644 --- a/src/create_torrent.cpp +++ b/src/create_torrent.cpp @@ -285,7 +285,8 @@ namespace { info_hash }; - storage_holder storage = disk_thread.new_torrent(default_storage_constructor, std::move(params), std::shared_ptr()); + storage_holder storage = disk_thread.new_torrent(default_storage_constructor + , params, std::shared_ptr()); settings_pack sett; sett.set_int(settings_pack::cache_size, 0); @@ -582,7 +583,7 @@ namespace { for (file_index_t i(0); i != m_files.end_file(); ++i) { - files.list().push_back(entry()); + files.list().emplace_back(); entry& file_e = files.list().back(); if (m_include_mtime && m_files.mtime(i)) file_e["mtime"] = m_files.mtime(i); file_e["length"] = m_files.file_size(i); @@ -633,7 +634,7 @@ namespace { int const num_nodes = merkle_num_nodes(num_leafs); int const first_leaf = num_nodes - num_leafs; m_merkle_tree.resize(num_nodes); - int const num_pieces = int(m_piece_hash.size()); + auto const num_pieces = int(m_piece_hash.size()); for (int i = 0; i < num_pieces; ++i) m_merkle_tree[first_leaf + i] = m_piece_hash[piece_index_t(i)]; for (int i = num_pieces; i < num_leafs; ++i) diff --git a/src/disk_io_thread_pool.cpp b/src/disk_io_thread_pool.cpp index 83604e048..98b017e13 100644 --- a/src/disk_io_thread_pool.cpp +++ b/src/disk_io_thread_pool.cpp @@ -132,7 +132,7 @@ namespace libtorrent { std::thread::id disk_io_thread_pool::first_thread_id() { std::lock_guard l(m_mutex); - if (m_threads.empty()) return std::thread::id(); + if (m_threads.empty()) return {}; return m_threads.front().get_id(); } @@ -185,7 +185,7 @@ namespace libtorrent { if (ec) return; std::lock_guard l(m_mutex); if (m_abort) return; - if (m_threads.size() == 0) return; + if (m_threads.empty()) return; m_idle_timer.expires_from_now(reap_idle_threads_interval); m_idle_timer.async_wait([this](error_code const& e) { reap_idle_threads(e); }); int const min_idle = m_min_idle_threads.exchange(m_num_idle_threads); diff --git a/src/disk_job_fence.cpp b/src/disk_job_fence.cpp index ddfe21efd..21927779e 100644 --- a/src/disk_job_fence.cpp +++ b/src/disk_job_fence.cpp @@ -45,8 +45,6 @@ POSSIBILITY OF SUCH DAMAGE. namespace libtorrent { namespace aux { - disk_job_fence::disk_job_fence() {} - int disk_job_fence::job_complete(disk_io_job* j, tailqueue& jobs) { std::lock_guard l(m_mutex); @@ -69,7 +67,7 @@ namespace libtorrent { namespace aux { // while this fence was up. However, if there's another fence // in the queue, stop there and raise the fence again int ret = 0; - while (m_blocked_jobs.size()) + while (!m_blocked_jobs.empty()) { disk_io_job *bj = m_blocked_jobs.pop_front(); if (bj->flags & disk_io_job::fence) diff --git a/src/enum_net.cpp b/src/enum_net.cpp index 2e3b8c67a..1ece38b96 100644 --- a/src/enum_net.cpp +++ b/src/enum_net.cpp @@ -385,7 +385,7 @@ int _System __libsocket_sysctl(int* mib, u_int namelen, void *oldp, size_t *oldl bool parse_route(int, rt_msghdr* rtm, ip_route* rt_info) { sockaddr* rti_info[RTAX_MAX]; - sockaddr* sa = reinterpret_cast(rtm + 1); + auto* sa = reinterpret_cast(rtm + 1); for (int i = 0; i < RTAX_MAX; ++i) { if ((rtm->rtm_addrs & (1 << i)) == 0) @@ -957,7 +957,7 @@ int _System __libsocket_sysctl(int* mib, u_int namelen, void *oldp, size_t *oldl } std::unique_ptr buf(new (std::nothrow) char[needed]); - if (buf.get() == nullptr) + if (buf == nullptr) { ec = boost::asio::error::no_memory; return std::vector(); diff --git a/src/ffs.cpp b/src/ffs.cpp index 970babf11..2cf6c764a 100644 --- a/src/ffs.cpp +++ b/src/ffs.cpp @@ -46,7 +46,7 @@ namespace libtorrent { namespace aux { int count_leading_zeros_sw(span buf) { - int const num = int(buf.size()); + auto const num = int(buf.size()); std::uint32_t const* ptr = buf.data(); TORRENT_ASSERT(num >= 0); @@ -79,7 +79,7 @@ namespace libtorrent { namespace aux { int count_leading_zeros_hw(span buf) { - int const num = int(buf.size()); + auto const num = int(buf.size()); std::uint32_t const* ptr = buf.data(); TORRENT_ASSERT(num >= 0); @@ -116,7 +116,7 @@ namespace libtorrent { namespace aux { int count_trailing_ones_sw(span buf) { - int const num = int(buf.size()); + auto const num = int(buf.size()); std::uint32_t const* ptr = buf.data(); TORRENT_ASSERT(num >= 0); @@ -139,7 +139,7 @@ namespace libtorrent { namespace aux { int count_trailing_ones_hw(span buf) { - int const num = int(buf.size()); + auto const num = int(buf.size()); std::uint32_t const* ptr = buf.data(); TORRENT_ASSERT(num >= 0); diff --git a/src/file.cpp b/src/file.cpp index 9b54e6f05..854421c49 100644 --- a/src/file.cpp +++ b/src/file.cpp @@ -784,8 +784,8 @@ typedef struct _FILE_ALLOCATED_RANGE_BUFFER { bool coalesce_read_buffers(span& bufs , iovec_t& tmp) { - std::size_t const buf_size = aux::numeric_cast(bufs_size(bufs)); - char* buf = new char[buf_size]; + auto const buf_size = aux::numeric_cast(bufs_size(bufs)); + auto buf = new char[buf_size]; tmp = { buf, buf_size }; bufs = span(tmp); return true; @@ -801,8 +801,8 @@ typedef struct _FILE_ALLOCATED_RANGE_BUFFER { bool coalesce_write_buffers(span& bufs , iovec_t& tmp) { - std::size_t const buf_size = aux::numeric_cast(bufs_size(bufs)); - char* buf = new char[buf_size]; + auto const buf_size = aux::numeric_cast(bufs_size(bufs)); + auto buf = new char[buf_size]; gather_copy(bufs, buf); tmp = { buf, buf_size }; bufs = span(tmp); @@ -1162,7 +1162,7 @@ namespace { } } #else // NON-WINDOWS - struct stat st; + struct stat st{}; if (::fstat(native_handle(), &st) != 0) { ec.assign(errno, system_category()); @@ -1257,7 +1257,7 @@ namespace { } return file_size.QuadPart; #else - struct stat fs; + struct stat fs = {}; if (::fstat(native_handle(), &fs) != 0) { ec.assign(errno, system_category()); diff --git a/src/file_progress.cpp b/src/file_progress.cpp index e33c8074b..7fe6f1aae 100644 --- a/src/file_progress.cpp +++ b/src/file_progress.cpp @@ -39,10 +39,6 @@ POSSIBILITY OF SUCH DAMAGE. namespace libtorrent { namespace aux { - file_progress::file_progress() - { - } - void file_progress::init(piece_picker const& picker, file_storage const& fs) { INVARIANT_CHECK; diff --git a/src/file_storage.cpp b/src/file_storage.cpp index 48144216c..a23cdf937 100644 --- a/src/file_storage.cpp +++ b/src/file_storage.cpp @@ -483,7 +483,7 @@ namespace { TORRENT_ASSERT(file_iter != m_files.end()); if (file_offset < std::int64_t(file_iter->size)) { - file_slice f; + file_slice f{}; f.file_index = file_index_t(int(file_iter - m_files.begin())); f.offset = file_offset; f.size = std::min(std::int64_t(file_iter->size) - file_offset, std::int64_t(size)); @@ -530,7 +530,7 @@ namespace { TORRENT_ASSERT_PRECOND(file_index < end_file()); TORRENT_ASSERT(m_num_pieces >= 0); - peer_request ret; + peer_request ret{}; if (file_index >= end_file()) { ret.piece = piece_index_t{m_num_pieces}; diff --git a/src/gzip.cpp b/src/gzip.cpp index 29e45d1f5..955a9c54f 100644 --- a/src/gzip.cpp +++ b/src/gzip.cpp @@ -60,7 +60,7 @@ namespace libtorrent { const char* name() const BOOST_SYSTEM_NOEXCEPT override; std::string message(int ev) const override; boost::system::error_condition default_error_condition(int ev) const BOOST_SYSTEM_NOEXCEPT override - { return boost::system::error_condition(ev, *this); } + { return {ev, *this}; } }; const char* gzip_error_category::name() const BOOST_SYSTEM_NOEXCEPT @@ -104,7 +104,7 @@ namespace libtorrent { { boost::system::error_code make_error_code(error_code_enum e) { - return boost::system::error_code(e, gzip_category()); + return {e, gzip_category()}; } } @@ -142,7 +142,7 @@ namespace { { if (buffer.size() < 2) return -1; - std::size_t const extra_len = static_cast((buffer[1] << 8) | buffer[0]); + auto const extra_len = static_cast((buffer[1] << 8) | buffer[0]); if (buffer.size() < extra_len + 2) return -1; buffer = buffer.subspan(extra_len + 2); } diff --git a/src/hasher512.cpp b/src/hasher512.cpp index a4a22de1b..b5e80f4b0 100644 --- a/src/hasher512.cpp +++ b/src/hasher512.cpp @@ -130,12 +130,14 @@ namespace libtorrent { #endif } +#if defined TORRENT_USE_LIBGCRYPT hasher512::~hasher512() { -#if defined TORRENT_USE_LIBGCRYPT gcry_md_close(m_context); -#endif } +#else + hasher512::~hasher512() = default; +#endif #ifdef TORRENT_MACOS_DEPRECATED_LIBCRYPTO #pragma clang diagnostic pop diff --git a/src/http_seed_connection.cpp b/src/http_seed_connection.cpp index 3b037d680..b7d54fbff 100644 --- a/src/http_seed_connection.cpp +++ b/src/http_seed_connection.cpp @@ -98,8 +98,7 @@ namespace libtorrent { piece_block_progress http_seed_connection::downloading_piece_progress() const { - if (m_requests.empty()) - return piece_block_progress(); + if (m_requests.empty()) return {}; std::shared_ptr t = associated_torrent().lock(); TORRENT_ASSERT(t); diff --git a/src/http_tracker_connection.cpp b/src/http_tracker_connection.cpp index 8fcf2247f..e16ed5fa2 100644 --- a/src/http_tracker_connection.cpp +++ b/src/http_tracker_connection.cpp @@ -65,7 +65,7 @@ namespace libtorrent { , tracker_manager& man , tracker_request const& req , std::weak_ptr c) - : tracker_connection(man, req, ios, c) + : tracker_connection(man, req, ios, std::move(c)) {} void http_tracker_connection::start() diff --git a/src/i2p_stream.cpp b/src/i2p_stream.cpp index 7bcd26d79..549f65c72 100644 --- a/src/i2p_stream.cpp +++ b/src/i2p_stream.cpp @@ -75,7 +75,7 @@ namespace libtorrent { } boost::system::error_condition default_error_condition( int ev) const BOOST_SYSTEM_NOEXCEPT override - { return boost::system::error_condition(ev, *this); } + { return {ev, *this}; } }; @@ -89,7 +89,7 @@ namespace libtorrent { { boost::system::error_code make_error_code(i2p_error_code e) { - return error_code(e, i2p_category()); + return {e, i2p_category()}; } } @@ -180,8 +180,8 @@ namespace libtorrent { if (m_state == sam_idle && m_name_lookup.empty() && is_open()) do_name_lookup(name, std::move(handler)); else - m_name_lookup.push_back(std::make_pair(std::string(name) - , std::move(handler))); + m_name_lookup.emplace_back(std::string(name) + , std::move(handler)); } void i2p_connection::do_name_lookup(std::string const& name @@ -203,7 +203,7 @@ namespace libtorrent { if (!m_name_lookup.empty()) { std::pair& nl = m_name_lookup.front(); - do_name_lookup(std::move(nl.first), std::move(nl.second)); + do_name_lookup(nl.first, std::move(nl.second)); m_name_lookup.pop_front(); } @@ -227,13 +227,13 @@ namespace libtorrent { #endif } +#if TORRENT_USE_ASSERTS i2p_stream::~i2p_stream() { -#if TORRENT_USE_ASSERTS TORRENT_ASSERT(m_magic == 0x1337); m_magic = 0; -#endif } +#endif void i2p_stream::do_connect(error_code const& e, tcp::resolver::iterator i , handler_type h) @@ -285,7 +285,7 @@ namespace libtorrent { COMPLETE_ASYNC("i2p_stream::read_line"); if (handle_error(e, h)) return; - int const read_pos = int(m_buffer.size()); + auto const read_pos = int(m_buffer.size()); // look for \n which means end of the response if (m_buffer[read_pos - 1] != '\n') diff --git a/src/identify_client.cpp b/src/identify_client.cpp index 34249961c..fb7031386 100644 --- a/src/identify_client.cpp +++ b/src/identify_client.cpp @@ -383,11 +383,8 @@ namespace aux { // non standard encodings // ---------------------- - const int num_generic_mappings = sizeof(generic_mappings) / sizeof(generic_mappings[0]); - - for (int i = 0; i < num_generic_mappings; ++i) + for (auto const& e : generic_mappings) { - generic_map_entry const& e = generic_mappings[i]; if (find_string(PID + e.offset, e.id)) return e.name; } @@ -396,8 +393,8 @@ namespace aux { if (find_string(PID, "eX")) { - std::string user(PID + 2, PID + 14); - return std::string("eXeem ('") + user.c_str() + "')"; + std::string user(PID + 2, 12); + return std::string("eXeem ('") + user + "')"; } bool const is_equ_zero = std::equal(PID, PID + 12, "\0\0\0\0\0\0\0\0\0\0\0\0"); @@ -424,10 +421,8 @@ namespace aux { return "Generic"; std::string unknown("Unknown ["); - for (peer_id::const_iterator i = p.begin(); i != p.end(); ++i) - { - unknown += is_print(char(*i)) ? char(*i) : '.'; - } + for (unsigned char const c : p) + unknown += is_print(char(c)) ? char(c) : '.'; unknown += "]"; return unknown; } diff --git a/src/ip_notifier.cpp b/src/ip_notifier.cpp index 2e103c38b..bcf4995db 100644 --- a/src/ip_notifier.cpp +++ b/src/ip_notifier.cpp @@ -123,10 +123,20 @@ template , void (*Release)(T) = CFRefRelease> struct CFRef { - CFRef() {} + CFRef() = default; explicit CFRef(T h) : m_h(h) {} // take ownership ~CFRef() { release(); } + CFRef(CFRef&& rhs) : m_h(rhs.m_h) { rhs.m_h = nullptr; } + CFRef& operator=(CFRef&& rhs) + { + if (m_h == rhs.m_h) return *this; + release(); + m_h = rhs.m_h; + rhs.m_h = nullptr; + return *this; + } + CFRef(CFRef const& rhs) : m_h(rhs.m_h) { retain(); } CFRef& operator=(CFRef const& rhs) { diff --git a/src/ip_voter.cpp b/src/ip_voter.cpp index cffe35411..f78ea35bf 100644 --- a/src/ip_voter.cpp +++ b/src/ip_voter.cpp @@ -114,7 +114,7 @@ namespace libtorrent { sha1_hash const k = hash_address(source); // do we already have an entry for this external IP? - std::vector::iterator i = std::find_if(m_external_addresses.begin() + auto i = std::find_if(m_external_addresses.begin() , m_external_addresses.end(), [&ip] (external_ip_t const& e) { return e.addr == ip; }); if (i == m_external_addresses.end()) @@ -137,7 +137,7 @@ namespace libtorrent { // ones with the fewest votes m_external_addresses.erase(m_external_addresses.end() - 1); } - m_external_addresses.push_back(external_ip_t()); + m_external_addresses.emplace_back(); i = m_external_addresses.end() - 1; i->addr = ip; } diff --git a/src/kademlia/dht_state.cpp b/src/kademlia/dht_state.cpp index 3b868d32a..1cca95f82 100644 --- a/src/kademlia/dht_state.cpp +++ b/src/kademlia/dht_state.cpp @@ -42,13 +42,13 @@ namespace libtorrent { namespace dht { if (e.type() != bdecode_node::dict_t) return node_ids_t(); node_ids_t ret; // first look for an old-style nid - auto old_nid = e.dict_find_string_value(key); + auto const old_nid = e.dict_find_string_value(key); if (old_nid.size() == 20) { ret.emplace_back(address(), node_id(old_nid)); return ret; } - auto nids = e.dict_find_list(key); + auto const nids = e.dict_find_list(key); if (!nids) return ret; for (int i = 0; i < nids.list_size(); i++) { @@ -83,7 +83,7 @@ namespace { std::string node; std::back_insert_iterator out(node); detail::write_endpoint(ep, out); - list.push_back(entry(node)); + list.emplace_back(node); } return ret; } diff --git a/src/kademlia/dht_storage.cpp b/src/kademlia/dht_storage.cpp index b3c801e9f..4a9820fb7 100644 --- a/src/kademlia/dht_storage.cpp +++ b/src/kademlia/dht_storage.cpp @@ -58,7 +58,7 @@ namespace { { time_point added; tcp::endpoint addr; - bool seed; + bool seed = 0; }; // internal @@ -100,9 +100,9 @@ namespace { struct dht_mutable_item : dht_immutable_item { - signature sig; - sequence_number seq; - public_key key; + signature sig{}; + sequence_number seq{}; + public_key key{}; std::string salt; }; @@ -156,7 +156,7 @@ namespace { private: // explicitly disallow assignment, to silence msvc warning - immutable_item_comparator& operator=(immutable_item_comparator const&); + immutable_item_comparator& operator=(immutable_item_comparator const&) = delete; std::vector const& m_node_ids; }; @@ -270,7 +270,7 @@ namespace { if (random(std::uint32_t(candidates--)) > std::uint32_t(to_pick)) continue; - pe.push_back(entry()); + pe.emplace_back(); std::string& str = pe.back().string(); str.resize(18); diff --git a/src/kademlia/dht_tracker.cpp b/src/kademlia/dht_tracker.cpp index 20dea7cae..b3417124e 100644 --- a/src/kademlia/dht_tracker.cpp +++ b/src/kademlia/dht_tracker.cpp @@ -57,7 +57,7 @@ namespace libtorrent { namespace dht { namespace { // generate a new write token key every 5 minutes - time_duration const key_refresh + auto const key_refresh = duration_cast(minutes(5)); void add_dht_counters(node const& dht, counters& c) @@ -360,7 +360,8 @@ namespace libtorrent { namespace dht { // these functions provide a slightly higher level // interface to the get/put functionality in the DHT - void get_immutable_item_callback(item const& it, std::shared_ptr ctx + void get_immutable_item_callback(item const& it + , std::shared_ptr ctx , std::function f) { // the reason to wrap here is to control the return value diff --git a/src/kademlia/dos_blocker.cpp b/src/kademlia/dos_blocker.cpp index 330c0e88c..48592e1ce 100644 --- a/src/kademlia/dos_blocker.cpp +++ b/src/kademlia/dos_blocker.cpp @@ -43,10 +43,10 @@ namespace libtorrent { namespace dht { : m_message_rate_limit(5) , m_block_timeout(5 * 60) { - for (int i = 0; i < num_ban_nodes; ++i) + for (auto& e : m_ban_nodes) { - m_ban_nodes[i].count = 0; - m_ban_nodes[i].limit = min_time(); + e.count = 0; + e.limit = min_time(); } } diff --git a/src/kademlia/get_item.cpp b/src/kademlia/get_item.cpp index 97541c2ff..7a1154c0f 100644 --- a/src/kademlia/get_item.cpp +++ b/src/kademlia/get_item.cpp @@ -171,8 +171,8 @@ void get_item::done() void get_item_observer::reply(msg const& m) { - public_key pk; - signature sig; + public_key pk{}; + signature sig{}; sequence_number seq{0}; bdecode_node const r = m.message.dict_find_dict("r"); diff --git a/src/kademlia/node.cpp b/src/kademlia/node.cpp index 67e0a96ec..41003e3c6 100644 --- a/src/kademlia/node.cpp +++ b/src/kademlia/node.cpp @@ -115,7 +115,7 @@ node::node(aux::listen_socket_handle const& sock, socket_manager* sock_man , m_rpc(m_id, m_settings, m_table, sock, sock_man, observer) , m_sock(sock) , m_sock_man(sock_man) - , m_get_foreign_node(get_foreign_node) + , m_get_foreign_node(std::move(get_foreign_node)) , m_observer(observer) , m_protocol(map_protocol_to_descriptor(sock.get_local_endpoint().protocol() == tcp::v4() ? udp::v4() : udp::v6())) , m_last_tracker_tick(aux::time_now()) @@ -286,7 +286,7 @@ void node::incoming(aux::listen_socket_handle const& s, msg const& m) if (ext_ip && ext_ip.string_length() >= 16) { // this node claims we use the wrong node-ID! - address_v6::bytes_type b; + address_v6::bytes_type b{}; std::memcpy(&b[0], ext_ip.string_ptr(), 16); if (m_observer != nullptr) m_observer->set_external_address(m_sock, address_v6(b) @@ -295,7 +295,7 @@ void node::incoming(aux::listen_socket_handle const& s, msg const& m) #endif if (ext_ip && ext_ip.string_length() >= 4) { - address_v4::bytes_type b; + address_v4::bytes_type b{}; std::memcpy(&b[0], ext_ip.string_ptr(), 4); if (m_observer != nullptr) m_observer->set_external_address(m_sock, address_v4(b) @@ -507,15 +507,15 @@ void node::get_item(public_key const& pk, std::string const& salt namespace { void put(std::vector> const& nodes - , std::shared_ptr ta) + , std::shared_ptr const& ta) { ta->set_targets(nodes); ta->start(); } void put_data_cb(item i, bool auth - , std::shared_ptr ta - , std::function f) + , std::shared_ptr const& ta + , std::function const& f) { // call data_callback only when we got authoritative data. if (auth) @@ -724,7 +724,7 @@ void node::status(std::vector& table for (auto const& r : m_running_requests) { - requests.push_back(dht_lookup()); + requests.emplace_back(); dht_lookup& lookup = requests.back(); r->status(lookup); } @@ -745,12 +745,11 @@ void node::status(session_status& s) m_table.status(s); s.dht_total_allocations += m_rpc.num_allocated_observers(); - for (std::set::iterator i = m_running_requests.begin() - , end(m_running_requests.end()); i != end; ++i) + for (auto& r : m_running_requests) { - s.active_requests.push_back(dht_lookup()); + s.active_requests.emplace_back(); dht_lookup& lookup = s.active_requests.back(); - (*i)->status(lookup); + r->status(lookup); } } #endif @@ -912,7 +911,7 @@ void node::incoming_request(msg const& m, entry& e) return; } - int port = int(msg_keys[1].int_value()); + auto port = int(msg_keys[1].int_value()); // is the announcer asking to ignore the explicit // listen port and instead use the source port of the packet? diff --git a/src/kademlia/node_id.cpp b/src/kademlia/node_id.cpp index a7785002c..11c9a6465 100644 --- a/src/kademlia/node_id.cpp +++ b/src/kademlia/node_id.cpp @@ -91,9 +91,9 @@ node_id generate_id_impl(address const& ip_, std::uint32_t r) std::uint8_t const* mask = nullptr; int num_octets = 0; - address_v4::bytes_type b4; + address_v4::bytes_type b4{}; #if TORRENT_USE_IPV6 - address_v6::bytes_type b6; + address_v6::bytes_type b6{}; if (ip_.is_v6()) { b6 = ip_.to_v6().to_bytes(); diff --git a/src/kademlia/put_data.cpp b/src/kademlia/put_data.cpp index 10ae3b63f..0ed8c8535 100644 --- a/src/kademlia/put_data.cpp +++ b/src/kademlia/put_data.cpp @@ -87,7 +87,7 @@ bool put_data::invoke(observer_ptr o) // TODO: what if o is not an instance of put_data_observer? This need to be // redesigned for better type safety. - put_data_observer* po = static_cast(o.get()); + auto* po = static_cast(o.get()); entry e; e["y"] = "q"; diff --git a/src/kademlia/routing_table.cpp b/src/kademlia/routing_table.cpp index 9690e000b..8804b6e33 100644 --- a/src/kademlia/routing_table.cpp +++ b/src/kademlia/routing_table.cpp @@ -246,21 +246,21 @@ node_entry const* routing_table::next_refresh() // this will have a bias towards pinging nodes close to us first. for (auto i = m_buckets.rbegin(), end(m_buckets.rend()); i != end; ++i) { - for (auto j = i->live_nodes.begin(), end2(i->live_nodes.end()); j != end2; ++j) + for (auto& n : i->live_nodes) { // this shouldn't happen - TORRENT_ASSERT(m_id != j->id); - if (j->id == m_id) continue; + TORRENT_ASSERT(m_id != n.id); + if (n.id == m_id) continue; - if (j->last_queried == min_time()) + if (n.last_queried == min_time()) { - candidate = &*j; + candidate = &n; goto out; } - if (candidate == nullptr || j->last_queried < candidate->last_queried) + if (candidate == nullptr || n.last_queried < candidate->last_queried) { - candidate = &*j; + candidate = &n; } } } @@ -329,19 +329,16 @@ bool compare_ip_cidr(address const& lhs, address const& rhs) node_entry* routing_table::find_node(udp::endpoint const& ep , routing_table::table_t::iterator* bucket) { - for (table_t::iterator i = m_buckets.begin() - , end(m_buckets.end()); i != end; ++i) + for (auto i = m_buckets.begin() , end(m_buckets.end()); i != end; ++i) { - for (bucket_t::iterator j = i->replacements.begin(); - j != i->replacements.end(); ++j) + for (auto j = i->replacements.begin(); j != i->replacements.end(); ++j) { if (j->addr() != ep.address()) continue; if (j->port() != ep.port()) continue; *bucket = i; return &*j; } - for (bucket_t::iterator j = i->live_nodes.begin(); - j != i->live_nodes.end(); ++j) + for (auto j = i->live_nodes.begin(); j != i->live_nodes.end(); ++j) { if (j->addr() != ep.address()) continue; if (j->port() != ep.port()) continue; diff --git a/src/lazy_bdecode.cpp b/src/lazy_bdecode.cpp index ca55df7dc..7e8ec8f5a 100644 --- a/src/lazy_bdecode.cpp +++ b/src/lazy_bdecode.cpp @@ -264,7 +264,7 @@ namespace { else if (int(m_size) == this->capacity()) { int const capacity = this->capacity() * lazy_entry_grow_factor / 100; - lazy_dict_entry* tmp = new (std::nothrow) lazy_dict_entry[capacity + 1]; + auto* tmp = new (std::nothrow) lazy_dict_entry[capacity + 1]; if (tmp == nullptr) return nullptr; std::memcpy(tmp, m_data.dict, sizeof(lazy_dict_entry) * (m_size + 1)); for (int i = 0; i < int(m_size); ++i) m_data.dict[i + 1].val.release(); @@ -349,7 +349,7 @@ namespace { pascal_string lazy_entry::dict_find_pstr(char const* name) const { lazy_entry const* e = dict_find(name); - if (e == nullptr || e->type() != lazy_entry::string_t) return pascal_string(nullptr, 0); + if (e == nullptr || e->type() != lazy_entry::string_t) return {nullptr, 0}; return e->string_pstr(); } @@ -459,7 +459,7 @@ namespace { pascal_string lazy_entry::list_pstr_at(int i) const { lazy_entry const* e = list_at(i); - if (e == nullptr || e->type() != lazy_entry::string_t) return pascal_string(nullptr, 0); + if (e == nullptr || e->type() != lazy_entry::string_t) return {nullptr, 0}; return e->string_pstr(); } @@ -489,8 +489,7 @@ namespace { std::pair lazy_entry::data_section() const { - typedef std::pair return_t; - return return_t(m_begin, m_len); + return {m_begin, m_len}; } namespace { diff --git a/src/magnet_uri.cpp b/src/magnet_uri.cpp index d49c02e3a..9908cfdda 100644 --- a/src/magnet_uri.cpp +++ b/src/magnet_uri.cpp @@ -131,7 +131,7 @@ namespace libtorrent { , storage_constructor_type sc , void* userdata) { - add_torrent_params params(sc); + add_torrent_params params(std::move(sc)); params.storage_mode = storage_mode; params.userdata = userdata; params.save_path = save_path; diff --git a/src/part_file.cpp b/src/part_file.cpp index ac565f30a..56c2f052f 100644 --- a/src/part_file.cpp +++ b/src/part_file.cpp @@ -316,7 +316,7 @@ namespace libtorrent { auto bytes_read = std::size_t(m_file.readv(slot_offset + piece_offset, v, ec)); v = v.first(bytes_read); TORRENT_ASSERT(!ec); - if (ec || v.size() == 0) return; + if (ec || v.empty()) return; f(file_offset, {buf.get(), std::size_t(block_to_copy)}); diff --git a/src/path.cpp b/src/path.cpp index d9e8806cb..5d9126b60 100644 --- a/src/path.cpp +++ b/src/path.cpp @@ -347,7 +347,7 @@ namespace libtorrent { // most errors are passed through, except for the ones that indicate that // hard links are not supported and require a copy. // TODO: 2 test this on a FAT volume to see what error we get! - if (errno != EMLINK || errno != EXDEV) + if (errno != EMLINK && errno != EXDEV) { // some error happened, report up to the caller ec.assign(errno, system_category()); diff --git a/src/pe_crypto.cpp b/src/pe_crypto.cpp index 626e4345c..e2193428f 100644 --- a/src/pe_crypto.cpp +++ b/src/pe_crypto.cpp @@ -67,7 +67,7 @@ namespace libtorrent { std::array export_key(key_t const& k) { std::array ret; - std::uint8_t* begin = reinterpret_cast(ret.data()); + auto* begin = reinterpret_cast(ret.data()); std::uint8_t* end = mp::export_bits(k, begin, 8); // TODO: it would be nice to be able to export to a fixed width field, so @@ -161,7 +161,7 @@ namespace libtorrent { int next_barrier = 0; span> out_iovec; - if (bufs.size() != 0) + if (!bufs.empty()) { std::tie(next_barrier, out_iovec) = m_send_barriers.front().enc_handler->encrypt(bufs); @@ -238,9 +238,8 @@ namespace libtorrent { bool place_barrier = false; if (!m_send_barriers.empty()) { - std::list::iterator end = m_send_barriers.end(); --end; - for (std::list::iterator b = m_send_barriers.begin(); - b != end; ++b) + auto const end = std::prev(m_send_barriers.end()); + for (auto b = m_send_barriers.begin(); b != end; ++b) pending_encryption -= b->next; TORRENT_ASSERT(pending_encryption >= 0); m_send_barriers.back().next = pending_encryption; @@ -314,7 +313,7 @@ namespace libtorrent { int bytes_processed = 0; for (auto& buf : bufs) { - unsigned char* const pos = reinterpret_cast(buf.data()); + auto* const pos = reinterpret_cast(buf.data()); int const len = int(buf.size()); TORRENT_ASSERT(len >= 0); @@ -333,7 +332,7 @@ namespace libtorrent { int bytes_processed = 0; for (auto& buf : bufs) { - unsigned char* const pos = reinterpret_cast(buf.data()); + auto* const pos = reinterpret_cast(buf.data()); int const len = int(buf.size()); TORRENT_ASSERT(len >= 0); diff --git a/src/peer_connection.cpp b/src/peer_connection.cpp index 6df16f579..ddf191d7c 100644 --- a/src/peer_connection.cpp +++ b/src/peer_connection.cpp @@ -1277,7 +1277,7 @@ namespace libtorrent { } #if TORRENT_USE_I2P - i2p_stream* i2ps = m_socket->get(); + auto* i2ps = m_socket->get(); if (!i2ps && t->torrent_file().is_i2p() && !m_settings.get_bool(settings_pack::allow_i2p_mixed)) { @@ -4275,7 +4275,7 @@ namespace libtorrent { #if !defined(TORRENT_DISABLE_ENCRYPTION) && !defined(TORRENT_DISABLE_EXTENSIONS) if (type() == connection_type::bittorrent && op != operation_t::connect) { - bt_peer_connection* bt = static_cast(this); + auto* bt = static_cast(this); if (bt->supports_encryption()) m_counters.inc_stats_counter( counters::error_encrypted_peers); if (bt->rc4_encrypted() && bt->supports_encryption()) @@ -5540,7 +5540,7 @@ namespace libtorrent { { // this const_cast is a here because chained_buffer need to be // fixed. - char* ptr = const_cast(i->data()); + auto* ptr = const_cast(i->data()); m_send_buffer.prepend_buffer(span(ptr, i->size()) , static_cast(i->size())); } diff --git a/src/peer_connection_handle.cpp b/src/peer_connection_handle.cpp index d992d13d5..3748509d8 100644 --- a/src/peer_connection_handle.cpp +++ b/src/peer_connection_handle.cpp @@ -51,7 +51,7 @@ void peer_connection_handle::add_extension(std::shared_ptr ext) #ifndef TORRENT_DISABLE_EXTENSIONS std::shared_ptr pc = native_handle(); TORRENT_ASSERT(pc); - pc->add_extension(ext); + pc->add_extension(std::move(ext)); #else TORRENT_UNUSED(ext); #endif @@ -329,7 +329,7 @@ void bt_peer_connection_handle::switch_send_crypto(std::shared_ptr pc = native_handle(); TORRENT_ASSERT(pc); - pc->switch_send_crypto(crypto); + pc->switch_send_crypto(std::move(crypto)); #else TORRENT_UNUSED(crypto); #endif @@ -340,7 +340,7 @@ void bt_peer_connection_handle::switch_recv_crypto(std::shared_ptr pc = native_handle(); TORRENT_ASSERT(pc); - pc->switch_recv_crypto(crypto); + pc->switch_recv_crypto(std::move(crypto)); #else TORRENT_UNUSED(crypto); #endif diff --git a/src/piece_picker.cpp b/src/piece_picker.cpp index 989d4cde7..ff5649262 100644 --- a/src/piece_picker.cpp +++ b/src/piece_picker.cpp @@ -98,8 +98,7 @@ namespace libtorrent { m_reverse_cursor = m_piece_map.end_index(); m_cursor = piece_index_t(0); - for (int i = 0; i < piece_pos::num_download_categories; ++i) - m_downloads[i].clear(); + for (auto& c : m_downloads) c.clear(); m_block_info.clear(); m_free_block_infos.clear(); @@ -108,14 +107,13 @@ namespace libtorrent { m_num_have = 0; m_num_passed = 0; m_dirty = true; - for (std::vector::iterator i = m_piece_map.begin() - , end(m_piece_map.end()); i != end; ++i) + for (auto& m : m_piece_map) { - i->peer_count = 0; - i->download_state = piece_pos::piece_open; - i->index = prio_index_t(0); + m.peer_count = 0; + m.download_state = piece_pos::piece_open; + m.index = prio_index_t(0); #ifdef TORRENT_DEBUG_REFCOUNTS - i->have_peers.clear(); + m.have_peers.clear(); #endif } @@ -143,7 +141,7 @@ namespace libtorrent { int state = m_piece_map[index].download_queue(); if (state != piece_pos::piece_open) { - std::vector::const_iterator piece = find_dl_piece(state, index); + auto piece = find_dl_piece(state, index); TORRENT_ASSERT(piece != m_downloads[state].end()); st = *piece; return; @@ -200,9 +198,8 @@ namespace libtorrent { // always insert into bucket 0 (piece_downloading) downloading_piece ret; ret.index = piece; - int download_state = piece_pos::piece_downloading; - std::vector::iterator downloading_iter - = std::lower_bound(m_downloads[download_state].begin() + int const download_state = piece_pos::piece_downloading; + auto downloading_iter = std::lower_bound(m_downloads[download_state].begin() , m_downloads[download_state].end(), ret); TORRENT_ASSERT(downloading_iter == m_downloads[download_state].end() || downloading_iter->index != piece); @@ -236,7 +233,7 @@ namespace libtorrent { check_piece_state(); #endif - int download_state = m_piece_map[i->index].download_queue(); + int const download_state = m_piece_map[i->index].download_queue(); TORRENT_ASSERT(download_state != piece_pos::piece_open); TORRENT_ASSERT(find_dl_piece(download_state, i->index) == i); #if TORRENT_USE_ASSERTS @@ -265,16 +262,16 @@ namespace libtorrent { #endif std::vector ret; - for (int k = 0; k < piece_pos::num_download_categories; ++k) - ret.insert(ret.end(), m_downloads[k].begin(), m_downloads[k].end()); + for (auto const& c : m_downloads) + ret.insert(ret.end(), c.begin(), c.end()); return ret; } int piece_picker::get_download_queue_size() const { int ret = 0; - for (int k = 0; k < piece_pos::num_download_categories; ++k) - ret += int(m_downloads[k].size()); + for (auto const& c : m_downloads) + ret += int(c.size()); return ret; } @@ -845,7 +842,7 @@ namespace libtorrent { print_pieces(); #endif TORRENT_ASSERT(elem_index < m_pieces.end_index()); - prio_index_t temp; + prio_index_t temp{}; do { temp = --m_priority_boundaries[priority]; @@ -909,7 +906,7 @@ namespace libtorrent { #endif if (priority > new_priority) { - prio_index_t new_index; + prio_index_t new_index{}; piece_index_t temp = index; for (;;) { @@ -946,7 +943,7 @@ namespace libtorrent { } else { - prio_index_t new_index; + prio_index_t new_index{}; piece_index_t temp = index; for (;;) { @@ -1168,11 +1165,8 @@ namespace libtorrent { TORRENT_ASSERT(m_seeds > 0); --m_seeds; - for (std::vector::iterator i = m_piece_map.begin() - , end(m_piece_map.end()); i != end; ++i) - { - ++i->peer_count; - } + for (auto& m : m_piece_map) + ++m.peer_count; m_dirty = true; } @@ -1200,7 +1194,7 @@ namespace libtorrent { break_one_seed(); } - int prev_priority = p.priority(this); + int const prev_priority = p.priority(this); #ifdef TORRENT_DEBUG_REFCOUNTS TORRENT_ASSERT(p.have_peers.count(peer) == 1); @@ -1753,8 +1747,7 @@ namespace libtorrent { if (p.downloading()) { - std::vector::iterator i = find_dl_piece( - p.download_queue(), index); + auto i = find_dl_piece(p.download_queue(), index); if (i != m_downloads[p.download_queue()].end()) update_piece_state(i); } @@ -1916,23 +1909,21 @@ namespace { // now, copy over the pointers. We also apply a filter here to not // include ineligible pieces in certain modes. For instance, a piece // that the current peer doesn't have is not included. - for (std::vector::const_iterator i - = m_downloads[piece_pos::piece_downloading].begin() - , end(m_downloads[piece_pos::piece_downloading].end()); i != end; ++i) + for (auto& dp : m_downloads[piece_pos::piece_downloading]) { pc.inc_stats_counter(counters::piece_picker_partial_loops); // in time critical mode, only pick high priority pieces if ((options & time_critical_mode) - && piece_priority(i->index) != top_priority) + && piece_priority(dp.index) != top_priority) continue; - if (!is_piece_free(i->index, pieces)) continue; + if (!is_piece_free(dp.index, pieces)) continue; - TORRENT_ASSERT(m_piece_map[i->index].download_queue() + TORRENT_ASSERT(m_piece_map[dp.index].download_queue() == piece_pos::piece_downloading); - ordered_partials[num_ordered_partials++] = &*i; + ordered_partials[num_ordered_partials++] = &dp; } // now, sort the list. @@ -2179,7 +2170,7 @@ namespace { { pc.inc_stats_counter(counters::piece_picker_rand_loops); TORRENT_ASSERT(is_piece_free(k, pieces)); - interesting_blocks.push_back(piece_block(k, j)); + interesting_blocks.emplace_back(k, j); --num_blocks; --prefer_contiguous_blocks; if (prefer_contiguous_blocks <= 0 @@ -2328,7 +2319,7 @@ get_out: TORRENT_ASSERT(info.piece_index == dp->index); if (info.state != block_info::state_requested || info.peer == peer) continue; - temp.push_back(piece_block(dp->index, idx)); + temp.emplace_back(dp->index, idx); } // are we done? if (!temp.empty()) @@ -2453,20 +2444,18 @@ get_out: #if TORRENT_USE_INVARIANT_CHECKS void piece_picker::check_peers() { - for (std::vector::iterator i = m_block_info.begin() - , end(m_block_info.end()); i != end; ++i) + for (auto const& b : m_block_info) { - TORRENT_ASSERT(i->peer == nullptr || static_cast(i->peer)->in_use); + TORRENT_ASSERT(b.peer == nullptr || static_cast(b.peer)->in_use); } } #endif void piece_picker::clear_peer(torrent_peer* peer) { - for (std::vector::iterator i = m_block_info.begin() - , end(m_block_info.end()); i != end; ++i) + for (auto& b : m_block_info) { - if (i->peer == peer) i->peer = nullptr; + if (b.peer == peer) b.peer = nullptr; } } @@ -2547,8 +2536,7 @@ get_out: // looked through the downloading pieces if (options & prioritize_partials) return num_blocks; - std::vector::const_iterator i = find_dl_piece( - piece_pos::piece_downloading, piece); + auto i = find_dl_piece(piece_pos::piece_downloading, piece); TORRENT_ASSERT(i != m_downloads[state].end()); return add_blocks_downloading(*i, pieces @@ -2565,7 +2553,7 @@ get_out: num_blocks_in_piece = num_blocks; TORRENT_ASSERT(is_piece_free(piece, pieces)); for (int j = 0; j < num_blocks_in_piece; ++j) - interesting_blocks.push_back(piece_block(piece, j)); + interesting_blocks.emplace_back(piece, j); num_blocks -= num_blocks_in_piece; } else @@ -2580,7 +2568,7 @@ get_out: TORRENT_ASSERT(is_piece_free(k, pieces)); for (int j = 0; j < num_blocks_in_piece; ++j) { - interesting_blocks.push_back(piece_block(k, j)); + interesting_blocks.emplace_back(k, j); --num_blocks; --prefer_contiguous_blocks; if (prefer_contiguous_blocks == 0 @@ -2652,7 +2640,7 @@ get_out: block_info const& info = binfo[block_idx]; TORRENT_ASSERT(info.piece_index == dp.index); if (info.state != block_info::state_none) continue; - backup_blocks2.push_back(piece_block(dp.index, block_idx)); + backup_blocks2.emplace_back(dp.index, block_idx); } return num_blocks; } @@ -2666,7 +2654,7 @@ get_out: if (info.state != block_info::state_none) continue; // this block is interesting (we don't have it yet). - interesting_blocks.push_back(piece_block(dp.index, block_idx)); + interesting_blocks.emplace_back(dp.index, block_idx); // we have found a block that's free to download --num_blocks; // if we prefer contiguous blocks, continue picking from this @@ -2875,8 +2863,7 @@ get_out: // the new state downloading_piece cmp; cmp.index = dp_info.index; - std::vector::iterator i = std::lower_bound( - m_downloads[p.download_queue()].begin() + auto i = std::lower_bound(m_downloads[p.download_queue()].begin() , m_downloads[p.download_queue()].end(), cmp); TORRENT_ASSERT(i == m_downloads[p.download_queue()].end() || i->index != dp_info.index); @@ -3548,9 +3535,9 @@ get_out: int piece_picker::unverified_blocks() const { int counter = 0; - for (int k = 0; k < piece_pos::num_download_categories; ++k) + for (auto const& c : m_downloads) { - for (auto const& dp : m_downloads[k]) + for (auto const& dp : c) { counter += int(dp.finished); } diff --git a/src/platform_util.cpp b/src/platform_util.cpp index 846290dee..940d94aea 100644 --- a/src/platform_util.cpp +++ b/src/platform_util.cpp @@ -68,7 +68,7 @@ namespace libtorrent { return 256; #elif TORRENT_USE_RLIMIT - struct rlimit rl; + struct rlimit rl{}; if (getrlimit(RLIMIT_NOFILE, &rl) == 0) { if (rl.rlim_cur == rlim_infinity) @@ -125,7 +125,7 @@ namespace libtorrent { #if TORRENT_USE_RLIMIT if (ret > 0) { - struct rlimit r; + struct rlimit r{}; if (getrlimit(rlimit_as, &r) == 0 && r.rlim_cur != rlim_infinity) { if (ret > std::int64_t(r.rlim_cur)) diff --git a/src/puff.cpp b/src/puff.cpp index 778b0e7cc..d3fab0b52 100644 --- a/src/puff.cpp +++ b/src/puff.cpp @@ -82,8 +82,8 @@ // this whole file is just preserved and warnings are suppressed #include "libtorrent/aux_/disable_warnings_push.hpp" -#include /* for setjmp(), longjmp(), and jmp_buf */ -#include /* for NULL */ +#include /* for setjmp(), longjmp(), and jmp_buf */ +#include /* for nullptr */ #include "libtorrent/puff.hpp" /* prototype for puff() */ #define local static /* for local function definitions */ @@ -113,7 +113,7 @@ struct state { int bitcnt; /* number of bits in bit buffer */ /* input limit error return state for bits() and decode() */ - jmp_buf env; + std::jmp_buf env; }; /* @@ -135,7 +135,7 @@ local int bits(struct state *s, int need) val = s->bitbuf; while (s->bitcnt < need) { if (s->incnt == s->inlen) - longjmp(s->env, 1); /* out of input */ + std::longjmp(s->env, 1); /* out of input */ val |= long(s->in[s->incnt++]) << s->bitcnt; /* load eight bits */ s->bitcnt += 8; } @@ -185,7 +185,7 @@ local int stored(struct state *s) /* copy len bytes from in to out */ if (s->incnt + len > s->inlen) return 2; /* not enough input */ - if (s->out != NULL) { + if (s->out != nullptr) { if (s->outcnt + len > s->outlen) return 1; /* not enough output space */ while (len--) @@ -280,7 +280,7 @@ local int decode(struct state *s, const struct huffman *h) code = first = index = 0; len = 1; next = h->count + 1; - while (1) { + for (;;) { while (left--) { code |= bitbuf & 1; bitbuf >>= 1; @@ -300,7 +300,7 @@ local int decode(struct state *s, const struct huffman *h) if (left == 0) break; if (s->incnt == s->inlen) - longjmp(s->env, 1); /* out of input */ + std::longjmp(s->env, 1); /* out of input */ bitbuf = s->in[s->incnt++]; if (left > 8) left = 8; @@ -466,7 +466,7 @@ local int codes(struct state *s, return symbol; /* invalid symbol */ if (symbol < 256) { /* literal: symbol is the byte */ /* write out the literal */ - if (s->out != NULL) { + if (s->out != nullptr) { if (s->outcnt == s->outlen) return 1; s->out[s->outcnt] = symbol; @@ -491,7 +491,7 @@ local int codes(struct state *s, #endif /* copy length bytes from distance bytes back */ - if (s->out != NULL) { + if (s->out != nullptr) { if (s->outcnt + len > s->outlen) return 1; while (len--) { diff --git a/src/request_blocks.cpp b/src/request_blocks.cpp index 1084d97c5..868f63b31 100644 --- a/src/request_blocks.cpp +++ b/src/request_blocks.cpp @@ -204,17 +204,18 @@ namespace libtorrent { for (std::vector::iterator i = interesting_pieces.begin(); i != interesting_pieces.end(); ++i) + for (piece_block const& pb : interesting_pieces) { if (prefer_contiguous_blocks == 0 && num_requests <= 0) break; - if (time_critical_mode && p.piece_priority(i->piece_index) != top_priority) + if (time_critical_mode && p.piece_priority(pb.piece_index) != top_priority) { // assume the subsequent pieces are not prio 7 and // be done break; } - int num_block_requests = p.num_peers(*i); + int num_block_requests = p.num_peers(pb); if (num_block_requests > 0) { // have we picked enough pieces? @@ -225,30 +226,30 @@ namespace libtorrent { // as well just exit the loop if (dont_pick_busy_blocks) break; - TORRENT_ASSERT(p.num_peers(*i) > 0); - busy_block = *i; + TORRENT_ASSERT(p.num_peers(pb) > 0); + busy_block = pb; continue; } - TORRENT_ASSERT(p.num_peers(*i) == 0); + TORRENT_ASSERT(p.num_peers(pb) == 0); // don't request pieces we already have in our request queue // This happens when pieces time out or the peer sends us // pieces we didn't request. Those aren't marked in the // piece picker, but we still keep track of them in the // download queue - if (std::find_if(dq.begin(), dq.end(), aux::has_block(*i)) != dq.end() - || std::find_if(rq.begin(), rq.end(), aux::has_block(*i)) != rq.end()) + if (std::find_if(dq.begin(), dq.end(), aux::has_block(pb)) != dq.end() + || std::find_if(rq.begin(), rq.end(), aux::has_block(pb)) != rq.end()) { #if TORRENT_USE_ASSERTS std::vector::const_iterator j - = std::find_if(dq.begin(), dq.end(), aux::has_block(*i)); + = std::find_if(dq.begin(), dq.end(), aux::has_block(pb)); if (j != dq.end()) TORRENT_ASSERT(j->timed_out || j->not_wanted); #endif #ifndef TORRENT_DISABLE_LOGGING c.peer_log(peer_log_alert::info, "PIECE_PICKER" , "not_picking: %d,%d already in queue" - , static_cast(i->piece_index), i->block_index); + , static_cast(pb.piece_index), pb.block_index); #endif continue; } @@ -256,9 +257,9 @@ namespace libtorrent { // ok, we found a piece that's not being downloaded // by somebody else. request it from this peer // and return - if (!c.add_request(*i, {})) continue; - TORRENT_ASSERT(p.num_peers(*i) == 1); - TORRENT_ASSERT(p.is_requested(*i)); + if (!c.add_request(pb, {})) continue; + TORRENT_ASSERT(p.num_peers(pb) == 1); + TORRENT_ASSERT(p.is_requested(pb)); num_requests--; } diff --git a/src/session.cpp b/src/session.cpp index 5e5b04d41..922dc91ee 100644 --- a/src/session.cpp +++ b/src/session.cpp @@ -432,7 +432,7 @@ namespace { , std::shared_ptr impl) : m_io_service(std::move(ios)) , m_thread(std::move(t)) - , m_impl(impl) + , m_impl(std::move(impl)) {} session_proxy::session_proxy(session_proxy const&) = default; session_proxy& session_proxy::operator=(session_proxy const&) = default; @@ -450,7 +450,7 @@ namespace { } session_params::session_params(settings_pack sp) - : session_params(sp, default_plugins()) + : session_params(std::move(sp), default_plugins()) {} session_params::session_params(settings_pack sp diff --git a/src/session_handle.cpp b/src/session_handle.cpp index 97487086a..520eb8098 100644 --- a/src/session_handle.cpp +++ b/src/session_handle.cpp @@ -398,7 +398,7 @@ namespace { // we cannot capture a unique_ptr into a lambda in c++11, so we use a raw // pointer for now. async_call uses a lambda expression to post the call // to the main thread - add_torrent_params* p = new add_torrent_params(std::move(params)); + auto* p = new add_torrent_params(std::move(params)); p->save_path = complete(p->save_path); #ifndef TORRENT_NO_DEPRECATE @@ -419,7 +419,7 @@ namespace { , bool paused , storage_constructor_type sc) { - add_torrent_params p(sc); + add_torrent_params p(std::move(sc)); p.ti = std::make_shared(ti); p.save_path = save_path; if (resume_data.type() != entry::undefined_t) @@ -445,7 +445,7 @@ namespace { { TORRENT_ASSERT_PRECOND(!save_path.empty()); - add_torrent_params p(sc); + add_torrent_params p(std::move(sc)); p.trackers.push_back(tracker_url); p.info_hash = info_hash; p.save_path = save_path; diff --git a/src/session_impl.cpp b/src/session_impl.cpp index fa57fb505..de0b2a157 100644 --- a/src/session_impl.cpp +++ b/src/session_impl.cpp @@ -352,7 +352,7 @@ namespace aux { { TORRENT_UNUSED(ad); - session_impl* ses = reinterpret_cast(arg); + auto* ses = reinterpret_cast(arg); const char* servername = SSL_get_servername(s, TLSEXT_NAMETYPE_host_name); if (!servername || strlen(servername) < 40) @@ -1356,7 +1356,7 @@ namespace { std::uint32_t session_impl::get_tracker_key(address const& iface) const { - uintptr_t const ses = reinterpret_cast(this); + auto const ses = reinterpret_cast(this); hasher h(reinterpret_cast(&ses), sizeof(ses)); if (iface.is_v4()) { @@ -3294,7 +3294,7 @@ namespace { if (p.download_queue().size() + p.request_queue().size() > 0) ++num_peers[protocol][peer_connection::download_channel]; - if (p.upload_queue().size() > 0) + if (!p.upload_queue().empty()) ++num_peers[protocol][peer_connection::upload_channel]; } @@ -3339,8 +3339,7 @@ namespace { // check for incoming connections that might have timed out // -------------------------------------------------------------- - for (connection_map::iterator i = m_connections.begin(); - i != m_connections.end();) + for (auto i = m_connections.begin(); i != m_connections.end();) { peer_connection* p = (*i).get(); ++i; @@ -4052,7 +4051,7 @@ namespace { for (torrent_peer* pi : prev_opt_unchoke) { TORRENT_ASSERT(pi->optimistically_unchoked); - peer_connection* p = static_cast(pi->connection); + auto* p = static_cast(pi->connection); std::shared_ptr t = p->associated_torrent().lock(); pi->optimistically_unchoked = false; m_stats_counters.inc_stats_counter(counters::num_peers_up_unchoked_optimistic, -1); @@ -4152,7 +4151,7 @@ namespace { { if ((m_download_connect_attempts >= m_settings.get_int( settings_pack::connect_seed_every_n_download) - && want_peers_finished.size()) + && !want_peers_finished.empty()) || want_peers_download.empty()) { // pick a finished torrent to give a peer to @@ -4208,8 +4207,7 @@ namespace { // TODO: 3 there should be a pre-calculated list of all peers eligible for // unchoking std::vector peers; - for (connection_map::iterator i = m_connections.begin(); - i != m_connections.end();) + for (auto i = m_connections.begin(); i != m_connections.end();) { std::shared_ptr p = *i; TORRENT_ASSERT(p); @@ -4596,7 +4594,7 @@ namespace { for (auto& t : state_updates) { TORRENT_ASSERT(t->m_links[aux::session_impl::torrent_state_updates].in_list()); - status.push_back(torrent_status()); + status.emplace_back(); // querying accurate download counters may require // the torrent to be loaded. Loading a torrent, and evicting another // one will lead to calling state_updated(), which screws with @@ -5774,7 +5772,7 @@ namespace { void session_impl::set_dht_storage(dht::dht_storage_constructor_type sc) { - m_dht_storage_constructor = sc; + m_dht_storage_constructor = std::move(sc); } #ifndef TORRENT_NO_DEPRECATE @@ -5896,7 +5894,7 @@ namespace { { if (!m_dht) return; m_dht->get_item(dht::public_key(key.data()), std::bind(&session_impl::get_mutable_callback - , this, _1, _2), salt); + , this, _1, _2), std::move(salt)); } namespace { @@ -5914,9 +5912,9 @@ namespace { dht::signature const sig = i.sig(); dht::public_key const pk = i.pk(); dht::sequence_number const seq = i.seq(); - std::string const salt = i.salt(); - alerts.emplace_alert(pk.bytes, sig.bytes, salt - , seq.value, num); + std::string salt = i.salt(); + alerts.emplace_alert(pk.bytes, sig.bytes + , std::move(salt), seq.value, num); } } @@ -5964,7 +5962,7 @@ namespace { if (!m_dht) return; m_dht->put_item(dht::public_key(key.data()) , std::bind(&on_dht_put_mutable_item, std::ref(m_alerts), _1, _2) - , std::bind(&put_mutable_callback, _1, cb), salt); + , std::bind(&put_mutable_callback, _1, std::move(cb)), salt); } void session_impl::dht_get_peers(sha1_hash const& info_hash) @@ -6771,7 +6769,7 @@ namespace { ips[is_local(local_addr)][local_addr.is_v6()] = local_addr; } - return external_ip(ips[1][0], ips[0][0], ips[1][1], ips[0][1]); + return {ips[1][0], ips[0][0], ips[1][1], ips[0][1]}; } // this is the DHT observer version. DHT is the implied source diff --git a/src/smart_ban.cpp b/src/smart_ban.cpp index 254afdf31..8cdc38dea 100644 --- a/src/smart_ban.cpp +++ b/src/smart_ban.cpp @@ -83,7 +83,7 @@ namespace { // has this piece failed earlier? If it has, go through the // CRCs from the time it failed and ban the peers that // sent bad blocks - std::map::iterator i = m_block_hashes.lower_bound(piece_block(p, 0)); + auto i = m_block_hashes.lower_bound(piece_block(p, 0)); if (i == m_block_hashes.end() || i->first.piece_index != p) return; int size = m_torrent.torrent_file().piece_size(p); @@ -172,7 +172,7 @@ namespace { sha1_hash digest; }; - void on_read_failed_block(piece_block b, address a + void on_read_failed_block(piece_block const b, address const a , disk_buffer_holder buffer, int const block_size, disk_job_flags_t , storage_error const& error) { @@ -185,8 +185,7 @@ namespace { h.update({buffer.get(), std::size_t(block_size)}); h.update(reinterpret_cast(&m_salt), sizeof(m_salt)); - std::pair const range - = m_torrent.find_peers(a); + auto const range = m_torrent.find_peers(a); // there is no peer with this address anymore if (range.first == range.second) return; @@ -194,7 +193,7 @@ namespace { torrent_peer* p = (*range.first); block_entry e = {p, h.final()}; - std::map::iterator i = m_block_hashes.lower_bound(b); + auto i = m_block_hashes.lower_bound(b); if (i != m_block_hashes.end() && i->first == b && i->second.peer == p) { @@ -254,9 +253,9 @@ namespace { #endif } - void on_read_ok_block(std::pair b, address a - , disk_buffer_holder buffer, int const block_size, disk_job_flags_t - , storage_error const& error) + void on_read_ok_block(std::pair const b + , address const a, disk_buffer_holder buffer, int const block_size + , disk_job_flags_t, storage_error const& error) { TORRENT_ASSERT(m_torrent.session().is_single_thread()); @@ -271,8 +270,7 @@ namespace { if (b.second.digest == ok_digest) return; // find the peer - std::pair range - = m_torrent.find_peers(a); + auto range = m_torrent.find_peers(a); if (range.first == range.second) return; torrent_peer* p = nullptr; for (; range.first != range.second; ++range.first) @@ -319,7 +317,7 @@ namespace { std::uint32_t const m_salt; // explicitly disallow assignment, to silence msvc warning - smart_ban_plugin& operator=(smart_ban_plugin const&); + smart_ban_plugin& operator=(smart_ban_plugin const&) = delete; }; } } diff --git a/src/socks5_stream.cpp b/src/socks5_stream.cpp index 60868fd40..e18c1601c 100644 --- a/src/socks5_stream.cpp +++ b/src/socks5_stream.cpp @@ -40,9 +40,7 @@ namespace libtorrent { namespace socks_error { boost::system::error_code make_error_code(socks_error_code e) - { - return error_code(e, socks_category()); - } + { return {e, socks_category()}; } } struct socks_error_category : boost::system::error_category @@ -70,7 +68,7 @@ namespace libtorrent { } boost::system::error_condition default_error_condition( int ev) const BOOST_SYSTEM_NOEXCEPT override - { return boost::system::error_condition(ev, *this); } + { return {ev, *this}; } }; boost::system::error_category& socks_category() diff --git a/src/stack_allocator.cpp b/src/stack_allocator.cpp index 2620ebeb3..26772a158 100644 --- a/src/stack_allocator.cpp +++ b/src/stack_allocator.cpp @@ -83,7 +83,7 @@ namespace aux { { int const ret = int(m_storage.size()); int const size = int(buf.size()); - if (size < 1) return allocation_slot(); + if (size < 1) return {}; m_storage.resize(ret + size); std::memcpy(&m_storage[ret], buf.data(), numeric_cast(size)); return allocation_slot(ret); @@ -91,7 +91,7 @@ namespace aux { allocation_slot stack_allocator::allocate(int const bytes) { - if (bytes < 1) return allocation_slot(); + if (bytes < 1) return {}; int const ret = m_storage.end_index(); m_storage.resize(ret + bytes); return allocation_slot(ret); diff --git a/src/string_util.cpp b/src/string_util.cpp index 43072541f..72ceffdb3 100644 --- a/src/string_util.cpp +++ b/src/string_util.cpp @@ -164,7 +164,7 @@ namespace libtorrent { { if (str == nullptr) return nullptr; std::size_t const len = std::strlen(str); - char* tmp = new char[len + 1]; + auto* tmp = new char[len + 1]; std::copy(str, str + len, tmp); tmp[len] = '\0'; return tmp; diff --git a/src/timestamp_history.cpp b/src/timestamp_history.cpp index 80d5a8ca7..f335682ab 100644 --- a/src/timestamp_history.cpp +++ b/src/timestamp_history.cpp @@ -46,8 +46,7 @@ std::uint32_t timestamp_history::add_sample(std::uint32_t sample, bool step) { if (!initialized()) { - for (int i = 0; i < history_size; ++i) - m_history[i] = sample; + m_history.fill(sample); m_base = sample; m_num_samples = 0; } @@ -82,10 +81,10 @@ std::uint32_t timestamp_history::add_sample(std::uint32_t sample, bool step) m_history[m_index] = sample; // update m_base m_base = sample; - for (int i = 0; i < history_size; ++i) + for (auto& h : m_history) { - if (compare_less_wrap(m_history[i], m_base, TIME_MASK)) - m_base = m_history[i]; + if (compare_less_wrap(h, m_base, TIME_MASK)) + m_base = h; } } return ret; @@ -96,10 +95,10 @@ void timestamp_history::adjust_base(int change) TORRENT_ASSERT(initialized()); m_base += aux::numeric_cast(change); // make sure this adjustment sticks by updating all history slots - for (int i = 0; i < history_size; ++i) + for (auto& h : m_history) { - if (compare_less_wrap(m_history[i], m_base, TIME_MASK)) - m_history[i] = m_base; + if (compare_less_wrap(h, m_base, TIME_MASK)) + h = m_base; } } diff --git a/src/torrent.cpp b/src/torrent.cpp index 6fb550f61..759d4861e 100644 --- a/src/torrent.cpp +++ b/src/torrent.cpp @@ -844,7 +844,7 @@ namespace libtorrent { { TORRENT_INCREMENT(m_iterating_connections); if (pc->type() != connection_type::bittorrent) continue; - bt_peer_connection* p = static_cast(pc); + auto* p = static_cast(pc); p->write_share_mode(); } #endif @@ -1431,7 +1431,7 @@ namespace libtorrent { X509* cert = X509_STORE_CTX_get_current_cert(ctx.native_handle()); // Go through the alternate names in the certificate looking for matching DNS entries - GENERAL_NAMES* gens = static_cast( + auto* gens = static_cast( X509_get_ext_d2i(cert, NID_subject_alt_name, nullptr, nullptr)); #ifndef TORRENT_DISABLE_LOGGING @@ -1444,7 +1444,7 @@ namespace libtorrent { if (gen->type != GEN_DNS) continue; ASN1_IA5STRING* domain = gen->d.dNSName; if (domain->type != V_ASN1_IA5STRING || !domain->data || !domain->length) continue; - const char* torrent_name = reinterpret_cast(domain->data); + auto const* torrent_name = reinterpret_cast(domain->data); std::size_t const name_length = aux::numeric_cast(domain->length); #ifndef TORRENT_DISABLE_LOGGING @@ -1477,7 +1477,7 @@ namespace libtorrent { } if (common_name && common_name->data && common_name->length) { - const char* torrent_name = reinterpret_cast(common_name->data); + auto const* torrent_name = reinterpret_cast(common_name->data); std::size_t const name_length = aux::numeric_cast(common_name->length); #ifndef TORRENT_DISABLE_LOGGING @@ -1905,7 +1905,7 @@ namespace libtorrent { { TORRENT_INCREMENT(m_iterating_connections); if (pe->type() != connection_type::bittorrent) continue; - bt_peer_connection* p = static_cast(pe); + auto* p = static_cast(pe); if (!p->supports_holepunch()) continue; if (p->was_introduced_by(ep)) return p; } @@ -2032,7 +2032,7 @@ namespace libtorrent { // checking this torrent. pick it up where we left off if (!should_start_full_check && m_add_torrent_params - && m_add_torrent_params->have_pieces.size() > 0 + && !m_add_torrent_params->have_pieces.empty() && m_add_torrent_params->have_pieces.size() < m_torrent_file->num_pieces()) { m_checking_piece = m_num_checked_pieces @@ -2437,7 +2437,7 @@ namespace libtorrent { void torrent::use_interface(std::string net_interfaces) { std::shared_ptr p = std::make_shared(); - p->set_str(settings_pack::outgoing_interfaces, net_interfaces); + p->set_str(settings_pack::outgoing_interfaces, std::move(net_interfaces)); m_ses.apply_settings_pack(p); } #endif @@ -3968,7 +3968,7 @@ namespace libtorrent { p->trust_points = trust_points; if (p->connection) { - peer_connection* peer = static_cast(p->connection); + auto* peer = static_cast(p->connection); TORRENT_ASSERT(peer->m_in_use == 1337); peer->received_valid_data(index); } @@ -4073,7 +4073,7 @@ namespace libtorrent { { if (p && p->connection) { - peer_connection* peer = static_cast(p->connection); + auto* peer = static_cast(p->connection); peer->piece_failed = true; } } @@ -4089,7 +4089,7 @@ namespace libtorrent { bool allow_disconnect = true; if (p->connection) { - peer_connection* peer = static_cast(p->connection); + auto* peer = static_cast(p->connection); TORRENT_ASSERT(peer->m_in_use == 1337); // the peer implementation can ask not to be disconnected. @@ -4137,7 +4137,7 @@ namespace libtorrent { if (p->connection) { - peer_connection* peer = static_cast(p->connection); + auto* peer = static_cast(p->connection); #ifndef TORRENT_DISABLE_LOGGING if (should_log()) { @@ -4186,7 +4186,7 @@ namespace libtorrent { { if (p && p->connection) { - peer_connection* peer = static_cast(p->connection); + auto* peer = static_cast(p->connection); peer->piece_failed = false; } } @@ -4690,7 +4690,7 @@ namespace libtorrent { { torrent_peer* tp = *i; if (tp == nullptr || tp->connection == nullptr) continue; - peer_connection* peer = static_cast(tp->connection); + auto* peer = static_cast(tp->connection); peer->make_time_critical(piece_block(piece, block)); } } @@ -5540,7 +5540,7 @@ namespace libtorrent { debug_log("removing web seed: \"%s\"", web->url.c_str()); #endif - peer_connection* peer = static_cast(web->peer_info.connection); + auto* peer = static_cast(web->peer_info.connection); if (peer != nullptr) { // if we have a connection for this web seed, we also need to @@ -6399,7 +6399,7 @@ namespace libtorrent { TORRENT_ASSERT(tp->in_use); if (tp->connection) { - peer_connection* peer = static_cast(tp->connection); + auto* peer = static_cast(tp->connection); TORRENT_ASSERT(peer->m_in_use); bi.set_peer(peer->remote()); if (bi.state == block_info::requested) @@ -9940,7 +9940,7 @@ namespace libtorrent { TORRENT_ASSERT(i != m_web_seeds.end()); if (i == m_web_seeds.end()) return; - peer_connection* peer = static_cast(i->peer_info.connection); + auto* peer = static_cast(i->peer_info.connection); if (peer != nullptr) { // if we have a connection for this web seed, we also need to @@ -10356,7 +10356,7 @@ namespace libtorrent { torrent_peer* p = info.peer; if (p != nullptr && p->connection) { - peer_connection* peer = static_cast(p->connection); + auto* peer = static_cast(p->connection); auto pbp = peer->downloading_piece_progress(); if (pbp.piece_index == dp.index && pbp.block_index == idx) block = pbp.bytes_downloaded; diff --git a/src/torrent_handle.cpp b/src/torrent_handle.cpp index db3515827..6320a7e57 100644 --- a/src/torrent_handle.cpp +++ b/src/torrent_handle.cpp @@ -87,7 +87,7 @@ namespace libtorrent { { std::shared_ptr t = m_torrent.lock(); if (!t) aux::throw_ex(errors::invalid_torrent_handle); - session_impl& ses = static_cast(t->session()); + auto& ses = static_cast(t->session()); ses.get_io_service().dispatch([=,&ses] () { #ifndef BOOST_NO_EXCEPTIONS @@ -114,7 +114,7 @@ namespace libtorrent { { std::shared_ptr t = m_torrent.lock(); if (!t) aux::throw_ex(errors::invalid_torrent_handle); - session_impl& ses = static_cast(t->session()); + auto& ses = static_cast(t->session()); // this is the flag to indicate the call has completed bool done = false; @@ -150,7 +150,7 @@ namespace libtorrent { #else if (!t) return r; #endif - session_impl& ses = static_cast(t->session()); + auto& ses = static_cast(t->session()); // this is the flag to indicate the call has completed bool done = false; @@ -772,7 +772,7 @@ namespace libtorrent { std::shared_ptr t = m_torrent.lock(); if (!t || !t->has_storage()) return; - session_impl& ses = static_cast(t->session()); + auto& ses = static_cast(t->session()); status = ses.disk_thread().get_status(t->storage()); } #endif @@ -793,7 +793,7 @@ namespace libtorrent { { std::shared_ptr t = m_torrent.lock(); if (!t || !t->has_storage()) return {}; - session_impl& ses = static_cast(t->session()); + auto& ses = static_cast(t->session()); return ses.disk_thread().get_status(t->storage()); } diff --git a/src/udp_socket.cpp b/src/udp_socket.cpp index defe77556..6b175abe8 100644 --- a/src/udp_socket.cpp +++ b/src/udp_socket.cpp @@ -82,8 +82,8 @@ private: std::shared_ptr self() { return shared_from_this(); } void on_name_lookup(error_code const& e, tcp::resolver::iterator i); - void on_connect_timeout(error_code const& ec); - void on_connected(error_code const& ec); + void on_connect_timeout(error_code const& e); + void on_connected(error_code const& e); void handshake1(error_code const& e); void handshake2(error_code const& e); void handshake3(error_code const& e); @@ -160,7 +160,7 @@ udp_socket::udp_socket(io_service& ios) int udp_socket::read(span pkts, error_code& ec) { - int const num = int(pkts.size()); + auto const num = int(pkts.size()); int ret = 0; packet p; @@ -350,7 +350,7 @@ bool udp_socket::unwrap(udp::endpoint& from, span& buf) using namespace libtorrent::detail; // the minimum socks5 header size - int const size = aux::numeric_cast(buf.size()); + auto const size = aux::numeric_cast(buf.size()); if (size <= 10) return false; char* p = buf.data(); @@ -521,11 +521,11 @@ void socks5::on_name_lookup(error_code const& e, tcp::resolver::iterator i) , self(), _1)); } -void socks5::on_connect_timeout(error_code const& ec) +void socks5::on_connect_timeout(error_code const& e) { COMPLETE_ASYNC("socks5::on_connect_timeout"); - if (ec == boost::asio::error::operation_aborted) return; + if (e == boost::asio::error::operation_aborted) return; if (m_abort) return; diff --git a/src/udp_tracker_connection.cpp b/src/udp_tracker_connection.cpp index 63525c523..016f4fa9e 100644 --- a/src/udp_tracker_connection.cpp +++ b/src/udp_tracker_connection.cpp @@ -62,7 +62,7 @@ namespace libtorrent { , tracker_manager& man , tracker_request const& req , std::weak_ptr c) - : tracker_connection(man, req, ios, c) + : tracker_connection(man, req, ios, std::move(c)) , m_transaction_id(0) , m_attempts(0) , m_state(action_t::error) @@ -146,7 +146,7 @@ namespace libtorrent { std::shared_ptr cb = requester(); if (cb && cb->should_log()) { - cb->debug_log("*** UDP_TRACKER [ host: \"%s\" ip: \"%s\" | error: \"%s\" ]" + cb->debug_log(R"(*** UDP_TRACKER [ host: "%s" ip: "%s" | error: "%s" ])" , m_hostname.c_str(), print_endpoint(m_target).c_str(), ec.message().c_str()); } #endif @@ -157,7 +157,7 @@ namespace libtorrent { #ifndef TORRENT_DISABLE_LOGGING if (cb && cb->should_log()) { - cb->debug_log("*** UDP_TRACKER trying next IP [ host: \"%s\" ip: \"%s\" ]" + cb->debug_log(R"(*** UDP_TRACKER trying next IP [ host: "%s" ip: "%s" ])" , m_hostname.c_str(), print_endpoint(m_target).c_str()); } #endif diff --git a/src/upnp.cpp b/src/upnp.cpp index 09486fac5..9e724b55f 100644 --- a/src/upnp.cpp +++ b/src/upnp.cpp @@ -71,9 +71,7 @@ constexpr std::size_t max_global_mappings = 50; namespace upnp_errors { boost::system::error_code make_error_code(error_code_enum e) - { - return error_code(e, upnp_category()); - } + { return {e, upnp_category()}; } } // upnp_errors namespace @@ -372,7 +370,7 @@ void upnp::resend_request(error_code const& ec) } } -void upnp::on_reply(udp::endpoint const& from, char* buffer +void upnp::on_reply(udp::endpoint const& from, char const* buffer , std::size_t const bytes_transferred) { TORRENT_ASSERT(is_single_thread()); @@ -1222,7 +1220,7 @@ struct upnp_error_category : boost::system::error_category boost::system::error_condition default_error_condition( int ev) const BOOST_SYSTEM_NOEXCEPT override { - return boost::system::error_condition(ev, *this); + return {ev, *this}; } }; @@ -1593,9 +1591,9 @@ void upnp::on_expire(error_code const& ec) time_point const now = aux::time_now(); time_point next_expire = max_time(); - for (auto i = m_devices.begin(), end(m_devices.end()); i != end; ++i) + for (auto& dev : m_devices) { - rootdevice& d = const_cast(*i); + rootdevice& d = const_cast(dev); TORRENT_ASSERT(d.magic == 1337); for (port_mapping_t m{0}; m < m_mappings.end_index(); ++m) { @@ -1633,21 +1631,21 @@ void upnp::close() m_closing = true; m_socket.close(); - for (auto i = m_devices.begin(), end(m_devices.end()); i != end; ++i) + for (auto& dev : m_devices) { - rootdevice& d = const_cast(*i); + rootdevice& d = const_cast(dev); TORRENT_ASSERT(d.magic == 1337); if (d.control_url.empty()) continue; - for (auto j = d.mapping.begin(), end2(d.mapping.end()); j != end2; ++j) + for (auto& m : d.mapping) { - if (j->protocol == portmap_protocol::none) continue; - if (j->act == portmap_action::add) + if (m.protocol == portmap_protocol::none) continue; + if (m.act == portmap_action::add) { - j->act = portmap_action::none; + m.act = portmap_action::none; continue; } - j->act = portmap_action::del; - m_mappings[port_mapping_t{static_cast(j - d.mapping.begin())}].protocol = portmap_protocol::none; + m.act = portmap_action::del; + m_mappings[port_mapping_t{static_cast(&m - d.mapping.data())}].protocol = portmap_protocol::none; } if (num_mappings() > 0) update_map(d, port_mapping_t{0}); } diff --git a/src/ut_metadata.cpp b/src/ut_metadata.cpp index 9145ab807..107982c87 100644 --- a/src/ut_metadata.cpp +++ b/src/ut_metadata.cpp @@ -126,7 +126,7 @@ namespace libtorrent {namespace { } bool received_metadata(ut_metadata_peer_plugin& source - , char const* buf, int const size, int const piece, int const total_size); + , char const* buf, int size, int piece, int total_size); // returns a piece of the metadata that // we should request. @@ -333,8 +333,9 @@ namespace libtorrent {namespace { m_pc.disconnect(errors::invalid_metadata_message, operation_t::bittorrent, 2); return true; } - int type = int(type_ent->integer()); - int piece = int(piece_ent->integer()); + // TODO: make this an enum class + auto const type = int(type_ent->integer()); + auto const piece = int(piece_ent->integer()); #ifndef TORRENT_DISABLE_LOGGING m_pc.peer_log(peer_log_alert::incoming_message, "UT_METADATA" @@ -417,7 +418,7 @@ namespace libtorrent {namespace { while (!m_incoming_requests.empty() && m_pc.send_buffer_size() < send_buffer_limit) { - int piece = m_incoming_requests.front(); + int const piece = m_incoming_requests.front(); m_incoming_requests.erase(m_incoming_requests.begin()); write_metadata_packet(metadata_piece, piece); } @@ -436,7 +437,7 @@ namespace libtorrent {namespace { && m_sent_requests.size() < 2 && has_metadata()) { - int piece = m_tp.metadata_request(m_pc.has_metadata()); + int const piece = m_tp.metadata_request(m_pc.has_metadata()); if (piece == -1) return; m_sent_requests.push_back(piece); @@ -493,7 +494,7 @@ namespace libtorrent {namespace { // from requesting this block by setting a timeout on it. int ut_metadata_plugin::metadata_request(bool const has_metadata) { - std::vector::iterator i = std::min_element( + auto i = std::min_element( m_requested_metadata.begin(), m_requested_metadata.end()); if (m_requested_metadata.empty()) diff --git a/src/utf8.cpp b/src/utf8.cpp index 8b1ecf3c0..eb46b0448 100644 --- a/src/utf8.cpp +++ b/src/utf8.cpp @@ -201,9 +201,7 @@ namespace { boost::system::error_condition default_error_condition( int ev) const BOOST_SYSTEM_NOEXCEPT override - { - return boost::system::error_condition(ev, *this); - } + { return {ev, *this}; } }; } // anonymous namespace @@ -211,7 +209,7 @@ namespace { { boost::system::error_code make_error_code(utf8_errors::error_code_enum e) { - return error_code(e, utf8_category()); + return {e, utf8_category()}; } } // utf_errors namespace diff --git a/src/utp_socket_manager.cpp b/src/utp_socket_manager.cpp index d2e5d9d10..5d537b29b 100644 --- a/src/utp_socket_manager.cpp +++ b/src/utp_socket_manager.cpp @@ -163,7 +163,7 @@ namespace libtorrent { if (p.size() < sizeof(utp_header)) return false; - utp_header const* ph = reinterpret_cast(p.data()); + auto const* ph = reinterpret_cast(p.data()); // UTP_LOGV("incoming packet version:%d\n", int(ph->get_version())); @@ -231,7 +231,7 @@ namespace libtorrent { int link_mtu, utp_mtu; mtu_for_dest(ep.address(), link_mtu, utp_mtu); utp_init_mtu(str->get_impl(), link_mtu, utp_mtu); - utp_init_socket(str->get_impl(), socket); + utp_init_socket(str->get_impl(), std::move(socket)); bool ret = utp_incoming_packet(str->get_impl(), p, ep, receive_time); if (!ret) return false; m_cb(c); diff --git a/src/utp_stream.cpp b/src/utp_stream.cpp index 44267498a..466bfaaa8 100644 --- a/src/utp_stream.cpp +++ b/src/utp_stream.cpp @@ -872,7 +872,7 @@ int utp_stream::read_buffer_size() const void utp_stream::on_close_reason(void* self, close_reason_t reason) { - utp_stream* s = static_cast(self); + auto* s = static_cast(self); // it's possible the socket has been unlinked already, in which case m_impl // will be nullptr @@ -883,7 +883,7 @@ void utp_stream::on_close_reason(void* self, close_reason_t reason) void utp_stream::on_read(void* self, std::size_t const bytes_transferred , error_code const& ec, bool const shutdown) { - utp_stream* s = static_cast(self); + auto* s = static_cast(self); UTP_LOGV("%8p: calling read handler read:%d ec:%s shutdown:%d\n", static_cast(s->m_impl) , int(bytes_transferred), ec.message().c_str(), shutdown); @@ -903,7 +903,7 @@ void utp_stream::on_read(void* self, std::size_t const bytes_transferred void utp_stream::on_write(void* self, std::size_t const bytes_transferred , error_code const& ec, bool const shutdown) { - utp_stream* s = static_cast(self); + auto* s = static_cast(self); UTP_LOGV("%8p: calling write handler written:%d ec:%s shutdown:%d\n" , static_cast(s->m_impl) @@ -923,7 +923,7 @@ void utp_stream::on_write(void* self, std::size_t const bytes_transferred void utp_stream::on_connect(void* self, error_code const& ec, bool shutdown) { - utp_stream* s = static_cast(self); + auto* s = static_cast(self); TORRENT_ASSERT(s); UTP_LOGV("%8p: calling connect handler ec:%s shutdown:%d\n" @@ -1310,7 +1310,7 @@ void utp_socket_impl::send_syn() p->num_fast_resend = 0; #endif p->need_resend = false; - utp_header* h = reinterpret_cast(p->buf); + auto* h = reinterpret_cast(p->buf); h->type_ver = (ST_SYN << 4) | 1; h->extension = utp_no_extension; // using recv_id here is intentional! This is an odd @@ -1645,7 +1645,7 @@ void utp_socket_impl::remove_sack_header(packet* p) // remove the sack header std::uint8_t* ptr = p->buf + sizeof(utp_header); - utp_header* h = reinterpret_cast(p->buf); + auto* h = reinterpret_cast(p->buf); TORRENT_ASSERT(h->extension == utp_sack); @@ -2135,7 +2135,7 @@ bool utp_socket_impl::resend_packet(packet* p, bool fast_resend) if (fast_resend) ++p->num_fast_resend; #endif p->need_resend = false; - utp_header* h = reinterpret_cast(p->buf); + auto* h = reinterpret_cast(p->buf); // update packet header h->timestamp_difference_microseconds = m_reply_micro; p->send_time = clock_type::now(); @@ -2604,7 +2604,7 @@ bool utp_socket_impl::incoming_packet(span buf { INVARIANT_CHECK; - utp_header const* ph = reinterpret_cast(buf.data()); + auto const* ph = reinterpret_cast(buf.data()); m_sm.inc_stats_counter(counters::utp_packets_in); diff --git a/src/web_peer_connection.cpp b/src/web_peer_connection.cpp index 1658c5542..bd85e74d7 100644 --- a/src/web_peer_connection.cpp +++ b/src/web_peer_connection.cpp @@ -254,8 +254,7 @@ void web_peer_connection::disconnect(error_code const& ec piece_block_progress web_peer_connection::downloading_piece_progress() const { - if (m_requests.empty()) - return piece_block_progress(); + if (m_requests.empty()) return {}; std::shared_ptr t = associated_torrent().lock(); TORRENT_ASSERT(t); @@ -267,7 +266,7 @@ piece_block_progress web_peer_connection::downloading_piece_progress() const // this is used to make sure that the block_index stays within // bounds. If the entire piece is downloaded, the block_index // would otherwise point to one past the end - int correction = m_piece.size() ? -1 : 0; + int correction = m_piece.empty() ? 0 : -1; ret.block_index = (m_requests.front().start + int(m_piece.size()) + correction) / t->block_size(); TORRENT_ASSERT(ret.block_index < int(piece_block::invalid.block_index)); TORRENT_ASSERT(ret.piece_index < piece_block::invalid.piece_index); @@ -514,7 +513,7 @@ namespace { ec = errors::no_content_length; } } - return std::tuple(range_start, range_end); + return std::make_tuple(range_start, range_end); } } @@ -662,7 +661,7 @@ void web_peer_connection::handle_redirect(int const bytes_left) if (web->peer_info.connection != nullptr) { - peer_connection* pc = static_cast(web->peer_info.connection); + auto* pc = static_cast(web->peer_info.connection); // we just learned that this host has this file, and we're currently // connected to it. Make it advertise that it has this file to the @@ -874,7 +873,7 @@ void web_peer_connection::on_receive(error_code const& error // === CHUNKED ENCODING === // ========================= - while (m_chunk_pos >= 0 && recv_buffer.size() > 0) + while (m_chunk_pos >= 0 && !recv_buffer.empty()) { // first deliver any payload we have in the buffer so far, ahead of // the next chunk header. diff --git a/test/test_upnp.cpp b/test/test_upnp.cpp index 56c9d4307..581eaaa91 100644 --- a/test/test_upnp.cpp +++ b/test/test_upnp.cpp @@ -69,7 +69,7 @@ char const* soap_delete_response[] = { "" ""}; -void incoming_msearch(udp::endpoint const& from, char* buffer +void incoming_msearch(udp::endpoint const& from, char const* buffer , int size) { http_parser p;