20
20
#include "rmem.h"
21
21
#include "extension_value_class.h"
22
22
#include <assert.h>
23
+ #include <limits.h>
23
24
24
25
#if !defined(HAVE_RB_PROC_CALL_WITH_BLOCK )
25
26
#define rb_proc_call_with_block (recv , argc , argv , block ) rb_funcallv(recv, rb_intern("call"), argc, argv)
26
27
#endif
27
28
28
29
static int RAW_TYPE_STRING = 256 ;
29
30
static int RAW_TYPE_BINARY = 257 ;
31
+ static int16_t INITIAL_BUFFER_CAPACITY_MAX = SHRT_MAX ;
30
32
31
33
static msgpack_rmem_t s_stack_rmem ;
32
34
@@ -37,6 +39,11 @@ static inline VALUE rb_hash_new_capa(long capa)
37
39
}
38
40
#endif
39
41
42
+ static inline int16_t initial_buffer_size (long size )
43
+ {
44
+ return (size > INITIAL_BUFFER_CAPACITY_MAX ) ? INITIAL_BUFFER_CAPACITY_MAX : size ;
45
+ }
46
+
40
47
void msgpack_unpacker_static_init (void )
41
48
{
42
49
assert (sizeof (msgpack_unpacker_stack_entry_t ) * MSGPACK_UNPACKER_STACK_CAPACITY <= MSGPACK_RMEM_PAGE_SIZE );
@@ -375,14 +382,14 @@ static int read_primitive(msgpack_unpacker_t* uk)
375
382
if (count == 0 ) {
376
383
return object_complete (uk , rb_ary_new ());
377
384
}
378
- return _msgpack_unpacker_stack_push (uk , STACK_TYPE_ARRAY , count , rb_ary_new2 (count ));
385
+ return _msgpack_unpacker_stack_push (uk , STACK_TYPE_ARRAY , count , rb_ary_new2 (initial_buffer_size ( count ) ));
379
386
380
387
SWITCH_RANGE (b , 0x80 , 0x8f ) // FixMap
381
388
int count = b & 0x0f ;
382
389
if (count == 0 ) {
383
390
return object_complete (uk , rb_hash_new ());
384
391
}
385
- return _msgpack_unpacker_stack_push (uk , STACK_TYPE_MAP_KEY , count * 2 , rb_hash_new_capa (count ));
392
+ return _msgpack_unpacker_stack_push (uk , STACK_TYPE_MAP_KEY , count * 2 , rb_hash_new_capa (initial_buffer_size ( count ) ));
386
393
387
394
SWITCH_RANGE (b , 0xc0 , 0xdf ) // Variable
388
395
switch (b ) {
@@ -605,7 +612,7 @@ static int read_primitive(msgpack_unpacker_t* uk)
605
612
if (count == 0 ) {
606
613
return object_complete (uk , rb_ary_new ());
607
614
}
608
- return _msgpack_unpacker_stack_push (uk , STACK_TYPE_ARRAY , count , rb_ary_new2 (count ));
615
+ return _msgpack_unpacker_stack_push (uk , STACK_TYPE_ARRAY , count , rb_ary_new2 (initial_buffer_size ( count ) ));
609
616
}
610
617
611
618
case 0xdd : // array 32
@@ -615,7 +622,7 @@ static int read_primitive(msgpack_unpacker_t* uk)
615
622
if (count == 0 ) {
616
623
return object_complete (uk , rb_ary_new ());
617
624
}
618
- return _msgpack_unpacker_stack_push (uk , STACK_TYPE_ARRAY , count , rb_ary_new2 (count ));
625
+ return _msgpack_unpacker_stack_push (uk , STACK_TYPE_ARRAY , count , rb_ary_new2 (initial_buffer_size ( count ) ));
619
626
}
620
627
621
628
case 0xde : // map 16
@@ -625,7 +632,7 @@ static int read_primitive(msgpack_unpacker_t* uk)
625
632
if (count == 0 ) {
626
633
return object_complete (uk , rb_hash_new ());
627
634
}
628
- return _msgpack_unpacker_stack_push (uk , STACK_TYPE_MAP_KEY , count * 2 , rb_hash_new_capa (count ));
635
+ return _msgpack_unpacker_stack_push (uk , STACK_TYPE_MAP_KEY , count * 2 , rb_hash_new_capa (initial_buffer_size ( count ) ));
629
636
}
630
637
631
638
case 0xdf : // map 32
@@ -635,7 +642,7 @@ static int read_primitive(msgpack_unpacker_t* uk)
635
642
if (count == 0 ) {
636
643
return object_complete (uk , rb_hash_new ());
637
644
}
638
- return _msgpack_unpacker_stack_push (uk , STACK_TYPE_MAP_KEY , count * 2 , rb_hash_new_capa (count ));
645
+ return _msgpack_unpacker_stack_push (uk , STACK_TYPE_MAP_KEY , count * 2 , rb_hash_new_capa (initial_buffer_size ( count ) ));
639
646
}
640
647
641
648
default :
0 commit comments