Skip to content

Commit d727658

Browse files
committed
Move overflow check up
1 parent 349c133 commit d727658

File tree

2 files changed

+8
-10
lines changed

2 files changed

+8
-10
lines changed

include/msgpack/v1/vrefbuffer.hpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ class vrefbuffer {
5858
:m_ref_size(std::max(ref_size, detail::packer_max_buffer_size + 1)),
5959
m_chunk_size(chunk_size)
6060
{
61+
if((sizeof(chunk) + chunk_size) < chunk_size) {
62+
throw std::bad_alloc();
63+
}
64+
6165
size_t nfirst = (sizeof(iovec) < 72/2) ?
6266
72 / sizeof(iovec) : 8;
6367

@@ -71,11 +75,6 @@ class vrefbuffer {
7175
m_end = array + nfirst;
7276
m_array = array;
7377

74-
if((sizeof(chunk) + chunk_size) < chunk_size){
75-
::free(array);
76-
throw std::bad_alloc();
77-
}
78-
7978
chunk* c = static_cast<chunk*>(::malloc(sizeof(chunk) + chunk_size));
8079
if(!c) {
8180
::free(array);

src/vrefbuffer.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ bool msgpack_vrefbuffer_init(msgpack_vrefbuffer* vbuf,
3030
ref_size > MSGPACK_PACKER_MAX_BUFFER_SIZE + 1 ?
3131
ref_size : MSGPACK_PACKER_MAX_BUFFER_SIZE + 1 ;
3232

33+
if((sizeof(msgpack_vrefbuffer_chunk) + chunk_size) < chunk_size) {
34+
return false;
35+
}
36+
3337
nfirst = (sizeof(struct iovec) < 72/2) ?
3438
72 / sizeof(struct iovec) : 8;
3539

@@ -43,11 +47,6 @@ bool msgpack_vrefbuffer_init(msgpack_vrefbuffer* vbuf,
4347
vbuf->end = array + nfirst;
4448
vbuf->array = array;
4549

46-
if((sizeof(msgpack_vrefbuffer_chunk) + chunk_size) < chunk_size){
47-
free(array);
48-
return false;
49-
}
50-
5150
chunk = (msgpack_vrefbuffer_chunk*)malloc(
5251
sizeof(msgpack_vrefbuffer_chunk) + chunk_size);
5352
if(chunk == NULL) {

0 commit comments

Comments
 (0)