Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -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
Expand Down
6 changes: 4 additions & 2 deletions ext/msgpack/unpacker.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}

Expand Down
1 change: 1 addition & 0 deletions ext/msgpack/unpacker.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ struct msgpack_unpacker_t {

int reading_raw_type;
unsigned int head_byte;
int raised;

/* options */
int symbol_ext_type;
Expand Down
7 changes: 5 additions & 2 deletions ext/msgpack/unpacker_class.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
23 changes: 23 additions & 0 deletions spec/packer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
22 changes: 22 additions & 0 deletions spec/unpacker_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading