From 521587c026f9f3ef9a5e975ed77b229bd567c3ce Mon Sep 17 00:00:00 2001 From: arvidn Date: Sat, 11 Nov 2017 16:33:48 +0100 Subject: [PATCH] make sure the cork destructor doesn't leak exceptions --- include/libtorrent/peer_connection.hpp | 19 +++++++++++++++++-- src/peer_connection.cpp | 7 ++++++- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/include/libtorrent/peer_connection.hpp b/include/libtorrent/peer_connection.hpp index ce68941d5..e502a2d33 100644 --- a/include/libtorrent/peer_connection.hpp +++ b/include/libtorrent/peer_connection.hpp @@ -680,6 +680,8 @@ namespace aux { std::shared_ptr self() { + TORRENT_ASSERT(!m_destructed); + TORRENT_ASSERT(m_in_use == 1337); TORRENT_ASSERT(!m_in_constructor); return shared_from_this(); } @@ -1202,8 +1204,21 @@ namespace aux { ~cork() { if (!m_need_uncork) return; - m_pc.m_channel_state[peer_connection::upload_channel] &= ~peer_info::bw_network; - m_pc.setup_send(); + try { + m_pc.m_channel_state[peer_connection::upload_channel] &= ~peer_info::bw_network; + m_pc.setup_send(); + } + catch (std::bad_alloc const&) { + m_pc.disconnect(make_error_code(boost::system::errc::not_enough_memory) + , operation_t::sock_write); + } + catch (boost::system::system_error const& err) { + m_pc.disconnect(err.code(), operation_t::sock_write); + } + catch (...) { + m_pc.disconnect(make_error_code(boost::system::errc::not_enough_memory) + , operation_t::sock_write); + } } private: peer_connection& m_pc; diff --git a/src/peer_connection.cpp b/src/peer_connection.cpp index bd9c1dde6..72ff3250c 100644 --- a/src/peer_connection.cpp +++ b/src/peer_connection.cpp @@ -4104,7 +4104,7 @@ namespace libtorrent { torrent_peer* self_peer = peer_info_struct(); #ifndef TORRENT_DISABLE_LOGGING - if (should_log(peer_log_alert::info)) + if (should_log(peer_log_alert::info)) try { switch (error) { @@ -4130,6 +4130,11 @@ namespace libtorrent { peer_log(peer_log_alert::info, "SHORT_LIVED_DISCONNECT", ""); } } + catch (std::exception const& err) + { + peer_log(peer_log_alert::info, "PEER_ERROR" ,"op: %d error: unknown error (failed with exception) %s" + , static_cast(op), err.what()); + } #endif if (!(m_channel_state[upload_channel] & peer_info::bw_network))