From d3199342d603c8334a13d64b4069b479f5010f9d Mon Sep 17 00:00:00 2001 From: Jean Boussier Date: Thu, 3 Dec 2026 08:23:31 +0100 Subject: [PATCH] Handle recursive unpacker procs throwing Or using early return or any other construct causing `rb_errinfo` not to be an exception. Reported-By: S4lmon-SH <241259593+S4lmon-SH@users.noreply.github.com> --- ChangeLog | 2 ++ ext/msgpack/unpacker.c | 6 ++++-- ext/msgpack/unpacker.h | 1 + ext/msgpack/unpacker_class.c | 7 +++++-- spec/packer_spec.rb | 23 +++++++++++++++++++++++ spec/unpacker_spec.rb | 22 ++++++++++++++++++++++ 6 files changed, 57 insertions(+), 4 deletions(-) diff --git a/ChangeLog b/ChangeLog index 06d7e037..707deed4 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,5 @@ +* Handle recursive unpacker procs throwing (or early return, etc). + 2026-10-30 1.8.7 * Fix the Java version of the unpacker to never preallocate objects larger than diff --git a/ext/msgpack/unpacker.c b/ext/msgpack/unpacker.c index 7ee9463f..7752d03b 100644 --- a/ext/msgpack/unpacker.c +++ b/ext/msgpack/unpacker.c @@ -242,7 +242,8 @@ static inline int object_complete_ext(msgpack_unpacker_t* uk, int ext_type, VALU int raised; obj = protected_proc_call(proc, 1, &arg, &raised); if (raised) { - uk->last_object = rb_errinfo(); + uk->raised = raised; + uk->last_object = Qnil; return PRIMITIVE_RECURSIVE_RAISED; } return object_complete(uk, obj); @@ -404,7 +405,8 @@ static inline int read_raw_body_begin(msgpack_unpacker_t* uk, int raw_type) uk->stack.depth = barrier_depth - 1; if (raised) { - uk->last_object = rb_errinfo(); + uk->raised = raised; + uk->last_object = Qnil; return PRIMITIVE_RECURSIVE_RAISED; } diff --git a/ext/msgpack/unpacker.h b/ext/msgpack/unpacker.h index ef4cb71c..72287641 100644 --- a/ext/msgpack/unpacker.h +++ b/ext/msgpack/unpacker.h @@ -65,6 +65,7 @@ struct msgpack_unpacker_t { int reading_raw_type; unsigned int head_byte; + int raised; /* options */ int symbol_ext_type; diff --git a/ext/msgpack/unpacker_class.c b/ext/msgpack/unpacker_class.c index bbecccad..ba28956c 100644 --- a/ext/msgpack/unpacker_class.c +++ b/ext/msgpack/unpacker_class.c @@ -182,9 +182,12 @@ NORETURN(static void raise_unpacker_error(msgpack_unpacker_t *uk, int r)) case PRIMITIVE_UNEXPECTED_EXT_TYPE: rb_raise(eUnknownExtTypeError, "unexpected extension type"); break; - case PRIMITIVE_RECURSIVE_RAISED: - rb_exc_raise(msgpack_unpacker_get_last_object(uk)); + case PRIMITIVE_RECURSIVE_RAISED: { + int raised = uk->raised; + uk->raised = 0; + rb_jump_tag(raised); break; + } default: rb_raise(eUnpackError, "logically unknown error %d", r); } diff --git a/spec/packer_spec.rb b/spec/packer_spec.rb index 0a17a40a..acf90f92 100644 --- a/spec/packer_spec.rb +++ b/spec/packer_spec.rb @@ -626,6 +626,29 @@ def to_msgpack_ext packer.full_pack end + it "doesn't crash when recursive packer throw" do + hash_with_indifferent_access = Class.new(Hash) + msgpack = MessagePack::Factory.new + msgpack.register_type( + 0x02, + hash_with_indifferent_access, + packer: ->(value, packer) do + throw :oops, :oops + end, + recursive: true + ) + + packer = msgpack.packer + + hash = hash_with_indifferent_access.new + 2.times do + result = catch(:oops) do + packer.write(hash) + end + expect(result).to be(:oops) + end + end + it 'does not copy freed memory when a string is freed during io.write' do skip if IS_JRUBY diff --git a/spec/unpacker_spec.rb b/spec/unpacker_spec.rb index de2f06cd..8aaf49fe 100644 --- a/spec/unpacker_spec.rb +++ b/spec/unpacker_spec.rb @@ -902,6 +902,28 @@ def flatten(struct, results = []) end end + it "doesn't crash when recursive unpacker throw" do + hash_with_indifferent_access = Class.new(Hash) + msgpack = MessagePack::Factory.new + msgpack.register_type( + 0x01, + hash_with_indifferent_access, + packer: ->(value, packer) do + packer.write(value.to_h) + end, + unpacker: ->(unpacker) { throw :oops, :oops }, + recursive: true + ) + payload = msgpack.dump(hash_with_indifferent_access.new) + + 2.times do + result = catch(:oops) do + msgpack.load("\xd4\x01A".b) + end + expect(result).to be(:oops) + end + end + if RUBY_PLATFORM != "java" it "doesn't leak when a recursive unpacker raises" do hash_with_indifferent_access = Class.new(Hash)