properly throw on failing to allocate memory in buffer and bitfield

This commit is contained in:
Arvid Norberg 2015-05-09 03:56:57 +00:00
parent a3a06ca45c
commit 3218931495
2 changed files with 15 additions and 5 deletions

View File

@ -309,14 +309,20 @@ namespace libtorrent
const int b = (bits + 31) / 32;
if (m_buf)
{
m_buf = static_cast<boost::uint32_t*>(std::realloc(m_buf-1, (b+1) * 4));
m_buf = m_buf + 1;
boost::uint32_t* tmp = static_cast<boost::uint32_t*>(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<boost::uint32_t*>(std::malloc((b+1) * 4));
m_buf = m_buf + 1;
boost::uint32_t* tmp = static_cast<boost::uint32_t*>(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)

View File

@ -217,7 +217,11 @@ public:
TORRENT_ASSERT(n > 0);
TORRENT_ASSERT(n < 0xffffffffu);
m_begin = static_cast<char*>(std::realloc(m_begin, n));
char* tmp = static_cast<char*>(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);
}