Skip to content

Commit 2674e34

Browse files
authored
Merge pull request #547 from redboltz/fix_overflow
Fixed integer overflow and EXT size problem.
2 parents 0b7cabd + c5c3de8 commit 2674e34

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

src/unpack.c

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,8 +189,14 @@ static inline int template_callback_false(unpack_user* u, msgpack_object* o)
189189

190190
static inline int template_callback_array(unpack_user* u, unsigned int n, msgpack_object* o)
191191
{
192+
unsigned int size;
192193
o->type = MSGPACK_OBJECT_ARRAY;
193194
o->via.array.size = 0;
195+
size = n*sizeof(msgpack_object);
196+
if (size / sizeof(msgpack_object) != n) {
197+
// integer overflow
198+
return -1;
199+
}
194200
o->via.array.ptr = (msgpack_object*)msgpack_zone_malloc(u->z, n*sizeof(msgpack_object));
195201
if(o->via.array.ptr == NULL) { return -1; }
196202
return 0;
@@ -210,9 +216,15 @@ static inline int template_callback_array_item(unpack_user* u, msgpack_object* c
210216

211217
static inline int template_callback_map(unpack_user* u, unsigned int n, msgpack_object* o)
212218
{
219+
unsigned int size;
213220
o->type = MSGPACK_OBJECT_MAP;
214221
o->via.map.size = 0;
215-
o->via.map.ptr = (msgpack_object_kv*)msgpack_zone_malloc(u->z, n*sizeof(msgpack_object_kv));
222+
size = n*sizeof(msgpack_object_kv);
223+
if (size / sizeof(msgpack_object_kv) != n) {
224+
// integer overflow
225+
return -1;
226+
}
227+
o->via.map.ptr = (msgpack_object_kv*)msgpack_zone_malloc(u->z, size);
216228
if(o->via.map.ptr == NULL) { return -1; }
217229
return 0;
218230
}
@@ -255,6 +267,9 @@ static inline int template_callback_bin(unpack_user* u, const char* b, const cha
255267

256268
static inline int template_callback_ext(unpack_user* u, const char* b, const char* p, unsigned int l, msgpack_object* o)
257269
{
270+
if (l == 0) {
271+
return MSGPACK_UNPACK_PARSE_ERROR;
272+
}
258273
MSGPACK_UNUSED(u);
259274
MSGPACK_UNUSED(b);
260275
o->type = MSGPACK_OBJECT_EXT;

0 commit comments

Comments
 (0)