From 592544f30f587294351bafce3dee8f53ff7cf03e Mon Sep 17 00:00:00 2001 From: arvidn Date: Tue, 5 Nov 2019 19:14:46 +0000 Subject: [PATCH] fix some PVS studio warnings --- examples/connection_tester.cpp | 4 ++-- include/libtorrent/bitfield.hpp | 4 ++-- include/libtorrent/packet_pool.hpp | 2 +- src/http_connection.cpp | 3 +-- src/peer_connection.cpp | 2 +- src/stack_allocator.cpp | 2 +- src/upnp.cpp | 5 ++--- 7 files changed, 10 insertions(+), 12 deletions(-) diff --git a/examples/connection_tester.cpp b/examples/connection_tester.cpp index ac93330fe..2bb4c0b79 100644 --- a/examples/connection_tester.cpp +++ b/examples/connection_tester.cpp @@ -681,7 +681,7 @@ struct peer_conn if (corruption_counter == 0) { corruption_counter = 1000; - memset(write_buffer, 0, 10); + std::memset(write_buffer, 0, 10); } } char* ptr = write_buf_proto; @@ -1009,7 +1009,7 @@ int main(int argc, char* argv[]) , torrent_name, std::strerror(errno)); return 1; } - size_t ret = fwrite(&buf[0], 1, buf.size(), f); + size_t ret = fwrite(buf.data(), 1, buf.size(), f); if (ret != buf.size()) { std::fprintf(stderr, "write returned: %d (expected %d)\n", int(ret), int(buf.size())); diff --git a/include/libtorrent/bitfield.hpp b/include/libtorrent/bitfield.hpp index 6c92306f1..8d67e6847 100644 --- a/include/libtorrent/bitfield.hpp +++ b/include/libtorrent/bitfield.hpp @@ -247,13 +247,13 @@ namespace libtorrent { void set_all() noexcept { if (size() == 0) return; - std::memset(buf(), 0xff, std::size_t(num_words() * 4)); + std::memset(buf(), 0xff, std::size_t(num_words()) * 4); clear_trailing_bits(); } void clear_all() noexcept { if (size() == 0) return; - std::memset(buf(), 0x00, std::size_t(num_words() * 4)); + std::memset(buf(), 0x00, std::size_t(num_words()) * 4); } // make the bitfield empty, of zero size. diff --git a/include/libtorrent/packet_pool.hpp b/include/libtorrent/packet_pool.hpp index 40560b2ca..807afafb2 100644 --- a/include/libtorrent/packet_pool.hpp +++ b/include/libtorrent/packet_pool.hpp @@ -113,7 +113,7 @@ namespace libtorrent { { packet* p = static_cast(std::malloc(sizeof(packet) + aux::numeric_cast(size))); if (p == nullptr) aux::throw_ex(); - new (p) packet(); + p = new (p) packet(); p->allocated = aux::numeric_cast(size); return packet_ptr(p); } diff --git a/src/http_connection.cpp b/src/http_connection.cpp index 8e63c4b84..901efd535 100644 --- a/src/http_connection.cpp +++ b/src/http_connection.cpp @@ -384,11 +384,11 @@ void http_connection::start(std::string const& hostname, int port } else #endif + m_hostname = hostname; if (ps && ps->proxy_hostnames && (ps->type == settings_pack::socks5 || ps->type == settings_pack::socks5_pw)) { - m_hostname = hostname; m_port = std::uint16_t(port); m_endpoints.emplace_back(address(), m_port); connect(); @@ -400,7 +400,6 @@ void http_connection::start(std::string const& hostname, int port , std::bind(&http_connection::on_resolve , me, _1, _2)); } - m_hostname = hostname; m_port = std::uint16_t(port); } } diff --git a/src/peer_connection.cpp b/src/peer_connection.cpp index c01fe8326..a1853d90f 100644 --- a/src/peer_connection.cpp +++ b/src/peer_connection.cpp @@ -4085,7 +4085,7 @@ namespace libtorrent { if (m_connecting) { m_counters.inc_stats_counter(counters::num_peers_half_open, -1); - if (t) t->dec_num_connecting(m_peer_info); + if (t && m_peer_info) t->dec_num_connecting(m_peer_info); m_connecting = false; } diff --git a/src/stack_allocator.cpp b/src/stack_allocator.cpp index 05522110b..51ed07dd4 100644 --- a/src/stack_allocator.cpp +++ b/src/stack_allocator.cpp @@ -71,7 +71,7 @@ namespace aux { #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wformat-nonliteral" #endif - int const ret = std::vsnprintf(m_storage.data() + pos, static_cast(len + 1), fmt, args); + int const ret = std::vsnprintf(m_storage.data() + pos, static_cast(len) + 1, fmt, args); #ifdef __clang__ #pragma clang diagnostic pop #endif diff --git a/src/upnp.cpp b/src/upnp.cpp index b1e8a5bb1..95850957f 100644 --- a/src/upnp.cpp +++ b/src/upnp.cpp @@ -1284,7 +1284,7 @@ void upnp::on_upnp_get_ip_address_response(error_code const& e #ifndef TORRENT_DISABLE_LOGGING if (s.error_code != -1) { - log("error while getting external IP address, code: %u", s.error_code); + log("error while getting external IP address, code: %d", s.error_code); } #endif @@ -1386,8 +1386,7 @@ void upnp::on_upnp_map_response(error_code const& e if (s.error_code != -1) { #ifndef TORRENT_DISABLE_LOGGING - log("error while adding port map, code: %u" - , s.error_code); + log("error while adding port map, code: %d", s.error_code); #endif }