@@ -65,7 +65,11 @@ static size_t Unpacker_memsize(const void *ptr)
65
65
total_size += sizeof (msgpack_unpacker_ext_registry_t ) / (uk -> ext_registry -> borrow_count + 1 );
66
66
}
67
67
68
- total_size += (uk -> stack -> depth + 1 ) * sizeof (msgpack_unpacker_stack_t );
68
+ msgpack_unpacker_stack_t * stack = uk -> stack ;
69
+ while (stack ) {
70
+ total_size += (stack -> depth + 1 ) * sizeof (msgpack_unpacker_stack_t );
71
+ stack = stack -> parent ;
72
+ }
69
73
70
74
return total_size + msgpack_buffer_memsize (& uk -> buffer );
71
75
}
@@ -156,20 +160,28 @@ static VALUE Unpacker_allow_unknown_ext_p(VALUE self)
156
160
return uk -> allow_unknown_ext ? Qtrue : Qfalse ;
157
161
}
158
162
159
- NORETURN (static void raise_unpacker_error (int r ))
163
+ NORETURN (static void raise_unpacker_error (msgpack_unpacker_t * uk , int r ))
160
164
{
165
+ uk -> stack -> depth = 0 ;
161
166
switch (r ) {
162
167
case PRIMITIVE_EOF :
163
168
rb_raise (rb_eEOFError , "end of buffer reached" );
169
+ break ;
164
170
case PRIMITIVE_INVALID_BYTE :
165
171
rb_raise (eMalformedFormatError , "invalid byte" );
172
+ break ;
166
173
case PRIMITIVE_STACK_TOO_DEEP :
167
174
rb_raise (eStackError , "stack level too deep" );
175
+ break ;
168
176
case PRIMITIVE_UNEXPECTED_TYPE :
169
177
rb_raise (eUnexpectedTypeError , "unexpected type" );
178
+ break ;
170
179
case PRIMITIVE_UNEXPECTED_EXT_TYPE :
171
- // rb_bug("unexpected extension type");
172
180
rb_raise (eUnknownExtTypeError , "unexpected extension type" );
181
+ break ;
182
+ case PRIMITIVE_RECURSIVE_RAISED :
183
+ rb_exc_raise (msgpack_unpacker_get_last_object (uk ));
184
+ break ;
173
185
default :
174
186
rb_raise (eUnpackError , "logically unknown error %d" , r );
175
187
}
@@ -190,7 +202,7 @@ static VALUE Unpacker_read(VALUE self)
190
202
191
203
int r = msgpack_unpacker_read (uk , 0 );
192
204
if (r < 0 ) {
193
- raise_unpacker_error (r );
205
+ raise_unpacker_error (uk , r );
194
206
}
195
207
196
208
return msgpack_unpacker_get_last_object (uk );
@@ -202,7 +214,7 @@ static VALUE Unpacker_skip(VALUE self)
202
214
203
215
int r = msgpack_unpacker_skip (uk , 0 );
204
216
if (r < 0 ) {
205
- raise_unpacker_error (r );
217
+ raise_unpacker_error (uk , r );
206
218
}
207
219
208
220
return Qnil ;
@@ -214,7 +226,7 @@ static VALUE Unpacker_skip_nil(VALUE self)
214
226
215
227
int r = msgpack_unpacker_skip_nil (uk );
216
228
if (r < 0 ) {
217
- raise_unpacker_error (r );
229
+ raise_unpacker_error (uk , r );
218
230
}
219
231
220
232
if (r ) {
@@ -230,7 +242,7 @@ static VALUE Unpacker_read_array_header(VALUE self)
230
242
uint32_t size ;
231
243
int r = msgpack_unpacker_read_array_header (uk , & size );
232
244
if (r < 0 ) {
233
- raise_unpacker_error (r );
245
+ raise_unpacker_error (uk , r );
234
246
}
235
247
236
248
return ULONG2NUM (size ); // long at least 32 bits
@@ -243,7 +255,7 @@ static VALUE Unpacker_read_map_header(VALUE self)
243
255
uint32_t size ;
244
256
int r = msgpack_unpacker_read_map_header (uk , & size );
245
257
if (r < 0 ) {
246
- raise_unpacker_error (( int ) r );
258
+ raise_unpacker_error (uk , r );
247
259
}
248
260
249
261
return ULONG2NUM (size ); // long at least 32 bits
@@ -270,7 +282,7 @@ static VALUE Unpacker_each_impl(VALUE self)
270
282
if (r == PRIMITIVE_EOF ) {
271
283
return Qnil ;
272
284
}
273
- raise_unpacker_error (r );
285
+ raise_unpacker_error (uk , r );
274
286
}
275
287
VALUE v = msgpack_unpacker_get_last_object (uk );
276
288
#ifdef JRUBY
@@ -369,7 +381,7 @@ static VALUE Unpacker_full_unpack(VALUE self)
369
381
370
382
int r = msgpack_unpacker_read (uk , 0 );
371
383
if (r < 0 ) {
372
- raise_unpacker_error (r );
384
+ raise_unpacker_error (uk , r );
373
385
}
374
386
375
387
/* raise if extra bytes follow */
0 commit comments