From e92cf183404036b70502d567f7e459714c36e78c Mon Sep 17 00:00:00 2001 From: arvidn Date: Thu, 19 Jul 2018 14:27:12 +0200 Subject: [PATCH] fix some warnings around unused variables, dead stores, potentially dereferencing nullptr and calling virtual functions from constructor --- include/libtorrent/peer_connection.hpp | 4 ++-- src/kademlia/node.cpp | 2 +- src/peer_connection.cpp | 16 ++++++++-------- src/resolve_links.cpp | 2 +- src/utp_stream.cpp | 9 +++------ test/test_block_cache.cpp | 2 ++ 6 files changed, 17 insertions(+), 18 deletions(-) diff --git a/include/libtorrent/peer_connection.hpp b/include/libtorrent/peer_connection.hpp index 9bc50d2e6..a59f98ab9 100644 --- a/include/libtorrent/peer_connection.hpp +++ b/include/libtorrent/peer_connection.hpp @@ -516,9 +516,9 @@ namespace aux { int est_reciprocation_rate() const { return m_est_reciprocation_rate; } #ifndef TORRENT_DISABLE_LOGGING - bool should_log(peer_log_alert::direction_t direction) const override; + bool should_log(peer_log_alert::direction_t direction) const override final; void peer_log(peer_log_alert::direction_t direction - , char const* event, char const* fmt, ...) const noexcept override TORRENT_FORMAT(4,5); + , char const* event, char const* fmt, ...) const noexcept override final TORRENT_FORMAT(4,5); void peer_log(peer_log_alert::direction_t direction , char const* event) const noexcept; diff --git a/src/kademlia/node.cpp b/src/kademlia/node.cpp index 8c5090310..32d678f51 100644 --- a/src/kademlia/node.cpp +++ b/src/kademlia/node.cpp @@ -453,7 +453,7 @@ void node::announce(sha1_hash const& info_hash, int listen_port, announce_flags_ } #endif - if (listen_port == 0) + if (listen_port == 0 && m_observer != nullptr) { listen_port = m_observer->get_listen_port( flags & announce::ssl_torrent ? aux::transport::ssl : aux::transport::plaintext diff --git a/src/peer_connection.cpp b/src/peer_connection.cpp index 6c6d7c83b..61ad3b9a7 100644 --- a/src/peer_connection.cpp +++ b/src/peer_connection.cpp @@ -5893,7 +5893,7 @@ namespace libtorrent { if (buffer_size > 0) { span const vec = m_recv_buffer.reserve(buffer_size); - std::size_t bytes = m_socket->read_some( + std::size_t const bytes = m_socket->read_some( boost::asio::mutable_buffers_1(vec.data(), vec.size()), ec); // this is weird. You would imagine read_some() would do this @@ -5908,14 +5908,14 @@ namespace libtorrent { #endif TORRENT_ASSERT(bytes > 0 || ec); - if (ec == boost::asio::error::would_block || ec == boost::asio::error::try_again) + if (ec) { - bytes = 0; - } - else if (ec) - { - disconnect(ec, operation_t::sock_read); - return; + if (ec != boost::asio::error::would_block + && ec != boost::asio::error::try_again) + { + disconnect(ec, operation_t::sock_read); + return; + } } else { diff --git a/src/resolve_links.cpp b/src/resolve_links.cpp index 64248f969..7f57e4d37 100644 --- a/src/resolve_links.cpp +++ b/src/resolve_links.cpp @@ -84,7 +84,7 @@ void resolve_links::match(std::shared_ptr const& ti std::int64_t const file_size = fs.file_size(i); - auto range = m_file_sizes.equal_range(file_size); + auto const range = m_file_sizes.equal_range(file_size); for (auto iter = range.first; iter != range.second; ++iter) { TORRENT_ASSERT(iter->second >= file_index_t(0)); diff --git a/src/utp_stream.cpp b/src/utp_stream.cpp index 252c67207..5f811eeca 100644 --- a/src/utp_stream.cpp +++ b/src/utp_stream.cpp @@ -1793,8 +1793,6 @@ bool utp_socket_impl::send_pkt(int const flags) return false; } - int packet_size = header_size + payload_size; - packet_ptr p; std::uint8_t* ptr = nullptr; utp_header* h = nullptr; @@ -1810,6 +1808,7 @@ bool utp_socket_impl::send_pkt(int const flags) m_sm.inc_stats_counter(counters::utp_payload_pkts_out); } + int const packet_size = header_size + payload_size; p->size = std::uint16_t(packet_size); p->header_size = std::uint16_t(packet_size - payload_size); p->num_transmissions = 0; @@ -1883,7 +1882,6 @@ bool utp_socket_impl::send_pkt(int const flags) return false; } - packet_size = p->size; payload_size = p->size - p->header_size; } @@ -2572,7 +2570,7 @@ bool utp_socket_impl::test_socket_state() return false; } -void utp_socket_impl::init_mtu(int link_mtu, int utp_mtu) +void utp_socket_impl::init_mtu(int const link_mtu, int utp_mtu) { INVARIANT_CHECK; @@ -2580,9 +2578,8 @@ void utp_socket_impl::init_mtu(int link_mtu, int utp_mtu) { // we can't use larger packets than this since we're // not allocating any more memory for socket buffers - int decrease = link_mtu - TORRENT_ETHERNET_MTU; + int const decrease = link_mtu - TORRENT_ETHERNET_MTU; utp_mtu -= decrease; - link_mtu -= decrease; } // set the ceiling to what we found out from the interface diff --git a/test/test_block_cache.cpp b/test/test_block_cache.cpp index 2a5546128..9134764ec 100644 --- a/test/test_block_cache.cpp +++ b/test/test_block_cache.cpp @@ -433,6 +433,7 @@ void test_iovec() TEST_SETUP; ret = bc.allocate_iovec(iov); + TEST_EQUAL(ret, 0); bc.free_iovec(iov); } @@ -505,6 +506,7 @@ TORRENT_TEST(delete_piece) rj.storage = pm; rj.argument = remove_flags_t{}; ret = bc.try_read(&rj, alloc); + TEST_EQUAL(ret, -1); cached_piece_entry* pe_ = bc.find_piece(pm.get(), piece_index_t(0)); bc.mark_for_eviction(pe_, block_cache::disallow_ghost);