From 4a304273fd670dc57f2b23ab32f7012d7d3117ff Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Sun, 3 May 2015 03:28:39 +0000 Subject: [PATCH] fix some coverity issues --- examples/print.cpp | 6 ++++++ include/libtorrent/torrent.hpp | 3 ++- src/peer_connection.cpp | 6 ++++-- src/piece_picker.cpp | 2 ++ src/torrent.cpp | 23 +++++++++++++++-------- src/utp_stream.cpp | 11 ++++++----- 6 files changed, 35 insertions(+), 16 deletions(-) diff --git a/examples/print.cpp b/examples/print.cpp index eb1f6e786..4bff9eef6 100644 --- a/examples/print.cpp +++ b/examples/print.cpp @@ -179,6 +179,12 @@ void terminal_size(int* terminal_width, int* terminal_height) *terminal_height = coninfo.srWindow.Bottom - coninfo.srWindow.Top; #else int tty = open("/dev/tty", O_RDONLY); + if (tty < 0) + { + *terminal_width = 190; + *terminal_height = 100; + return; + } winsize size; int ret = ioctl(tty, TIOCGWINSZ, reinterpret_cast(&size)); close(tty); diff --git a/include/libtorrent/torrent.hpp b/include/libtorrent/torrent.hpp index 690be6cf2..b0a2a5da8 100644 --- a/include/libtorrent/torrent.hpp +++ b/include/libtorrent/torrent.hpp @@ -393,7 +393,8 @@ namespace libtorrent error_code error; }; void read_piece(int piece); - void on_disk_read_complete(disk_io_job const* j, peer_request r, read_piece_struct* rp); + void on_disk_read_complete(disk_io_job const* j, peer_request r + , boost::shared_ptr rp); storage_mode_t storage_mode() const { return (storage_mode_t)m_storage_mode; } storage_interface* get_storage() diff --git a/src/peer_connection.cpp b/src/peer_connection.cpp index 026917088..00c7e913e 100644 --- a/src/peer_connection.cpp +++ b/src/peer_connection.cpp @@ -5316,19 +5316,21 @@ namespace libtorrent TORRENT_ASSERT(is_single_thread()); shared_ptr t = m_torrent.lock(); + const int tick_interval = (std::max)(1, m_settings.get_int(settings_pack::tick_interval)); + if (channel == download_channel) { return (std::max)((std::max)(m_outstanding_bytes , m_recv_buffer.packet_bytes_remaining()) + 30 , int(boost::int64_t(m_statistics.download_rate()) * 2 - / (1000 / m_settings.get_int(settings_pack::tick_interval)))); + / (1000 / tick_interval))); } else { return (std::max)((std::max)(m_reading_bytes , m_send_buffer.size()) , int((boost::int64_t(m_statistics.upload_rate()) * 2 - * m_settings.get_int(settings_pack::tick_interval)) / 1000)); + * tick_interval) / 1000)); } } diff --git a/src/piece_picker.cpp b/src/piece_picker.cpp index b7250ccd4..54ead461e 100644 --- a/src/piece_picker.cpp +++ b/src/piece_picker.cpp @@ -835,6 +835,8 @@ namespace libtorrent int priority = p.priority(this); TORRENT_ASSERT(priority >= 0); + if (priority < 0) return; + if (int(m_priority_boundries.size()) <= priority) m_priority_boundries.resize(priority + 1, m_pieces.size()); diff --git a/src/torrent.cpp b/src/torrent.cpp index 39e357eaf..c6f4c3138 100644 --- a/src/torrent.cpp +++ b/src/torrent.cpp @@ -918,14 +918,22 @@ namespace libtorrent } TORRENT_ASSERT(piece >= 0 && piece < m_torrent_file->num_pieces()); - int piece_size = m_torrent_file->piece_size(piece); - int blocks_in_piece = (piece_size + block_size() - 1) / block_size(); + const int piece_size = m_torrent_file->piece_size(piece); + const int blocks_in_piece = (piece_size + block_size() - 1) / block_size(); - // if blocks_in_piece is 0, rp will leak TORRENT_ASSERT(blocks_in_piece > 0); TORRENT_ASSERT(piece_size > 0); - read_piece_struct* rp = new read_piece_struct; + if (blocks_in_piece == 0) + { + // this shouldn't actually happen + boost::shared_array buf; + m_ses.alerts().emplace_alert( + get_handle(), piece, buf, 0); + return; + } + + boost::shared_ptr rp = boost::make_shared(); rp->piece_data.reset(new (std::nothrow) char[piece_size]); rp->blocks_left = 0; rp->fail = false; @@ -939,7 +947,6 @@ namespace libtorrent rp->piece_data.reset(); m_ses.alerts().emplace_alert( get_handle(), r.piece, rp->piece_data, 0); - delete rp; return; } for (int i = 0; i < blocks_in_piece; ++i, r.start += block_size()) @@ -1194,7 +1201,8 @@ namespace libtorrent } } - void torrent::on_disk_read_complete(disk_io_job const* j, peer_request r, read_piece_struct* rp) + void torrent::on_disk_read_complete(disk_io_job const* j, peer_request r + , boost::shared_ptr rp) { // hold a reference until this function returns torrent_ref_holder h(this, "read_piece"); @@ -1229,7 +1237,6 @@ namespace libtorrent m_ses.alerts().emplace_alert( get_handle(), r.piece, rp->piece_data, size); } - delete rp; } } @@ -9920,7 +9927,7 @@ namespace libtorrent int num_downloaded_pieces = (std::max)(m_picker->num_have() , pieces_in_torrent - m_picker->num_filtered()); - if (num_downloaded_pieces * m_torrent_file->piece_length() + if (boost::int64_t(num_downloaded_pieces) * m_torrent_file->piece_length() * settings().get_int(settings_pack::share_mode_target) > m_total_uploaded && num_downloaded_pieces > 0) return; diff --git a/src/utp_stream.cpp b/src/utp_stream.cpp index 35c0c5fce..b26b8d1b8 100644 --- a/src/utp_stream.cpp +++ b/src/utp_stream.cpp @@ -3323,7 +3323,8 @@ bool utp_socket_impl::incoming_packet(boost::uint8_t const* buf, int size return false; } -void utp_socket_impl::do_ledbat(int acked_bytes, int delay, int in_flight) +void utp_socket_impl::do_ledbat(const int acked_bytes, const int delay + , const int in_flight) { INVARIANT_CHECK; @@ -3333,15 +3334,15 @@ void utp_socket_impl::do_ledbat(int acked_bytes, int delay, int in_flight) TORRENT_ASSERT(in_flight > 0); TORRENT_ASSERT(acked_bytes > 0); - int target_delay = m_sm->target_delay(); + const int target_delay = (std::max)(1, m_sm->target_delay()); // true if the upper layer is pushing enough data down the socket to be // limited by the cwnd. If this is not the case, we should not adjust cwnd. - bool cwnd_saturated = (m_bytes_in_flight + acked_bytes + m_mtu > (m_cwnd >> 16)); + const bool cwnd_saturated = (m_bytes_in_flight + acked_bytes + m_mtu > (m_cwnd >> 16)); // all of these are fixed points with 16 bits fraction portion - boost::int64_t window_factor = (boost::int64_t(acked_bytes) << 16) / in_flight; - boost::int64_t delay_factor = (boost::int64_t(target_delay - delay) << 16) / target_delay; + const boost::int64_t window_factor = (boost::int64_t(acked_bytes) << 16) / in_flight; + const boost::int64_t delay_factor = (boost::int64_t(target_delay - delay) << 16) / target_delay; boost::int64_t scaled_gain; if (delay >= target_delay)