From 790eda0f2c1ada4d9b69ca04ec59749d15e82076 Mon Sep 17 00:00:00 2001 From: Marcos Pinto Date: Tue, 15 Jan 2008 08:46:24 +0000 Subject: [PATCH] lt sync 1947 --- .../include/libtorrent/bandwidth_manager.hpp | 31 ++++--------- .../include/libtorrent/peer_connection.hpp | 2 +- libtorrent/src/disk_io_thread.cpp | 6 ++- libtorrent/src/pe_crypto.cpp | 46 +++++++++++-------- libtorrent/src/peer_connection.cpp | 5 +- libtorrent/src/torrent.cpp | 3 +- 6 files changed, 48 insertions(+), 45 deletions(-) diff --git a/libtorrent/include/libtorrent/bandwidth_manager.hpp b/libtorrent/include/libtorrent/bandwidth_manager.hpp index 1e6a4dc56..70ed67c7c 100644 --- a/libtorrent/include/libtorrent/bandwidth_manager.hpp +++ b/libtorrent/include/libtorrent/bandwidth_manager.hpp @@ -144,7 +144,7 @@ struct bandwidth_manager // this is used by web seeds void request_bandwidth(intrusive_ptr peer , int blk - , bool non_prioritized) throw() + , bool non_prioritized) { mutex_t::scoped_lock l(m_mutex); INVARIANT_CHECK; @@ -214,11 +214,9 @@ struct bandwidth_manager private: - void add_history_entry(history_entry const& e) throw() + void add_history_entry(history_entry const& e) { -#ifndef NDEBUG try { -#endif INVARIANT_CHECK; m_history.push_front(e); m_current_quota += e.amount; @@ -230,17 +228,13 @@ private: m_history_timer.expires_at(e.expires_at); m_history_timer.async_wait(bind(&bandwidth_manager::on_history_expire, this, _1)); -#ifndef NDEBUG } - catch (std::exception&) { TORRENT_ASSERT(false); } -#endif + catch (std::exception&) {} } - void on_history_expire(asio::error_code const& e) throw() + void on_history_expire(asio::error_code const& e) { -#ifndef NDEBUG try { -#endif if (e) return; mutex_t::scoped_lock l(m_mutex); @@ -273,13 +267,8 @@ private: // means we can hand out more (in case there // are still consumers in line) if (!m_queue.empty()) hand_out_bandwidth(l); -#ifndef NDEBUG } - catch (std::exception&) - { - TORRENT_ASSERT(false); - } -#endif + catch (std::exception&) {} } void hand_out_bandwidth(boost::mutex::scoped_lock& l) throw() @@ -289,9 +278,8 @@ private: // to the loop further down on the callstack if (m_in_hand_out_bandwidth) return; m_in_hand_out_bandwidth = true; -#ifndef NDEBUG + try { -#endif INVARIANT_CHECK; ptime now(time_now()); @@ -406,11 +394,12 @@ private: } if (!q.empty()) m_queue.insert(m_queue.begin(), q.begin(), q.end()); if (!tmp.empty()) m_queue.insert(m_queue.begin(), tmp.begin(), tmp.end()); -#ifndef NDEBUG } catch (std::exception& e) - { TORRENT_ASSERT(false); }; -#endif + { + m_in_hand_out_bandwidth = false; + throw; + } m_in_hand_out_bandwidth = false; } diff --git a/libtorrent/include/libtorrent/peer_connection.hpp b/libtorrent/include/libtorrent/peer_connection.hpp index 60cb4bc17..29600ac94 100755 --- a/libtorrent/include/libtorrent/peer_connection.hpp +++ b/libtorrent/include/libtorrent/peer_connection.hpp @@ -224,7 +224,7 @@ namespace libtorrent void add_stat(size_type downloaded, size_type uploaded); // is called once every second by the main loop - void second_tick(float tick_interval) throw(); + void second_tick(float tick_interval); boost::shared_ptr get_socket() const { return m_socket; } tcp::endpoint const& remote() const { return m_remote; } diff --git a/libtorrent/src/disk_io_thread.cpp b/libtorrent/src/disk_io_thread.cpp index 886d5d891..e1cfbfe5f 100644 --- a/libtorrent/src/disk_io_thread.cpp +++ b/libtorrent/src/disk_io_thread.cpp @@ -333,7 +333,11 @@ namespace libtorrent catch (std::exception& e) { // std::cerr << "DISK THREAD: exception: " << e.what() << std::endl; - j.str = e.what(); + try + { + j.str = e.what(); + } + catch (std::exception&) {} ret = -1; } diff --git a/libtorrent/src/pe_crypto.cpp b/libtorrent/src/pe_crypto.cpp index 093bb1265..2e1a7e890 100644 --- a/libtorrent/src/pe_crypto.cpp +++ b/libtorrent/src/pe_crypto.cpp @@ -1,6 +1,6 @@ /* -Copyright (c) 2007, Un Shyam +Copyright (c) 2007, Un Shyam & Arvid Norberg All rights reserved. Redistribution and use in source and binary forms, with or without @@ -40,23 +40,33 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/pe_crypto.hpp" #include "libtorrent/assert.hpp" -namespace libtorrent { +namespace libtorrent +{ - // Set the prime P and the generator, generate local public key - DH_key_exchange::DH_key_exchange () + DH_key_exchange::DH_key_exchange() { - m_DH = DH_new (); + m_DH = DH_new(); + if (m_DH == 0) throw std::bad_alloc(); + + m_DH->p = BN_bin2bn(m_dh_prime, sizeof(m_dh_prime), NULL); + m_DH->g = BN_bin2bn(m_dh_generator, sizeof(m_dh_generator), NULL); + if (m_DH->p == 0 || m_DH->g == 0) + { + DH_free(m_DH); + throw std::bad_alloc(); + } - m_DH->p = BN_bin2bn (m_dh_prime, sizeof(m_dh_prime), NULL); - m_DH->g = BN_bin2bn (m_dh_generator, sizeof(m_dh_generator), NULL); m_DH->length = 160l; TORRENT_ASSERT(sizeof(m_dh_prime) == DH_size(m_DH)); - DH_generate_key (m_DH); // TODO Check != 0 - - TORRENT_ASSERT(m_DH->pub_key); + DH_generate_key(m_DH); + if (m_DH->pub_key == 0) + { + DH_free(m_DH); + throw std::bad_alloc(); + } // DH can generate key sizes that are smaller than the size of // P with exponentially decreasing probability, in which case @@ -79,24 +89,25 @@ namespace libtorrent { DH_key_exchange::~DH_key_exchange () { TORRENT_ASSERT(m_DH); - DH_free (m_DH); + DH_free(m_DH); } - char const* DH_key_exchange::get_local_key () const + char const* DH_key_exchange::get_local_key() const { return m_dh_local_key; } // compute shared secret given remote public key - void DH_key_exchange::compute_secret (char const* remote_pubkey) + void DH_key_exchange::compute_secret(char const* remote_pubkey) { TORRENT_ASSERT(remote_pubkey); BIGNUM* bn_remote_pubkey = BN_bin2bn ((unsigned char*)remote_pubkey, 96, NULL); + if (bn_remote_pubkey == 0) throw std::bad_alloc(); char dh_secret[96]; - int secret_size = DH_compute_key ( (unsigned char*)dh_secret, - bn_remote_pubkey, m_DH); // TODO Check for errors + int secret_size = DH_compute_key((unsigned char*)dh_secret + , bn_remote_pubkey, m_DH); if (secret_size != 96) { @@ -104,11 +115,10 @@ namespace libtorrent { std::fill(m_dh_secret, m_dh_secret + 96 - secret_size, 0); } std::copy(dh_secret, dh_secret + secret_size, m_dh_secret + 96 - secret_size); - - BN_free (bn_remote_pubkey); + BN_free(bn_remote_pubkey); } - char const* DH_key_exchange::get_secret () const + char const* DH_key_exchange::get_secret() const { return m_dh_secret; } diff --git a/libtorrent/src/peer_connection.cpp b/libtorrent/src/peer_connection.cpp index fa9bb4b9c..0cadaabf8 100755 --- a/libtorrent/src/peer_connection.cpp +++ b/libtorrent/src/peer_connection.cpp @@ -2182,7 +2182,7 @@ namespace libtorrent if (m_packet_size >= m_recv_pos) m_recv_buffer.resize(m_packet_size); } - void peer_connection::second_tick(float tick_interval) throw() + void peer_connection::second_tick(float tick_interval) { INVARIANT_CHECK; @@ -2359,8 +2359,7 @@ namespace libtorrent else if (buffer_size_watermark > 80 * 1024) buffer_size_watermark = 80 * 1024; while (!m_requests.empty() - && (send_buffer_size() + m_reading_bytes < buffer_size_watermark) - && !m_choked) + && (send_buffer_size() + m_reading_bytes < buffer_size_watermark)) { TORRENT_ASSERT(t->valid_metadata()); peer_request& r = m_requests.front(); diff --git a/libtorrent/src/torrent.cpp b/libtorrent/src/torrent.cpp index 12db1e59b..c56090f5d 100755 --- a/libtorrent/src/torrent.cpp +++ b/libtorrent/src/torrent.cpp @@ -2250,7 +2250,8 @@ namespace libtorrent // skip forward in the queue until we find a prioritized peer // or hit the front of it. queue_t::reverse_iterator i = m_bandwidth_queue[channel].rbegin(); - while (i != m_bandwidth_queue[channel].rend() && i->non_prioritized) ++i; + if (!non_prioritized) + while (i != m_bandwidth_queue[channel].rend() && i->non_prioritized) ++i; m_bandwidth_queue[channel].insert(i.base(), bw_queue_entry( p, block_size, non_prioritized)); }