@@ -65,7 +65,11 @@ static size_t Unpacker_memsize(const void *ptr)
6565 total_size += sizeof (msgpack_unpacker_ext_registry_t ) / (uk -> ext_registry -> borrow_count + 1 );
6666 }
6767
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+ }
6973
7074 return total_size + msgpack_buffer_memsize (& uk -> buffer );
7175}
@@ -156,20 +160,28 @@ static VALUE Unpacker_allow_unknown_ext_p(VALUE self)
156160 return uk -> allow_unknown_ext ? Qtrue : Qfalse ;
157161}
158162
159- NORETURN (static void raise_unpacker_error (int r ))
163+ NORETURN (static void raise_unpacker_error (msgpack_unpacker_t * uk , int r ))
160164{
165+ uk -> stack -> depth = 0 ;
161166 switch (r ) {
162167 case PRIMITIVE_EOF :
163168 rb_raise (rb_eEOFError , "end of buffer reached" );
169+ break ;
164170 case PRIMITIVE_INVALID_BYTE :
165171 rb_raise (eMalformedFormatError , "invalid byte" );
172+ break ;
166173 case PRIMITIVE_STACK_TOO_DEEP :
167174 rb_raise (eStackError , "stack level too deep" );
175+ break ;
168176 case PRIMITIVE_UNEXPECTED_TYPE :
169177 rb_raise (eUnexpectedTypeError , "unexpected type" );
178+ break ;
170179 case PRIMITIVE_UNEXPECTED_EXT_TYPE :
171- // rb_bug("unexpected extension type");
172180 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 ;
173185 default :
174186 rb_raise (eUnpackError , "logically unknown error %d" , r );
175187 }
@@ -190,7 +202,7 @@ static VALUE Unpacker_read(VALUE self)
190202
191203 int r = msgpack_unpacker_read (uk , 0 );
192204 if (r < 0 ) {
193- raise_unpacker_error (r );
205+ raise_unpacker_error (uk , r );
194206 }
195207
196208 return msgpack_unpacker_get_last_object (uk );
@@ -202,7 +214,7 @@ static VALUE Unpacker_skip(VALUE self)
202214
203215 int r = msgpack_unpacker_skip (uk , 0 );
204216 if (r < 0 ) {
205- raise_unpacker_error (r );
217+ raise_unpacker_error (uk , r );
206218 }
207219
208220 return Qnil ;
@@ -214,7 +226,7 @@ static VALUE Unpacker_skip_nil(VALUE self)
214226
215227 int r = msgpack_unpacker_skip_nil (uk );
216228 if (r < 0 ) {
217- raise_unpacker_error (r );
229+ raise_unpacker_error (uk , r );
218230 }
219231
220232 if (r ) {
@@ -230,7 +242,7 @@ static VALUE Unpacker_read_array_header(VALUE self)
230242 uint32_t size ;
231243 int r = msgpack_unpacker_read_array_header (uk , & size );
232244 if (r < 0 ) {
233- raise_unpacker_error (r );
245+ raise_unpacker_error (uk , r );
234246 }
235247
236248 return ULONG2NUM (size ); // long at least 32 bits
@@ -243,7 +255,7 @@ static VALUE Unpacker_read_map_header(VALUE self)
243255 uint32_t size ;
244256 int r = msgpack_unpacker_read_map_header (uk , & size );
245257 if (r < 0 ) {
246- raise_unpacker_error (( int ) r );
258+ raise_unpacker_error (uk , r );
247259 }
248260
249261 return ULONG2NUM (size ); // long at least 32 bits
@@ -270,7 +282,7 @@ static VALUE Unpacker_each_impl(VALUE self)
270282 if (r == PRIMITIVE_EOF ) {
271283 return Qnil ;
272284 }
273- raise_unpacker_error (r );
285+ raise_unpacker_error (uk , r );
274286 }
275287 VALUE v = msgpack_unpacker_get_last_object (uk );
276288#ifdef JRUBY
@@ -369,7 +381,7 @@ static VALUE Unpacker_full_unpack(VALUE self)
369381
370382 int r = msgpack_unpacker_read (uk , 0 );
371383 if (r < 0 ) {
372- raise_unpacker_error (r );
384+ raise_unpacker_error (uk , r );
373385 }
374386
375387 /* raise if extra bytes follow */
0 commit comments