Skip to content

Commit c056026

Browse files
committed
Fix memory leaks
1 parent 0421dab commit c056026

File tree

4 files changed

+10
-8
lines changed

4 files changed

+10
-8
lines changed

include/msgpack/v1/vrefbuffer.hpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,10 @@ class vrefbuffer {
7171
m_end = array + nfirst;
7272
m_array = array;
7373

74-
7574
if((sizeof(chunk) + chunk_size) < chunk_size){
75+
::free(array);
7676
throw std::bad_alloc();
7777
}
78-
7978

8079
chunk* c = static_cast<chunk*>(::malloc(sizeof(chunk) + chunk_size));
8180
if(!c) {

src/vrefbuffer.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ bool msgpack_vrefbuffer_init(msgpack_vrefbuffer* vbuf,
4444
vbuf->array = array;
4545

4646
if((sizeof(msgpack_vrefbuffer_chunk) + chunk_size) < chunk_size){
47+
free(array);
4748
return false;
4849
}
4950

@@ -171,7 +172,7 @@ int msgpack_vrefbuffer_append_copy(msgpack_vrefbuffer* vbuf,
171172
int msgpack_vrefbuffer_migrate(msgpack_vrefbuffer* vbuf, msgpack_vrefbuffer* to)
172173
{
173174
size_t sz = vbuf->chunk_size;
174-
msgpack_vrefbuffer_chunk* empty = NULL;
175+
msgpack_vrefbuffer_chunk* empty;
175176

176177
if((sizeof(msgpack_vrefbuffer_chunk) + sz) < sz){
177178
return -1;

test/msgpack_c.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1354,7 +1354,7 @@ TEST(MSGPACKC, unpack_array_uint64)
13541354
}
13551355

13561356

1357-
TEST(MSGPACKC, vreff_buffer_overflow)
1357+
TEST(MSGPACKC, vref_buffer_overflow)
13581358
{
13591359
msgpack_vrefbuffer vbuf;
13601360
msgpack_vrefbuffer to;

test/msgpack_vref.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -266,10 +266,12 @@ TEST(MSGPACK, vrefbuffer_small_int64)
266266
}
267267

268268
TEST(MSGPACK, vref_buffer_overflow)
269-
{
269+
{
270+
size_t ref_size = 0;
270271
size_t chunk_size = std::numeric_limits<size_t>::max();
271272
char *buf = (char *)malloc(chunk_size);
272-
ASSERT_THROW(msgpack::vrefbuffer vbuf(0, chunk_size), std::bad_alloc);
273-
msgpack::vrefbuffer vbuf(0,0x1000);
274-
ASSERT_THROW(vbuf.append_copy(buf, chunk_size), std::bad_alloc);
273+
ASSERT_THROW(msgpack::vrefbuffer vbuf(ref_size, chunk_size), std::bad_alloc);
274+
// msgpack::vrefbuffer vbuf2(0, 0x1000);
275+
// ASSERT_THROW(vbuf2.append_copy(buf, chunk_size), std::bad_alloc);
276+
free(buf);
275277
}

0 commit comments

Comments
 (0)