msgpack 1.8.3 → 1.8.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ed5705d96741dbe59f61d59de79d133dfead8636cbf8c9db51b684c425a52e34
4
- data.tar.gz: 6fa2a89acb1a2ae0db5068fa59b483d2292e0b9ad681f9c3c058cf164dd28385
3
+ metadata.gz: 65e7539211e49d3c0eeef7b22f588fc111cf0d3574a6f204e950ced43addc51a
4
+ data.tar.gz: 3fa655fa372b102bd136d0cf61c84f68bd4d9e0836cb14891d95bc2c239f0b01
5
5
  SHA512:
6
- metadata.gz: 8edfe983aaee5230a1c1547e45894403c9c4435a65bbe4d88b4e9a800bb31467ad867526f4d54baf2cd6b7738cd9458104fc0caa772bba0dcbcb25c427002218
7
- data.tar.gz: 3d282debd040b4f9b0f2a15c2c34068b64d01c696d1756f235c63078e54d5fcc2d1896f1b32321aa2c70ec67b8205eb608e841aff60eecd78a651e614016eac3
6
+ metadata.gz: 8588f17a3fa9eec124a7ef874322fcf1aa90318cda81e463f3b7186267b3c82ec3ecf6fdd514a9d265cb40d781b7718551aa8278be25540fc9aaaaec085e851b
7
+ data.tar.gz: b224acfb0d31868e42e684e1263b11fed097054a359fc3437e1185130874e5f635dbb386b43b509b84c99caae86799dbf9517fc1e4ad5849788d824dab47699c
data/ChangeLog CHANGED
@@ -1,3 +1,27 @@
1
+ 2026-10-30 1.8.7
2
+
3
+ * Fix the Java version of the unpacker to never preallocate objects larger than
4
+ the buffered size (similar to the fix for the C unpacker in 1.8.1).
5
+
6
+ 2026-10-27 1.8.6
7
+
8
+ * Fix use-after-free when a recursive extension proc rescues, skips or resets.
9
+ * Speed up decoding of oversized integers.
10
+ * Avoid excessive memory allocation from crafted container headers.
11
+
12
+ 2026-09-11 1.8.5
13
+
14
+ * Prevent stack depth underflow when skipping from recursive unpacker.
15
+ * Fix `Unpacker#skip_nil` to properly consume the buffer.
16
+ * Fix some small difference in Factory registration of the Java implementation.
17
+ * Fix some Factory/ext_type optimization edge cases.
18
+
19
+ 2026-07-28 1.8.4
20
+
21
+ * Fix `MessagePack::Buffer` to stop using already released memory page.
22
+ This could have caused data to leak across buffers when using the MessagePack::Buffer API
23
+ directly.
24
+
1
25
  2026-06-10 1.8.3
2
26
 
3
27
  * Fix an integer overflow when parsing maps.
@@ -6,7 +30,7 @@
6
30
 
7
31
  * Fix `Buffer#clear` to properly reset memory chunks before adding them back to the pool.
8
32
  This could have caused data to leak across buffers when using the MessagePack::Buffer API
9
- directly. [CVE-PENDING].
33
+ directly. [CVE-2026-54522].
10
34
 
11
35
  2026-05-28 1.8.1
12
36
 
@@ -137,11 +137,15 @@ public class Decoder implements Iterator<IRubyObject> {
137
137
  }
138
138
 
139
139
  private IRubyObject consumeArray(int size) {
140
- IRubyObject[] elements = new IRubyObject[size];
140
+ int preallocatedSize = size;
141
+ if (preallocatedSize > buffer.remaining()) {
142
+ preallocatedSize = buffer.remaining();
143
+ }
144
+ RubyArray array = runtime.newArray(preallocatedSize);
141
145
  for (int i = 0; i < size; i++) {
142
- elements[i] = next();
146
+ array.push(next());
143
147
  }
144
- return runtime.newArray(elements);
148
+ return array;
145
149
  }
146
150
 
147
151
  private IRubyObject consumeHash(int size) {
@@ -184,9 +188,12 @@ public class Decoder implements Iterator<IRubyObject> {
184
188
  }
185
189
 
186
190
  private byte[] readBytes(int size) {
187
- byte[] payload = new byte[size];
188
- buffer.get(payload);
189
- return payload;
191
+ if (buffer.remaining() >= size) {
192
+ byte[] payload = new byte[size];
193
+ buffer.get(payload);
194
+ return payload;
195
+ }
196
+ throw new BufferUnderflowException();
190
197
  }
191
198
 
192
199
  @Override
@@ -59,6 +59,7 @@ public class Factory extends RubyObject {
59
59
  Factory clone = (Factory)super.dup();
60
60
  clone.extensionRegistry = extensionRegistry();
61
61
  clone.hasSymbolExtType = hasSymbolExtType;
62
+ clone.hasBigIntExtType = hasBigIntExtType;
62
63
  return clone;
63
64
  }
64
65
 
@@ -110,8 +111,8 @@ public class Factory extends RubyObject {
110
111
 
111
112
  extensionRegistry.put(extModule, (int) typeId, recursive, packerProc, unpackerProc);
112
113
 
113
- if (extModule == runtime.getSymbol() && !packerProc.isNil()) {
114
- hasSymbolExtType = true;
114
+ if (extModule == runtime.getSymbol()) {
115
+ hasSymbolExtType = packerProc != null && !packerProc.isNil();
115
116
  }
116
117
 
117
118
  if (options != null) {
@@ -122,6 +123,8 @@ public class Factory extends RubyObject {
122
123
  } else {
123
124
  throw runtime.newArgumentError("oversized_integer_extension: true is only for Integer class");
124
125
  }
126
+ } else if (extModule == runtime.getModule("Integer")) {
127
+ hasBigIntExtType = false;
125
128
  }
126
129
  }
127
130
 
data/ext/msgpack/buffer.c CHANGED
@@ -127,6 +127,15 @@ void msgpack_buffer_mark(void *ptr)
127
127
 
128
128
  bool _msgpack_buffer_shift_chunk(msgpack_buffer_t* b)
129
129
  {
130
+ if(b->rmem_owner == &b->head->mem) {
131
+ /* the chunk that owns the rmem page is going away and takes the page
132
+ * with it. don't carve the remaining space out of a page that is
133
+ * already back in the pool. */
134
+ b->rmem_end = NULL;
135
+ b->rmem_last = NULL;
136
+ b->rmem_owner = NULL;
137
+ }
138
+
130
139
  _msgpack_buffer_chunk_destroy(b->head);
131
140
 
132
141
  if(b->head == &b->tail) {
@@ -264,6 +273,18 @@ static inline msgpack_buffer_chunk_t* _msgpack_buffer_alloc_new_chunk(msgpack_bu
264
273
  return chunk;
265
274
  }
266
275
 
276
+ /* b->tail is copied into nc and then rebuilt, so the rmem page pointer moves
277
+ * to nc. the owner has to follow it: if it kept pointing at b->tail.mem, the
278
+ * transfer in _msgpack_buffer_chunk_malloc would degenerate into a
279
+ * self-assignment and the page would be released by nc while the rebuilt tail
280
+ * is still reading from it. */
281
+ static inline void _msgpack_buffer_transfer_rmem_owner(msgpack_buffer_t* b, msgpack_buffer_chunk_t* nc)
282
+ {
283
+ if(b->rmem_owner == &b->tail.mem) {
284
+ b->rmem_owner = &nc->mem;
285
+ }
286
+ }
287
+
267
288
  static inline void _msgpack_buffer_add_new_chunk(msgpack_buffer_t* b)
268
289
  {
269
290
  if(b->head == &b->tail) {
@@ -275,6 +296,7 @@ static inline void _msgpack_buffer_add_new_chunk(msgpack_buffer_t* b)
275
296
  msgpack_buffer_chunk_t* nc = _msgpack_buffer_alloc_new_chunk(b);
276
297
 
277
298
  *nc = b->tail;
299
+ _msgpack_buffer_transfer_rmem_owner(b, nc);
278
300
  b->head = nc;
279
301
  nc->next = &b->tail;
280
302
 
@@ -295,6 +317,7 @@ static inline void _msgpack_buffer_add_new_chunk(msgpack_buffer_t* b)
295
317
 
296
318
  /* rebuild tail */
297
319
  *nc = b->tail;
320
+ _msgpack_buffer_transfer_rmem_owner(b, nc);
298
321
  before_tail->next = nc;
299
322
  nc->next = &b->tail;
300
323
  }
data/ext/msgpack/buffer.h CHANGED
@@ -237,14 +237,27 @@ void _msgpack_buffer_append_long_string(msgpack_buffer_t* b, VALUE string);
237
237
 
238
238
  static inline size_t msgpack_buffer_append_string(msgpack_buffer_t* b, VALUE string)
239
239
  {
240
- size_t length;
241
- char *ptr;
242
- RSTRING_GETMEM(string, ptr, length);
240
+ size_t length = RSTRING_LEN(string);
243
241
 
244
242
  if(length > b->write_reference_threshold) {
245
243
  _msgpack_buffer_append_long_string(b, string);
246
- } else {
247
- msgpack_buffer_append(b, ptr, length);
244
+ } else if(length > 0) {
245
+ /* Making room can run io.write or trigger GC, either of which may free
246
+ * or shrink the string, so reserve the space first and only then read
247
+ * its pointer. */
248
+ msgpack_buffer_ensure_writable(b, length);
249
+ char *ptr;
250
+ size_t current;
251
+ RSTRING_GETMEM(string, ptr, current);
252
+ if(current > length) {
253
+ current = length;
254
+ }
255
+ memcpy(b->tail.last, ptr, current);
256
+ if(current < length) {
257
+ memset(b->tail.last + current, 0, length - current);
258
+ }
259
+ b->tail.last += length;
260
+ RB_GC_GUARD(string);
248
261
  }
249
262
 
250
263
  return length;
@@ -121,7 +121,10 @@ static VALUE Factory_dup(VALUE self)
121
121
  msgpack_factory_t *fc = Factory_get(self);
122
122
  msgpack_factory_t *cloned_fc = Factory_get(clone);
123
123
 
124
+ cloned_fc->has_bigint_ext_type = fc->has_bigint_ext_type;
124
125
  cloned_fc->has_symbol_ext_type = fc->has_symbol_ext_type;
126
+ cloned_fc->optimized_symbol_ext_type = fc->optimized_symbol_ext_type;
127
+ cloned_fc->symbol_ext_type = fc->symbol_ext_type;
125
128
  cloned_fc->pkrg = fc->pkrg;
126
129
  msgpack_unpacker_ext_registry_borrow(fc->ukrg, &cloned_fc->ukrg);
127
130
  msgpack_packer_ext_registry_dup(clone, &fc->pkrg, &cloned_fc->pkrg);
@@ -230,12 +233,13 @@ static VALUE Factory_register_type_internal(VALUE self, VALUE rb_ext_type, VALUE
230
233
  }
231
234
 
232
235
  if(ext_module == rb_cSymbol) {
233
- if(NIL_P(options) || RTEST(rb_hash_aref(options, ID2SYM(rb_intern("packer"))))) {
234
- fc->has_symbol_ext_type = true;
235
- }
236
- if(RTEST(options) && RTEST(rb_hash_aref(options, ID2SYM(rb_intern("optimized_symbols_parsing"))))) {
237
- fc->optimized_symbol_ext_type = true;
238
- }
236
+ fc->symbol_ext_type = ext_type;
237
+ fc->has_symbol_ext_type = NIL_P(options) || RTEST(packer_proc);
238
+ fc->optimized_symbol_ext_type = RTEST(options) && RTEST(rb_hash_aref(options, ID2SYM(rb_intern("optimized_symbols_parsing"))));
239
+ }
240
+
241
+ if(ext_module == rb_cInteger) {
242
+ fc->has_bigint_ext_type = false;
239
243
  }
240
244
 
241
245
  if(RTEST(options)) {
@@ -246,7 +250,6 @@ static VALUE Factory_register_type_internal(VALUE self, VALUE rb_ext_type, VALUE
246
250
  rb_raise(rb_eArgError, "oversized_integer_extension: true is only for Integer class");
247
251
  }
248
252
  }
249
-
250
253
  if(RTEST(rb_hash_aref(options, ID2SYM(rb_intern("recursive"))))) {
251
254
  flags |= MSGPACK_EXT_RECURSIVE;
252
255
  }
data/ext/msgpack/packer.h CHANGED
@@ -25,11 +25,6 @@
25
25
  #define MSGPACK_PACKER_IO_FLUSH_THRESHOLD_TO_WRITE_STRING_BODY (1024)
26
26
  #endif
27
27
 
28
- #ifndef UNREACHABLE_RETURN
29
- // Ruby 2.5
30
- #define UNREACHABLE_RETURN() return
31
- #endif
32
-
33
28
  struct msgpack_packer_t;
34
29
  typedef struct msgpack_packer_t msgpack_packer_t;
35
30
 
@@ -418,7 +413,6 @@ static inline void msgpack_packer_write_string_value(msgpack_packer_t* pk, VALUE
418
413
 
419
414
  if(RB_UNLIKELY(len > 0xffffffffL)) {
420
415
  rb_raise(rb_eArgError, "size of string is too long to pack: %lu bytes should be <= %ld", len, 0xffffffffL);
421
- UNREACHABLE_RETURN();
422
416
  }
423
417
 
424
418
  if (RB_UNLIKELY(pk->compatibility_mode)) {
@@ -235,7 +235,7 @@ static VALUE Packer_write_extension(VALUE self, VALUE obj)
235
235
 
236
236
  VALUE rb_ext_type = RSTRUCT_GET(obj, 0);
237
237
  if(!RB_TYPE_P(rb_ext_type, T_FIXNUM)) {
238
- rb_raise(rb_eRangeError, "integer %s too big to convert to `signed char'", RSTRING_PTR(rb_String(rb_ext_type)));
238
+ rb_raise(rb_eRangeError, "integer %"PRIsVALUE" too big to convert to `signed char'", rb_ext_type);
239
239
  }
240
240
 
241
241
  int ext_type = FIX2INT(rb_ext_type);
@@ -74,9 +74,13 @@ static inline VALUE rb_hash_new_capa_inline(long capa)
74
74
  #define rb_hash_new_capa rb_hash_new_capa_inline
75
75
  #endif
76
76
 
77
- static inline int16_t initial_buffer_size(long size)
77
+ static inline int16_t initial_buffer_size(msgpack_unpacker_t* uk, long count, size_t bytes_per_element)
78
78
  {
79
- return (size > INITIAL_BUFFER_CAPACITY_MAX) ? INITIAL_BUFFER_CAPACITY_MAX : size;
79
+ size_t available = msgpack_buffer_all_readable_size(UNPACKER_BUFFER_(uk)) / bytes_per_element;
80
+ if((size_t)count > available) {
81
+ count = (long)available;
82
+ }
83
+ return (count > INITIAL_BUFFER_CAPACITY_MAX) ? INITIAL_BUFFER_CAPACITY_MAX : count;
80
84
  }
81
85
 
82
86
  void msgpack_unpacker_static_init(void)
@@ -110,6 +114,7 @@ static inline void _msgpack_unpacker_free_stack(msgpack_unpacker_stack_t* stack)
110
114
  }
111
115
  stack->data = NULL;
112
116
  stack->depth = 0;
117
+ stack->floor = 0;
113
118
  }
114
119
  }
115
120
 
@@ -169,7 +174,7 @@ void _msgpack_unpacker_reset(msgpack_unpacker_t* uk)
169
174
  uk->head_byte = HEAD_BYTE_REQUIRED;
170
175
 
171
176
  /*memset(uk->stack, 0, sizeof(msgpack_unpacker_t) * uk->stack.depth);*/
172
- uk->stack.depth = 0;
177
+ msgpack_unpacker_stack_rewind(uk);
173
178
  uk->last_object = Qnil;
174
179
  uk->reading_raw = Qnil;
175
180
  uk->reading_raw_remaining = 0;
@@ -211,7 +216,7 @@ static inline int object_complete(msgpack_unpacker_t* uk, VALUE object)
211
216
  return PRIMITIVE_OBJECT_COMPLETE;
212
217
  }
213
218
 
214
- static inline int object_complete_symbol(msgpack_unpacker_t* uk, VALUE object)
219
+ static inline int object_complete_frozen(msgpack_unpacker_t* uk, VALUE object)
215
220
  {
216
221
  uk->last_object = object;
217
222
  reset_head_byte(uk);
@@ -222,9 +227,9 @@ static inline int object_complete_ext(msgpack_unpacker_t* uk, int ext_type, VALU
222
227
  {
223
228
  if (uk->optimized_symbol_ext_type && ext_type == uk->symbol_ext_type) {
224
229
  if (RB_UNLIKELY(NIL_P(str))) { // empty extension is returned as Qnil
225
- return object_complete_symbol(uk, ID2SYM(rb_intern3("", 0, rb_utf8_encoding())));
230
+ return object_complete_frozen(uk, ID2SYM(rb_intern3("", 0, rb_utf8_encoding())));
226
231
  }
227
- return object_complete_symbol(uk, rb_str_intern(str));
232
+ return object_complete_frozen(uk, rb_str_intern(str));
228
233
  }
229
234
 
230
235
  int ext_flags;
@@ -378,10 +383,24 @@ static inline int read_raw_body_begin(msgpack_unpacker_t* uk, int raw_type)
378
383
  reset_head_byte(uk);
379
384
  uk->reading_raw_remaining = 0;
380
385
 
381
- _msgpack_unpacker_stack_push(uk, STACK_TYPE_RECURSIVE, 1, Qnil);
386
+ if(_msgpack_unpacker_stack_push(uk, STACK_TYPE_RECURSIVE, 1, Qnil) < 0) {
387
+ /* Recursive extensions re-enter msgpack_unpacker_read() through the
388
+ * user proc, consuming a full C stack frame per nesting level. If we
389
+ * ignore a failed push (stack already at MSGPACK_UNPACKER_STACK_CAPACITY)
390
+ * the recursion continues unbounded and exhausts the C stack (SIGSEGV)
391
+ * instead of raising StackError like every other container type. */
392
+ return PRIMITIVE_STACK_TOO_DEEP;
393
+ }
394
+ size_t barrier_depth = uk->stack.depth;
395
+ size_t saved_floor = uk->stack.floor;
382
396
  int raised;
397
+
398
+ uk->stack.floor = barrier_depth;
383
399
  obj = protected_proc_call(proc, 1, &uk->self, &raised);
384
- msgpack_unpacker_stack_pop(uk);
400
+ uk->stack.floor = saved_floor;
401
+
402
+ /* Not --depth: the proc may return with entries left above the barrier. */
403
+ uk->stack.depth = barrier_depth - 1;
385
404
 
386
405
  if (raised) {
387
406
  uk->last_object = rb_errinfo();
@@ -398,7 +417,7 @@ static inline int read_raw_body_begin(msgpack_unpacker_t* uk, int raw_type)
398
417
  int ret;
399
418
  if ((uk->optimized_symbol_ext_type && uk->symbol_ext_type == raw_type)) {
400
419
  VALUE symbol = msgpack_buffer_read_top_as_symbol(UNPACKER_BUFFER_(uk), length, raw_type != RAW_TYPE_BINARY);
401
- ret = object_complete_symbol(uk, symbol);
420
+ ret = object_complete_frozen(uk, symbol);
402
421
  } else if (is_reading_map_key(uk) && raw_type == RAW_TYPE_STRING) {
403
422
  /* don't use zerocopy for hash keys but get a frozen string directly
404
423
  * because rb_hash_aset freezes keys and it causes copying */
@@ -409,7 +428,7 @@ static inline int read_raw_body_begin(msgpack_unpacker_t* uk, int raw_type)
409
428
  } else {
410
429
  key = msgpack_buffer_read_top_as_symbol(UNPACKER_BUFFER_(uk), length, true);
411
430
  }
412
- ret = object_complete_symbol(uk, key);
431
+ ret = object_complete_frozen(uk, key);
413
432
  } else {
414
433
  if (uk->use_key_cache) {
415
434
  key = msgpack_buffer_read_top_as_interned_string(UNPACKER_BUFFER_(uk), &uk->key_cache, length);
@@ -450,10 +469,10 @@ static int read_primitive(msgpack_unpacker_t* uk)
450
469
 
451
470
  SWITCH_RANGE_BEGIN(b)
452
471
  SWITCH_RANGE(b, 0x00, 0x7f) // Positive Fixnum
453
- return object_complete(uk, INT2NUM(b));
472
+ return object_complete_frozen(uk, INT2NUM(b));
454
473
 
455
474
  SWITCH_RANGE(b, 0xe0, 0xff) // Negative Fixnum
456
- return object_complete(uk, INT2NUM((int8_t)b));
475
+ return object_complete_frozen(uk, INT2NUM((int8_t)b));
457
476
 
458
477
  SWITCH_RANGE(b, 0xa0, 0xbf) // FixRaw / fixstr
459
478
  size_t count = b & 0x1f;
@@ -466,27 +485,27 @@ static int read_primitive(msgpack_unpacker_t* uk)
466
485
  if(count == 0) {
467
486
  return object_complete(uk, rb_ary_new());
468
487
  }
469
- return _msgpack_unpacker_stack_push(uk, STACK_TYPE_ARRAY, count, rb_ary_new2(initial_buffer_size(count)));
488
+ return _msgpack_unpacker_stack_push(uk, STACK_TYPE_ARRAY, count, rb_ary_new2(initial_buffer_size(uk, count, 1)));
470
489
 
471
490
  SWITCH_RANGE(b, 0x80, 0x8f) // FixMap
472
491
  int count = b & 0x0f;
473
492
  if(count == 0) {
474
493
  return object_complete(uk, rb_hash_new());
475
494
  }
476
- return _msgpack_unpacker_stack_push(uk, STACK_TYPE_MAP_KEY, count*2, rb_hash_new_capa(initial_buffer_size(count)));
495
+ return _msgpack_unpacker_stack_push(uk, STACK_TYPE_MAP_KEY, count*2, rb_hash_new_capa(initial_buffer_size(uk, count, 2)));
477
496
 
478
497
  SWITCH_RANGE(b, 0xc0, 0xdf) // Variable
479
498
  switch(b) {
480
499
  case 0xc0: // nil
481
- return object_complete(uk, Qnil);
500
+ return object_complete_frozen(uk, Qnil);
482
501
 
483
502
  //case 0xc1: // string
484
503
 
485
504
  case 0xc2: // false
486
- return object_complete(uk, Qfalse);
505
+ return object_complete_frozen(uk, Qfalse);
487
506
 
488
507
  case 0xc3: // true
489
- return object_complete(uk, Qtrue);
508
+ return object_complete_frozen(uk, Qtrue);
490
509
 
491
510
  case 0xc7: // ext 8
492
511
  {
@@ -542,14 +561,14 @@ static int read_primitive(msgpack_unpacker_t* uk)
542
561
  {
543
562
  READ_CAST_BLOCK_OR_RETURN_EOF(cb, uk, 1);
544
563
  uint8_t u8 = cb.u8;
545
- return object_complete(uk, INT2NUM((int)u8));
564
+ return object_complete_frozen(uk, INT2NUM((int)u8));
546
565
  }
547
566
 
548
567
  case 0xcd: // unsigned int 16
549
568
  {
550
569
  READ_CAST_BLOCK_OR_RETURN_EOF(cb, uk, 2);
551
570
  uint16_t u16 = _msgpack_be16(cb.u16);
552
- return object_complete(uk, INT2NUM((int)u16));
571
+ return object_complete_frozen(uk, INT2NUM((int)u16));
553
572
  }
554
573
 
555
574
  case 0xce: // unsigned int 32
@@ -570,14 +589,14 @@ static int read_primitive(msgpack_unpacker_t* uk)
570
589
  {
571
590
  READ_CAST_BLOCK_OR_RETURN_EOF(cb, uk, 1);
572
591
  int8_t i8 = cb.i8;
573
- return object_complete(uk, INT2NUM((int)i8));
592
+ return object_complete_frozen(uk, INT2NUM((int)i8));
574
593
  }
575
594
 
576
595
  case 0xd1: // signed int 16
577
596
  {
578
597
  READ_CAST_BLOCK_OR_RETURN_EOF(cb, uk, 2);
579
598
  int16_t i16 = _msgpack_be16(cb.i16);
580
- return object_complete(uk, INT2NUM((int)i16));
599
+ return object_complete_frozen(uk, INT2NUM((int)i16));
581
600
  }
582
601
 
583
602
  case 0xd2: // signed int 32
@@ -696,7 +715,7 @@ static int read_primitive(msgpack_unpacker_t* uk)
696
715
  if(count == 0) {
697
716
  return object_complete(uk, rb_ary_new());
698
717
  }
699
- return _msgpack_unpacker_stack_push(uk, STACK_TYPE_ARRAY, count, rb_ary_new2(initial_buffer_size(count)));
718
+ return _msgpack_unpacker_stack_push(uk, STACK_TYPE_ARRAY, count, rb_ary_new2(initial_buffer_size(uk, count, 1)));
700
719
  }
701
720
 
702
721
  case 0xdd: // array 32
@@ -706,7 +725,7 @@ static int read_primitive(msgpack_unpacker_t* uk)
706
725
  if(count == 0) {
707
726
  return object_complete(uk, rb_ary_new());
708
727
  }
709
- return _msgpack_unpacker_stack_push(uk, STACK_TYPE_ARRAY, count, rb_ary_new2(initial_buffer_size(count)));
728
+ return _msgpack_unpacker_stack_push(uk, STACK_TYPE_ARRAY, count, rb_ary_new2(initial_buffer_size(uk, count, 1)));
710
729
  }
711
730
 
712
731
  case 0xde: // map 16
@@ -716,7 +735,7 @@ static int read_primitive(msgpack_unpacker_t* uk)
716
735
  if(count == 0) {
717
736
  return object_complete(uk, rb_hash_new());
718
737
  }
719
- return _msgpack_unpacker_stack_push(uk, STACK_TYPE_MAP_KEY, count*2, rb_hash_new_capa(initial_buffer_size(count)));
738
+ return _msgpack_unpacker_stack_push(uk, STACK_TYPE_MAP_KEY, count*2, rb_hash_new_capa(initial_buffer_size(uk, count, 2)));
720
739
  }
721
740
 
722
741
  case 0xdf: // map 32
@@ -726,7 +745,7 @@ static int read_primitive(msgpack_unpacker_t* uk)
726
745
  if(count == 0) {
727
746
  return object_complete(uk, rb_hash_new());
728
747
  }
729
- return _msgpack_unpacker_stack_push(uk, STACK_TYPE_MAP_KEY, count*2, rb_hash_new_capa(initial_buffer_size(count)));
748
+ return _msgpack_unpacker_stack_push(uk, STACK_TYPE_MAP_KEY, count*2, rb_hash_new_capa(initial_buffer_size(uk, count, 2)));
730
749
  }
731
750
 
732
751
  default:
@@ -879,6 +898,10 @@ int msgpack_unpacker_skip(msgpack_unpacker_t* uk, size_t target_stack_depth)
879
898
  container_completed:
880
899
  {
881
900
  msgpack_unpacker_stack_entry_t* top = _msgpack_unpacker_stack_entry_top(uk);
901
+ if(top->type == STACK_TYPE_RECURSIVE) {
902
+ STACK_FREE(uk);
903
+ return PRIMITIVE_OBJECT_COMPLETE;
904
+ }
882
905
 
883
906
  /* this section optimized out */
884
907
  // TODO object_complete still creates objects which should be optimized out
@@ -897,82 +920,6 @@ int msgpack_unpacker_skip(msgpack_unpacker_t* uk, size_t target_stack_depth)
897
920
  }
898
921
  }
899
922
 
900
- int msgpack_unpacker_peek_next_object_type(msgpack_unpacker_t* uk)
901
- {
902
- int b = get_head_byte(uk);
903
- if(b < 0) {
904
- return b;
905
- }
906
-
907
- SWITCH_RANGE_BEGIN(b)
908
- SWITCH_RANGE(b, 0x00, 0x7f) // Positive Fixnum
909
- return TYPE_INTEGER;
910
-
911
- SWITCH_RANGE(b, 0xe0, 0xff) // Negative Fixnum
912
- return TYPE_INTEGER;
913
-
914
- SWITCH_RANGE(b, 0xa0, 0xbf) // FixRaw
915
- return TYPE_RAW;
916
-
917
- SWITCH_RANGE(b, 0x90, 0x9f) // FixArray
918
- return TYPE_ARRAY;
919
-
920
- SWITCH_RANGE(b, 0x80, 0x8f) // FixMap
921
- return TYPE_MAP;
922
-
923
- SWITCH_RANGE(b, 0xc0, 0xdf) // Variable
924
- switch(b) {
925
- case 0xc0: // nil
926
- return TYPE_NIL;
927
-
928
- case 0xc2: // false
929
- case 0xc3: // true
930
- return TYPE_BOOLEAN;
931
-
932
- case 0xca: // float
933
- case 0xcb: // double
934
- return TYPE_FLOAT;
935
-
936
- case 0xcc: // unsigned int 8
937
- case 0xcd: // unsigned int 16
938
- case 0xce: // unsigned int 32
939
- case 0xcf: // unsigned int 64
940
- return TYPE_INTEGER;
941
-
942
- case 0xd0: // signed int 8
943
- case 0xd1: // signed int 16
944
- case 0xd2: // signed int 32
945
- case 0xd3: // signed int 64
946
- return TYPE_INTEGER;
947
-
948
- case 0xd9: // raw 8 / str 8
949
- case 0xda: // raw 16 / str 16
950
- case 0xdb: // raw 32 / str 32
951
- return TYPE_RAW;
952
-
953
- case 0xc4: // bin 8
954
- case 0xc5: // bin 16
955
- case 0xc6: // bin 32
956
- return TYPE_RAW;
957
-
958
- case 0xdc: // array 16
959
- case 0xdd: // array 32
960
- return TYPE_ARRAY;
961
-
962
- case 0xde: // map 16
963
- case 0xdf: // map 32
964
- return TYPE_MAP;
965
-
966
- default:
967
- return PRIMITIVE_INVALID_BYTE;
968
- }
969
-
970
- SWITCH_RANGE_DEFAULT
971
- return PRIMITIVE_INVALID_BYTE;
972
-
973
- SWITCH_RANGE_END
974
- }
975
-
976
923
  int msgpack_unpacker_skip_nil(msgpack_unpacker_t* uk)
977
924
  {
978
925
  int b = get_head_byte(uk);
@@ -980,8 +927,8 @@ int msgpack_unpacker_skip_nil(msgpack_unpacker_t* uk)
980
927
  return b;
981
928
  }
982
929
  if(b == 0xc0) {
930
+ reset_head_byte(uk);
983
931
  return 1;
984
932
  }
985
933
  return 0;
986
934
  }
987
-
@@ -43,6 +43,7 @@ typedef struct {
43
43
 
44
44
  struct msgpack_unpacker_stack_t {
45
45
  size_t depth;
46
+ size_t floor;
46
47
  size_t capacity;
47
48
  msgpack_unpacker_stack_entry_t *data;
48
49
  };
@@ -77,16 +78,6 @@ struct msgpack_unpacker_t {
77
78
 
78
79
  #define UNPACKER_BUFFER_(uk) (&(uk)->buffer)
79
80
 
80
- enum msgpack_unpacker_object_type {
81
- TYPE_NIL = 0,
82
- TYPE_BOOLEAN,
83
- TYPE_INTEGER,
84
- TYPE_FLOAT,
85
- TYPE_RAW,
86
- TYPE_ARRAY,
87
- TYPE_MAP,
88
- };
89
-
90
81
  void msgpack_unpacker_static_init(void);
91
82
 
92
83
  void msgpack_unpacker_static_destroy(void);
@@ -119,6 +110,11 @@ static inline void msgpack_unpacker_set_allow_unknown_ext(msgpack_unpacker_t* uk
119
110
  uk->allow_unknown_ext = enable;
120
111
  }
121
112
 
113
+ static inline void msgpack_unpacker_stack_rewind(msgpack_unpacker_t* uk)
114
+ {
115
+ uk->stack.depth = uk->stack.floor;
116
+ }
117
+
122
118
 
123
119
  /* error codes */
124
120
  #define PRIMITIVE_CONTAINER_START 1
@@ -139,9 +135,6 @@ static inline VALUE msgpack_unpacker_get_last_object(msgpack_unpacker_t* uk)
139
135
  return uk->last_object;
140
136
  }
141
137
 
142
-
143
- int msgpack_unpacker_peek_next_object_type(msgpack_unpacker_t* uk);
144
-
145
138
  int msgpack_unpacker_skip_nil(msgpack_unpacker_t* uk);
146
139
 
147
140
  int msgpack_unpacker_read_array_header(msgpack_unpacker_t* uk, uint32_t* result_size);
@@ -165,7 +165,7 @@ static VALUE Unpacker_allow_unknown_ext_p(VALUE self)
165
165
 
166
166
  NORETURN(static void raise_unpacker_error(msgpack_unpacker_t *uk, int r))
167
167
  {
168
- uk->stack.depth = 0;
168
+ msgpack_unpacker_stack_rewind(uk);
169
169
  switch(r) {
170
170
  case PRIMITIVE_EOF:
171
171
  rb_raise(rb_eEOFError, "end of buffer reached");
@@ -5,7 +5,8 @@ module MessagePack
5
5
  # We split the bigint in 32bits chunks so that individual part fits into
6
6
  # a MRI immediate Integer.
7
7
  CHUNK_BITLENGTH = 32
8
- FORMAT = 'CL>*'
8
+ CHUNK_FORMAT = 'L>*'
9
+ FORMAT = "C#{CHUNK_FORMAT}"
9
10
 
10
11
  if Integer.instance_method(:[]).arity != 1 # Ruby 2.7 and newer
11
12
  # Starting from Ruby 2.7 we can address arbitrary bitranges inside an Integer with Integer#[]
@@ -56,12 +57,7 @@ module MessagePack
56
57
  parts = data.unpack(FORMAT)
57
58
 
58
59
  sign = parts.shift
59
- sum = parts.pop.to_i
60
-
61
- parts.reverse_each do |part|
62
- sum = sum << CHUNK_BITLENGTH
63
- sum += part
64
- end
60
+ sum = parts.reverse.pack(CHUNK_FORMAT).unpack1('H*').to_i(16)
65
61
 
66
62
  sign == 0 ? sum : -sum
67
63
  end
@@ -14,10 +14,12 @@ module MessagePack
14
14
  options[:packer] = packer.to_sym.to_proc
15
15
  when Method
16
16
  options[:packer] = packer.to_proc
17
- when packer.respond_to?(:call)
18
- options[:packer] = packer.method(:call).to_proc
19
17
  else
20
- raise ::TypeError, "expected :packer argument to be a callable object, got: #{packer.inspect}"
18
+ if packer.respond_to?(:call)
19
+ options[:packer] = packer.method(:call).to_proc
20
+ else
21
+ raise ::TypeError, "expected :packer argument to be a callable object, got: #{packer.inspect}"
22
+ end
21
23
  end
22
24
 
23
25
  case unpacker = options[:unpacker]
@@ -27,10 +29,12 @@ module MessagePack
27
29
  options[:unpacker] = klass.method(unpacker).to_proc
28
30
  when Method
29
31
  options[:unpacker] = unpacker.to_proc
30
- when packer.respond_to?(:call)
31
- options[:unpacker] = unpacker.method(:call).to_proc
32
32
  else
33
- raise ::TypeError, "expected :unpacker argument to be a callable object, got: #{unpacker.inspect}"
33
+ if unpacker.respond_to?(:call)
34
+ options[:unpacker] = unpacker.method(:call).to_proc
35
+ else
36
+ raise ::TypeError, "expected :unpacker argument to be a callable object, got: #{unpacker.inspect}"
37
+ end
34
38
  end
35
39
  end
36
40
 
@@ -51,7 +55,7 @@ module MessagePack
51
55
  type = ary[0]
52
56
  packer_proc = ary[1]
53
57
  unpacker_proc = nil
54
- if unpacker.has_key?(type)
58
+ if unpacker.has_key?(type) && unpacker[type][0] == klass
55
59
  unpacker_proc = unpacker.delete(type)[1]
56
60
  end
57
61
  list << {type: type, class: klass, packer: packer_proc, unpacker: unpacker_proc}
@@ -85,7 +89,7 @@ module MessagePack
85
89
 
86
90
  def type_registered?(klass_or_type, selector=:both)
87
91
  case klass_or_type
88
- when Class
92
+ when Module
89
93
  klass = klass_or_type
90
94
  registered_types(selector).any?{|entry| klass <= entry[:class] }
91
95
  when Integer
@@ -23,7 +23,7 @@ module MessagePack
23
23
 
24
24
  def type_registered?(klass_or_type)
25
25
  case klass_or_type
26
- when Class
26
+ when Module
27
27
  klass = klass_or_type
28
28
  registered_types.any?{|entry| klass <= entry[:class] }
29
29
  when Integer
@@ -27,7 +27,7 @@ module MessagePack
27
27
 
28
28
  def type_registered?(klass_or_type)
29
29
  case klass_or_type
30
- when Class
30
+ when Module
31
31
  klass = klass_or_type
32
32
  registered_types.any?{|entry| klass == entry[:class] }
33
33
  when Integer
@@ -1,5 +1,5 @@
1
1
  module MessagePack
2
- VERSION = "1.8.3"
2
+ VERSION = "1.8.7"
3
3
  # Note for maintainers:
4
4
  # Don't miss building/releasing the JRuby version (rake buld:java)
5
5
  # See "How to build -java rubygems" in README for more details.
data/msgpack.gemspec CHANGED
@@ -29,13 +29,4 @@ Gem::Specification.new do |s|
29
29
  "documentation_uri" => "https://github.com/msgpack/msgpack/blob/master/spec.md",
30
30
  "source_code_uri" => "https://github.com/msgpack/msgpack-ruby"
31
31
  }
32
-
33
- s.add_development_dependency 'bundler'
34
- s.add_development_dependency 'rake'
35
- s.add_development_dependency 'rake-compiler', ['>= 1.1.9']
36
- s.add_development_dependency 'rspec', ['~> 3.3']
37
- s.add_development_dependency 'ruby_memcheck'
38
- s.add_development_dependency 'yard'
39
- s.add_development_dependency 'json'
40
- s.add_development_dependency 'benchmark-ips', ['~> 2.10.0']
41
32
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: msgpack
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.8.3
4
+ version: 1.8.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sadayuki Furuhashi
@@ -10,119 +10,7 @@ authors:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
  date: 1980-01-02 00:00:00.000000000 Z
13
- dependencies:
14
- - !ruby/object:Gem::Dependency
15
- name: bundler
16
- requirement: !ruby/object:Gem::Requirement
17
- requirements:
18
- - - ">="
19
- - !ruby/object:Gem::Version
20
- version: '0'
21
- type: :development
22
- prerelease: false
23
- version_requirements: !ruby/object:Gem::Requirement
24
- requirements:
25
- - - ">="
26
- - !ruby/object:Gem::Version
27
- version: '0'
28
- - !ruby/object:Gem::Dependency
29
- name: rake
30
- requirement: !ruby/object:Gem::Requirement
31
- requirements:
32
- - - ">="
33
- - !ruby/object:Gem::Version
34
- version: '0'
35
- type: :development
36
- prerelease: false
37
- version_requirements: !ruby/object:Gem::Requirement
38
- requirements:
39
- - - ">="
40
- - !ruby/object:Gem::Version
41
- version: '0'
42
- - !ruby/object:Gem::Dependency
43
- name: rake-compiler
44
- requirement: !ruby/object:Gem::Requirement
45
- requirements:
46
- - - ">="
47
- - !ruby/object:Gem::Version
48
- version: 1.1.9
49
- type: :development
50
- prerelease: false
51
- version_requirements: !ruby/object:Gem::Requirement
52
- requirements:
53
- - - ">="
54
- - !ruby/object:Gem::Version
55
- version: 1.1.9
56
- - !ruby/object:Gem::Dependency
57
- name: rspec
58
- requirement: !ruby/object:Gem::Requirement
59
- requirements:
60
- - - "~>"
61
- - !ruby/object:Gem::Version
62
- version: '3.3'
63
- type: :development
64
- prerelease: false
65
- version_requirements: !ruby/object:Gem::Requirement
66
- requirements:
67
- - - "~>"
68
- - !ruby/object:Gem::Version
69
- version: '3.3'
70
- - !ruby/object:Gem::Dependency
71
- name: ruby_memcheck
72
- requirement: !ruby/object:Gem::Requirement
73
- requirements:
74
- - - ">="
75
- - !ruby/object:Gem::Version
76
- version: '0'
77
- type: :development
78
- prerelease: false
79
- version_requirements: !ruby/object:Gem::Requirement
80
- requirements:
81
- - - ">="
82
- - !ruby/object:Gem::Version
83
- version: '0'
84
- - !ruby/object:Gem::Dependency
85
- name: yard
86
- requirement: !ruby/object:Gem::Requirement
87
- requirements:
88
- - - ">="
89
- - !ruby/object:Gem::Version
90
- version: '0'
91
- type: :development
92
- prerelease: false
93
- version_requirements: !ruby/object:Gem::Requirement
94
- requirements:
95
- - - ">="
96
- - !ruby/object:Gem::Version
97
- version: '0'
98
- - !ruby/object:Gem::Dependency
99
- name: json
100
- requirement: !ruby/object:Gem::Requirement
101
- requirements:
102
- - - ">="
103
- - !ruby/object:Gem::Version
104
- version: '0'
105
- type: :development
106
- prerelease: false
107
- version_requirements: !ruby/object:Gem::Requirement
108
- requirements:
109
- - - ">="
110
- - !ruby/object:Gem::Version
111
- version: '0'
112
- - !ruby/object:Gem::Dependency
113
- name: benchmark-ips
114
- requirement: !ruby/object:Gem::Requirement
115
- requirements:
116
- - - "~>"
117
- - !ruby/object:Gem::Version
118
- version: 2.10.0
119
- type: :development
120
- prerelease: false
121
- version_requirements: !ruby/object:Gem::Requirement
122
- requirements:
123
- - - "~>"
124
- - !ruby/object:Gem::Version
125
- version: 2.10.0
13
+ dependencies: []
126
14
  description: MessagePack is a binary-based efficient object serialization library.
127
15
  It enables to exchange structured objects between many languages like JSON. But
128
16
  unlike JSON, it is very fast and small.
@@ -210,7 +98,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
210
98
  - !ruby/object:Gem::Version
211
99
  version: '0'
212
100
  requirements: []
213
- rubygems_version: 4.0.12
101
+ rubygems_version: 4.0.20
214
102
  specification_version: 4
215
103
  summary: MessagePack, a binary-based efficient data interchange format.
216
104
  test_files: []