From 3218931495034883ad11157e7752fd6e4b6b13cf Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Sat, 9 May 2015 03:56:57 +0000 Subject: [PATCH] properly throw on failing to allocate memory in buffer and bitfield --- include/libtorrent/bitfield.hpp | 14 ++++++++++---- include/libtorrent/buffer.hpp | 6 +++++- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/include/libtorrent/bitfield.hpp b/include/libtorrent/bitfield.hpp index 019c794af..62bac9f6e 100644 --- a/include/libtorrent/bitfield.hpp +++ b/include/libtorrent/bitfield.hpp @@ -309,14 +309,20 @@ namespace libtorrent const int b = (bits + 31) / 32; if (m_buf) { - m_buf = static_cast(std::realloc(m_buf-1, (b+1) * 4)); - m_buf = m_buf + 1; + boost::uint32_t* tmp = static_cast(std::realloc(m_buf-1, (b+1) * 4)); +#ifndef BOOST_NO_EXCEPTIONS + if (tmp == NULL) throw std::bad_alloc(); +#endif + m_buf = tmp + 1; m_buf[-1] = bits; } else if (bits > 0) { - m_buf = static_cast(std::malloc((b+1) * 4)); - m_buf = m_buf + 1; + boost::uint32_t* tmp = static_cast(std::malloc((b+1) * 4)); +#ifndef BOOST_NO_EXCEPTIONS + if (tmp == NULL) throw std::bad_alloc(); +#endif + m_buf = tmp + 1; m_buf[-1] = bits; } else if (m_buf != NULL) diff --git a/include/libtorrent/buffer.hpp b/include/libtorrent/buffer.hpp index 8570056fb..e54b4b4ec 100644 --- a/include/libtorrent/buffer.hpp +++ b/include/libtorrent/buffer.hpp @@ -217,7 +217,11 @@ public: TORRENT_ASSERT(n > 0); TORRENT_ASSERT(n < 0xffffffffu); - m_begin = static_cast(std::realloc(m_begin, n)); + char* tmp = static_cast(std::realloc(m_begin, n)); +#ifndef BOOST_NO_EXCEPTIONS + if (tmp == NULL) throw std::bad_alloc(); +#endif + m_begin = tmp; m_capacity = boost::uint32_t(n); }