wrap most instances of throw, and terminate on builds where exceptions are disabled (#1639)

wrap most instances of throw, and terminate on builds where exceptions are disabled
This commit is contained in:
Arvid Norberg 2017-01-29 15:37:42 -05:00 committed by GitHub
parent 36858eae85
commit 29db3de6c0
17 changed files with 116 additions and 129 deletions

View File

@ -196,6 +196,7 @@ nobase_include_HEADERS = \
aux_/numeric_cast.hpp \
aux_/unique_ptr.hpp \
aux_/alloca.hpp \
aux_/throw.hpp \
aux_/typed_span.hpp \
\
extensions/smart_ban.hpp \

View File

@ -35,6 +35,8 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/config.hpp"
#include "libtorrent/span.hpp"
#include "libtorrent/aux_/throw.hpp"
#include <fcntl.h>
namespace libtorrent { namespace aux {
@ -46,11 +48,7 @@ namespace libtorrent { namespace aux {
{
if (m_fd < 0)
{
#ifndef BOOST_NO_EXCEPTIONS
throw system_error(error_code(errno, system_category()));
#else
std::terminate();
#endif
throw_ex<system_error>(error_code(errno, system_category()));
}
}
dev_random(dev_random const&) = delete;
@ -61,11 +59,7 @@ namespace libtorrent { namespace aux {
std::int64_t const ret = ::read(m_fd, buffer.data(), buffer.size());
if (ret != int(buffer.size()))
{
#ifndef BOOST_NO_EXCEPTIONS
throw system_error(errors::no_entropy);
#else
std::terminate();
#endif
throw_ex<system_error>(errors::no_entropy);
}
}

View File

@ -0,0 +1,54 @@
/*
Copyright (c) 2017, Arvid Norberg
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the distribution.
* Neither the name of the author nor the names of its
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef TORRENT_THROW_HPP_INCLUDED
#define TORRENT_THROW_HPP_INCLUDED
#include <utility> // for forward()
#include "libtorrent/config.hpp"
namespace libtorrent { namespace aux
{
template <typename T, typename... Args>
void TORRENT_NO_RETURN throw_ex(Args&&... args)
{
#ifdef BOOST_NO_EXCEPTIONS
std::terminate();
#else
throw T(std::forward<Args>(args)...);
#endif
}
}}
#endif

View File

@ -35,9 +35,12 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/config.hpp"
#include "libtorrent/error_code.hpp"
#include "libtorrent/aux_/throw.hpp"
#include "libtorrent/aux_/disable_warnings_push.hpp"
#include <windows.h>
#include <wincrypt.h>
#include "libtorrent/aux_/disable_warnings_pop.hpp"
namespace libtorrent { namespace aux
{
@ -48,11 +51,7 @@ namespace libtorrent { namespace aux
if (CryptAcquireContext(&ret, nullptr, nullptr, provider_type
, CRYPT_VERIFYCONTEXT) == false)
{
#ifndef BOOST_NO_EXCEPTIONS
throw system_error(error_code(GetLastError(), system_category()));
#else
std::terminate();
#endif
throw_ex<system_error>(error_code(GetLastError(), system_category()));
}
return ret;
}
@ -63,11 +62,7 @@ namespace libtorrent { namespace aux
if (!CryptGenRandom(provider, int(buffer.size())
, reinterpret_cast<BYTE*>(buffer.data())))
{
#ifndef BOOST_NO_EXCEPTIONS
throw system_error(error_code(GetLastError(), system_category()));
#else
std::terminate();
#endif
throw_ex<system_error>(error_code(GetLastError(), system_category()));
}
}
@ -98,11 +93,7 @@ namespace libtorrent { namespace aux
{
if (CryptHashData(m_hash, reinterpret_cast<BYTE const*>(data.data()), int(data.size()), 0) == false)
{
#ifndef BOOST_NO_EXCEPTIONS
throw system_error(error_code(GetLastError(), system_category()));
#else
std::terminate();
#endif
throw_ex<system_error>(error_code(GetLastError(), system_category()));
}
}
@ -112,11 +103,7 @@ namespace libtorrent { namespace aux
if (CryptGetHashParam(m_hash, HP_HASHVAL
, reinterpret_cast<BYTE*>(digest), &size, 0) == false)
{
#ifndef BOOST_NO_EXCEPTIONS
throw system_error(error_code(GetLastError(), system_category()));
#else
std::terminate();
#endif
throw_ex<system_error>(error_code(GetLastError(), system_category()));
}
TORRENT_ASSERT(size == DWORD(digest_size));
}
@ -126,11 +113,7 @@ namespace libtorrent { namespace aux
HCRYPTHASH ret;
if (CryptCreateHash(get_provider(), AlgId, 0, 0, &ret) == false)
{
#ifndef BOOST_NO_EXCEPTIONS
throw system_error(error_code(GetLastError(), system_category()));
#else
std::terminate();
#endif
throw_ex<system_error>(error_code(GetLastError(), system_category()));
}
return ret;
}
@ -140,11 +123,7 @@ namespace libtorrent { namespace aux
HCRYPTHASH ret;
if (CryptDuplicateHash(h.m_hash, 0, 0, &ret) == false)
{
#ifndef BOOST_NO_EXCEPTIONS
throw system_error(error_code(GetLastError(), system_category()));
#else
std::terminate();
#endif
throw_ex<system_error>(error_code(GetLastError(), system_category()));
}
return ret;
}

View File

@ -41,6 +41,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/invariant_check.hpp"
#include "libtorrent/assert.hpp"
#include "libtorrent/span.hpp"
#include "libtorrent/aux_/throw.hpp"
#if defined __GLIBC__
#include <malloc.h>
@ -73,15 +74,10 @@ public:
// for a variation of "malloc_size()"
size = (size + 7) & (~std::size_t(0x7));
// we have to use malloc here, to be compatible with the fancy query
// functions below
m_begin = static_cast<char*>(std::malloc(size));
if (m_begin == nullptr)
{
#ifndef BOOST_NO_EXCEPTIONS
throw std::bad_alloc();
#else
std::terminate();
#endif
}
if (m_begin == nullptr) aux::throw_ex<std::bad_alloc>();
// the actual allocation may be larger than we requested. If so, let the
// user take advantage of every single byte

View File

@ -326,6 +326,8 @@ POSSIBILITY OF SUCH DAMAGE.
// that could be marked noreturn.
#if defined __clang__ || defined __GNUC__
#define TORRENT_NO_RETURN __attribute((noreturn))
#elif _MSC_VER
#define TORRENT_NO_RETURN __declspec(noreturn)
#else
#define TORRENT_NO_RETURN
#endif

View File

@ -74,7 +74,7 @@ namespace libtorrent
class torrent;
#ifndef BOOST_NO_EXCEPTIONS
void throw_invalid_handle() TORRENT_NO_RETURN;
void TORRENT_NO_RETURN throw_invalid_handle();
#endif
using std::shared_ptr;

View File

@ -36,6 +36,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/string_util.hpp" // for allocate_string_copy
#include "libtorrent/disk_buffer_holder.hpp"
#include "libtorrent/aux_/alloca.hpp"
#include "libtorrent/aux_/throw.hpp"
#include "libtorrent/error_code.hpp"
#include "libtorrent/error.hpp"
#include "libtorrent/file_pool.hpp"
@ -1663,7 +1664,7 @@ namespace libtorrent
bool exceeded = false;
disk_buffer_holder buffer(*this, m_disk_cache.allocate_buffer(exceeded, o, "receive buffer"));
if (!buffer) throw std::bad_alloc();
if (!buffer) aux::throw_ex<std::bad_alloc>();
std::memcpy(buffer.get(), buf, r.length);
disk_io_job* j = allocate_job(disk_io_job::write);

View File

@ -43,6 +43,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/entry.hpp"
#include "libtorrent/hex.hpp"
#include "libtorrent/string_util.hpp"
#include "libtorrent/aux_/throw.hpp"
namespace libtorrent
{
@ -71,14 +72,8 @@ namespace libtorrent
namespace
{
TORRENT_NO_RETURN inline void throw_error()
{
#ifndef BOOST_NO_EXCEPTIONS
throw system_error(errors::invalid_entry_type);
#else
std::terminate();
#endif
}
inline void TORRENT_NO_RETURN throw_error()
{ aux::throw_ex<system_error>(errors::invalid_entry_type); }
template <class T>
void call_destructor(T* o)

View File

@ -38,6 +38,8 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/aux_/disable_warnings_pop.hpp"
#endif
#include "libtorrent/aux_/throw.hpp"
using namespace std::placeholders;
namespace libtorrent
@ -54,12 +56,7 @@ namespace libtorrent
#if defined TORRENT_BUILD_SIMULATOR
TORRENT_UNUSED(ios);
#elif defined TORRENT_WINDOWS
if (!m_hnd.is_open())
#ifndef BOOST_NO_EXCEPTIONS
throw system_error(WSAGetLastError(), system_category());
#else
std::terminate();
#endif // BOOST_NO_EXCEPTIONS
if (!m_hnd.is_open()) aux::throw_ex<system_error>(WSAGetLastError(), system_category());
m_ovl.hEvent = m_hnd.native_handle();
#elif !TORRENT_USE_NETLINK
TORRENT_UNUSED(ios);

View File

@ -49,6 +49,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/random.hpp"
#include <libtorrent/assert.hpp>
#include <libtorrent/aux_/time.hpp>
#include "libtorrent/aux_/throw.hpp"
#include "libtorrent/alert_types.hpp" // for dht_lookup
#include "libtorrent/performance_counters.hpp" // for counters
@ -1187,11 +1188,7 @@ node::protocol_descriptor const& node::map_protocol_to_descriptor(udp protocol)
}
TORRENT_ASSERT_FAIL();
#ifndef BOOST_NO_EXCEPTIONS
throw std::out_of_range("unknown protocol");
#else
std::terminate();
#endif
aux::throw_ex<std::out_of_range>("unknown protocol");
}
} } // namespace libtorrent::dht

View File

@ -33,6 +33,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/magnet_uri.hpp"
#include "libtorrent/session.hpp"
#include "libtorrent/aux_/escape_string.hpp"
#include "libtorrent/aux_/throw.hpp"
#include "libtorrent/torrent_status.hpp"
#include "libtorrent/torrent_info.hpp"
#include "libtorrent/announce_entry.hpp"
@ -160,7 +161,7 @@ namespace libtorrent
{
error_code ec;
torrent_handle ret = add_magnet_uri_deprecated(ses, uri, p, ec);
if (ec) throw system_error(ec);
if (ec) aux::throw_ex<system_error>(ec);
return ret;
}
#endif // BOOST_NO_EXCEPTIONS

View File

@ -34,23 +34,22 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/random.hpp"
#include "libtorrent/error_code.hpp"
#include "libtorrent/aux_/openssl.hpp"
#include "libtorrent/aux_/disable_warnings_push.hpp"
#include "libtorrent/aux_/throw.hpp"
#if TORRENT_USE_CRYPTOAPI
#include "libtorrent/aux_/win_crypto_provider.hpp"
#elif defined TORRENT_USE_LIBCRYPTO
#include "libtorrent/aux_/disable_warnings_push.hpp"
extern "C" {
#include <openssl/rand.h>
#include <openssl/err.h>
}
#include "libtorrent/aux_/disable_warnings_pop.hpp"
#endif
#include "libtorrent/aux_/disable_warnings_pop.hpp"
#if TORRENT_USE_DEV_RANDOM
#include "libtorrent/aux_/dev_random.hpp"
#endif
@ -94,14 +93,7 @@ namespace libtorrent
int r = RAND_bytes(reinterpret_cast<unsigned char*>(buffer.data())
, int(buffer.size()));
if (r != 1)
{
#ifndef BOOST_NO_EXCEPTIONS
throw system_error(errors::no_entropy);
#else
std::terminate();
#endif
}
if (r != 1) aux::throw_ex<system_error>(errors::no_entropy);
#else
// fallback

View File

@ -33,6 +33,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/session_handle.hpp"
#include "libtorrent/aux_/session_impl.hpp"
#include "libtorrent/aux_/session_call.hpp"
#include "libtorrent/aux_/throw.hpp"
#include "libtorrent/torrent.hpp"
#include "libtorrent/lazy_entry.hpp"
#include "libtorrent/peer_class.hpp"
@ -321,7 +322,7 @@ namespace libtorrent
error_code ec;
auto ecr = std::ref(ec);
torrent_handle r = sync_call_ret<torrent_handle>(&session_impl::add_torrent, p, ecr);
if (ec) throw system_error(ec);
if (ec) aux::throw_ex<system_error>(ec);
return r;
}
#endif
@ -677,7 +678,7 @@ namespace libtorrent
TORRENT_ASSERT(ret == 0);
#ifndef BOOST_NO_EXCEPTIONS
if (ret != 0) throw system_error(ec);
if (ret != 0) aux::throw_ex<system_error>(ec);
#endif
sync_call(&session_impl::load_state, &e, flags);
}
@ -704,7 +705,7 @@ namespace libtorrent
TORRENT_ASSERT(ret == 0);
#ifndef BOOST_NO_EXCEPTIONS
if (ret != 0) throw system_error(ec);
if (ret != 0) aux::throw_ex<system_error>(ec);
#endif
sync_call(&session_impl::load_state, &e, flags);
}

View File

@ -43,6 +43,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/entry.hpp"
#include "libtorrent/aux_/session_impl.hpp"
#include "libtorrent/aux_/session_call.hpp"
#include "libtorrent/aux_/throw.hpp"
#include "libtorrent/invariant_check.hpp"
#include "libtorrent/utf8.hpp"
#include "libtorrent/announce_entry.hpp"
@ -57,7 +58,7 @@ namespace libtorrent
{
#ifndef BOOST_NO_EXCEPTIONS
void throw_invalid_handle()
void TORRENT_NO_RETURN throw_invalid_handle()
{
throw system_error(errors::invalid_torrent_handle);
}
@ -67,14 +68,7 @@ namespace libtorrent
void torrent_handle::async_call(Fun f, Args&&... a) const
{
std::shared_ptr<torrent> t = m_torrent.lock();
if (!t)
{
#ifndef BOOST_NO_EXCEPTIONS
throw_invalid_handle();
#else
std::terminate();
#endif
}
if (!t) aux::throw_ex<system_error>(errors::invalid_torrent_handle);
session_impl& ses = static_cast<session_impl&>(t->session());
ses.get_io_service().dispatch([=,&ses] ()
{
@ -101,14 +95,7 @@ namespace libtorrent
void torrent_handle::sync_call(Fun f, Args&&... a) const
{
std::shared_ptr<torrent> t = m_torrent.lock();
if (!t)
{
#ifndef BOOST_NO_EXCEPTIONS
throw_invalid_handle();
#else
std::terminate();
#endif
}
if (!t) aux::throw_ex<system_error>(errors::invalid_torrent_handle);
session_impl& ses = static_cast<session_impl&>(t->session());
// this is the flag to indicate the call has completed

View File

@ -46,6 +46,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/aux_/escape_string.hpp" // maybe_url_encode
#include "libtorrent/aux_/merkle.hpp" // for merkle_*
#include "libtorrent/aux_/time.hpp"
#include "libtorrent/aux_/throw.hpp"
#include "libtorrent/add_torrent_params.hpp"
#include "libtorrent/magnet_uri.hpp"
#include "libtorrent/announce_entry.hpp"
@ -801,15 +802,11 @@ namespace libtorrent
error_code ec;
if (bdecode(buf.first, buf.first + buf.second, e, ec) != 0)
{
#ifndef BOOST_NO_EXCEPTIONS
throw system_error(ec);
#else
return;
#endif
aux::throw_ex<system_error>(ec);
}
#ifndef BOOST_NO_EXCEPTIONS
if (!parse_torrent_file(e, ec, 0))
throw system_error(ec);
aux::throw_ex<system_error>(ec);
#else
parse_torrent_file(e, ec, 0);
#endif
@ -827,14 +824,14 @@ namespace libtorrent
if (tmp.empty() || bdecode(&tmp[0], &tmp[0] + tmp.size(), e, ec) != 0)
{
#ifndef BOOST_NO_EXCEPTIONS
throw system_error(ec);
aux::throw_ex<system_error>(ec);
#else
return;
#endif
}
#ifndef BOOST_NO_EXCEPTIONS
if (!parse_torrent_file(e, ec, 0))
throw system_error(ec);
aux::throw_ex<system_error>(ec);
#else
parse_torrent_file(e, ec, 0);
#endif
@ -848,7 +845,7 @@ namespace libtorrent
{
error_code ec;
if (!parse_torrent_file(torrent_file, ec, flags))
throw system_error(ec);
aux::throw_ex<system_error>(ec);
INVARIANT_CHECK;
}
@ -860,10 +857,10 @@ namespace libtorrent
error_code ec;
bdecode_node e;
if (bdecode(buffer, buffer + size, e, ec) != 0)
throw system_error(ec);
aux::throw_ex<system_error>(ec);
if (!parse_torrent_file(e, ec, flags))
throw system_error(ec);
aux::throw_ex<system_error>(ec);
INVARIANT_CHECK;
}
@ -874,14 +871,14 @@ namespace libtorrent
std::vector<char> buf;
error_code ec;
int ret = load_file(filename, buf, ec);
if (ret < 0) throw system_error(ec);
if (ret < 0) aux::throw_ex<system_error>(ec);
bdecode_node e;
if (buf.empty() || bdecode(&buf[0], &buf[0] + buf.size(), e, ec) != 0)
throw system_error(ec);
aux::throw_ex<system_error>(ec);
if (!parse_torrent_file(e, ec, flags))
throw system_error(ec);
aux::throw_ex<system_error>(ec);
INVARIANT_CHECK;
}
@ -894,14 +891,14 @@ namespace libtorrent
std::vector<char> buf;
error_code ec;
int ret = load_file(wchar_utf8(filename), buf, ec);
if (ret < 0) throw system_error(ec);
if (ret < 0) aux::throw_ex<system_error>(ec);
bdecode_node e;
if (buf.empty() || bdecode(&buf[0], &buf[0] + buf.size(), e, ec) != 0)
throw system_error(ec);
aux::throw_ex<system_error>(ec);
if (!parse_torrent_file(e, ec, flags))
throw system_error(ec);
aux::throw_ex<system_error>(ec);
INVARIANT_CHECK;
}

View File

@ -41,6 +41,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/assert.hpp"
#include "libtorrent/error_code.hpp"
#include "libtorrent/ConvertUTF.h"
#include "libtorrent/aux_/throw.hpp"
#ifdef __clang__
@ -240,11 +241,7 @@ namespace libtorrent
{
error_code ec;
std::wstring ret = utf8_wchar(wide, ec);
#ifndef BOOST_NO_EXCEPTIONS
if (ec) throw system_error(ec);
#else
if (ec) std::terminate();
#endif
if (ec) aux::throw_ex<system_error>(ec);
return ret;
}
@ -267,11 +264,7 @@ namespace libtorrent
{
error_code ec;
std::string ret = wchar_utf8(wide, ec);
#ifndef BOOST_NO_EXCEPTIONS
if (ec) throw system_error(ec);
#else
if (ec) std::terminate();
#endif
if (ec) aux::throw_ex<system_error>(ec);
return ret;
}
}