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); }