diff --git a/include/msgpack/v1/vrefbuffer.hpp b/include/msgpack/v1/vrefbuffer.hpp index b80b7a6a..39bcd69c 100644 --- a/include/msgpack/v1/vrefbuffer.hpp +++ b/include/msgpack/v1/vrefbuffer.hpp @@ -58,6 +58,10 @@ public: :m_ref_size(std::max(ref_size, detail::packer_max_buffer_size + 1)), m_chunk_size(chunk_size) { + if((sizeof(chunk) + chunk_size) < chunk_size) { + throw std::bad_alloc(); + } + size_t nfirst = (sizeof(iovec) < 72/2) ? 72 / sizeof(iovec) : 8; @@ -71,11 +75,6 @@ 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(::malloc(sizeof(chunk) + chunk_size)); if(!c) { ::free(array); diff --git a/src/vrefbuffer.c b/src/vrefbuffer.c index ea493793..e03d832b 100644 --- a/src/vrefbuffer.c +++ b/src/vrefbuffer.c @@ -30,6 +30,10 @@ bool msgpack_vrefbuffer_init(msgpack_vrefbuffer* vbuf, ref_size > MSGPACK_PACKER_MAX_BUFFER_SIZE + 1 ? ref_size : MSGPACK_PACKER_MAX_BUFFER_SIZE + 1 ; + if((sizeof(msgpack_vrefbuffer_chunk) + chunk_size) < chunk_size) { + return false; + } + nfirst = (sizeof(struct iovec) < 72/2) ? 72 / sizeof(struct iovec) : 8; @@ -43,11 +47,6 @@ bool msgpack_vrefbuffer_init(msgpack_vrefbuffer* vbuf, vbuf->end = array + nfirst; vbuf->array = array; - if((sizeof(msgpack_vrefbuffer_chunk) + chunk_size) < chunk_size){ - free(array); - return false; - } - chunk = (msgpack_vrefbuffer_chunk*)malloc( sizeof(msgpack_vrefbuffer_chunk) + chunk_size); if(chunk == NULL) {