Fix memory leaks

This commit is contained in:
tbeu
2018-09-03 22:22:49 +02:00
parent 0421dabc1e
commit c056026dad
4 changed files with 10 additions and 8 deletions

View File

@@ -71,11 +71,10 @@ public:
m_end = array + nfirst;
m_array = array;
if((sizeof(chunk) + chunk_size) < chunk_size){
::free(array);
throw std::bad_alloc();
}
chunk* c = static_cast<chunk*>(::malloc(sizeof(chunk) + chunk_size));
if(!c) {

View File

@@ -44,6 +44,7 @@ bool msgpack_vrefbuffer_init(msgpack_vrefbuffer* vbuf,
vbuf->array = array;
if((sizeof(msgpack_vrefbuffer_chunk) + chunk_size) < chunk_size){
free(array);
return false;
}
@@ -171,7 +172,7 @@ int msgpack_vrefbuffer_append_copy(msgpack_vrefbuffer* vbuf,
int msgpack_vrefbuffer_migrate(msgpack_vrefbuffer* vbuf, msgpack_vrefbuffer* to)
{
size_t sz = vbuf->chunk_size;
msgpack_vrefbuffer_chunk* empty = NULL;
msgpack_vrefbuffer_chunk* empty;
if((sizeof(msgpack_vrefbuffer_chunk) + sz) < sz){
return -1;

View File

@@ -1354,7 +1354,7 @@ TEST(MSGPACKC, unpack_array_uint64)
}
TEST(MSGPACKC, vreff_buffer_overflow)
TEST(MSGPACKC, vref_buffer_overflow)
{
msgpack_vrefbuffer vbuf;
msgpack_vrefbuffer to;

View File

@@ -266,10 +266,12 @@ TEST(MSGPACK, vrefbuffer_small_int64)
}
TEST(MSGPACK, vref_buffer_overflow)
{
{
size_t ref_size = 0;
size_t chunk_size = std::numeric_limits<size_t>::max();
char *buf = (char *)malloc(chunk_size);
ASSERT_THROW(msgpack::vrefbuffer vbuf(0, chunk_size), std::bad_alloc);
msgpack::vrefbuffer vbuf(0,0x1000);
ASSERT_THROW(vbuf.append_copy(buf, chunk_size), std::bad_alloc);
ASSERT_THROW(msgpack::vrefbuffer vbuf(ref_size, chunk_size), std::bad_alloc);
// msgpack::vrefbuffer vbuf2(0, 0x1000);
// ASSERT_THROW(vbuf2.append_copy(buf, chunk_size), std::bad_alloc);
free(buf);
}