File tree Expand file tree Collapse file tree 3 files changed +37
-1
lines changed Expand file tree Collapse file tree 3 files changed +37
-1
lines changed Original file line number Diff line number Diff line change @@ -71,6 +71,12 @@ class vrefbuffer {
7171 m_end = array + nfirst;
7272 m_array = array;
7373
74+
75+ if ((sizeof (chunk) + chunk_size) < chunk_size){
76+ throw std::bad_alloc ();
77+ }
78+
79+
7480 chunk* c = static_cast <chunk*>(::malloc (sizeof (chunk) + chunk_size));
7581 if (!c) {
7682 ::free (array);
@@ -141,7 +147,11 @@ class vrefbuffer {
141147 if (sz < len) {
142148 sz = len;
143149 }
144-
150+
151+ if (sizeof (chunk) + sz < sz){
152+ throw std::bad_alloc ();
153+ }
154+
145155 chunk* c = static_cast <chunk*>(::malloc (sizeof (chunk) + sz));
146156 if (!c) {
147157 throw std::bad_alloc ();
@@ -183,6 +193,10 @@ class vrefbuffer {
183193 {
184194 size_t sz = m_chunk_size;
185195
196+ if ((sizeof (chunk) + sz) < sz){
197+ throw std::bad_alloc ();
198+ }
199+
186200 chunk* empty = static_cast <chunk*>(::malloc (sizeof (chunk) + sz));
187201 if (!empty) {
188202 throw std::bad_alloc ();
Original file line number Diff line number Diff line change @@ -1352,3 +1352,16 @@ TEST(MSGPACKC, unpack_array_uint64)
13521352 EXPECT_EQ (0xFFF0000000000001LL , obj.via .array .ptr [0 ].via .u64 );
13531353 msgpack_zone_destroy (&z);
13541354}
1355+
1356+
1357+ TEST (MSGPACKC, vreff_buffer_overflow)
1358+ {
1359+ msgpack_vrefbuffer vbuf;
1360+ msgpack_vrefbuffer to;
1361+ size_t ref_size = 0 ;
1362+ size_t len = 0x1000 ;
1363+ size_t chunk_size = std::numeric_limits<size_t >::max ();
1364+ char *buf = (char *)malloc (len);
1365+ EXPECT_FALSE (msgpack_vrefbuffer_init (&vbuf, ref_size, chunk_size));
1366+ EXPECT_EQ (-1 , msgpack_vrefbuffer_migrate (&vbuf, &to));
1367+ }
Original file line number Diff line number Diff line change @@ -264,3 +264,12 @@ TEST(MSGPACK, vrefbuffer_small_int64)
264264 msgpack::vrefbuffer vbuf (0 , 0 );
265265 GEN_TEST_VREF (int64_t , vbuf);
266266}
267+
268+ TEST (MSGPACK, vref_buffer_overflow)
269+ {
270+ size_t chunk_size = std::numeric_limits<size_t >::max ();
271+ 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);
275+ }
You can’t perform that action at this time.
0 commit comments