[C++] Additional security fixes in SimpleArray
Also, extend checked_add tests.
This commit is contained in:
Родитель
afc6e9c230
Коммит
42483908e2
|
@ -5,6 +5,7 @@
|
|||
|
||||
#include <limits>
|
||||
#include <stdexcept>
|
||||
#include <boost/static_assert.hpp>
|
||||
|
||||
namespace bond
|
||||
{
|
||||
|
@ -19,7 +20,9 @@ public:
|
|||
: _size(0),
|
||||
_capacity(N),
|
||||
_data(_insitu)
|
||||
{}
|
||||
{
|
||||
BOOST_STATIC_ASSERT(N != 0);
|
||||
}
|
||||
|
||||
~SimpleArray()
|
||||
{
|
||||
|
@ -38,6 +41,10 @@ public:
|
|||
|
||||
T pop()
|
||||
{
|
||||
if (_size == 0)
|
||||
{
|
||||
throw std::underflow_error("Can't pop empty array");
|
||||
}
|
||||
return _data[--_size];
|
||||
}
|
||||
|
||||
|
@ -72,7 +79,10 @@ private:
|
|||
void memfree()
|
||||
{
|
||||
if (_data != _insitu)
|
||||
{
|
||||
delete [] _data;
|
||||
_data = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
BOOST_STATIC_ASSERT(is_pod<T>::value);
|
||||
|
|
|
@ -18,6 +18,14 @@ struct CheckedAddUIntTest
|
|||
UT_AssertAreEqual(bond::detail::checked_add(x2, x3), x2 + x3);
|
||||
UT_AssertThrows(bond::detail::checked_add(x2, x4), std::overflow_error);
|
||||
UT_AssertAreEqual(bond::detail::checked_add(x3, x4), x3 + x4);
|
||||
|
||||
uint8_t y3 = 1;
|
||||
uint8_t y4 = 100;
|
||||
|
||||
UT_AssertThrows(bond::detail::checked_add(x1, y3), std::overflow_error);
|
||||
UT_AssertThrows(bond::detail::checked_add(x1, y4), std::overflow_error);
|
||||
UT_AssertAreEqual(bond::detail::checked_add(x2, y3), x2 + y3);
|
||||
UT_AssertThrows(bond::detail::checked_add(x2, y4), std::overflow_error);
|
||||
}
|
||||
};
|
||||
|
||||
|
|
Загрузка…
Ссылка в новой задаче