Skip to content
Closed
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
4 changes: 4 additions & 0 deletions ext/msgpack/packer_class.c
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,10 @@ static VALUE Packer_register_type_internal(VALUE self, VALUE rb_ext_type, VALUE
rb_raise(rb_eRangeError, "integer %d too big to convert to `signed char'", ext_type);
}

if (!rb_obj_is_proc(proc)) {
rb_raise(rb_eTypeError, "wrong argument type %"PRIsVALUE" (expected Proc)", rb_obj_class(proc));
}

msgpack_packer_ext_registry_put(self, &pk->ext_registry, ext_module, ext_type, 0, proc);

if (ext_module == rb_cSymbol) {
Expand Down
10 changes: 10 additions & 0 deletions spec/packer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,16 @@ def self.from_msgpack_ext(data)
packer.register_type(0x02, ValueTwo, &:to_msgpack_ext)
end

it 'rejects a callback that is not a Proc' do
packer = MessagePack::Packer.new
not_a_proc = Object.new
def not_a_proc.to_proc; "not a proc"; end

expect { packer.register_type(0x01, ValueOne, not_a_proc) }.to raise_error(TypeError, /expected Proc/)
expect { packer.register_type_internal(0x01, ValueOne, "not a proc") }.to raise_error(TypeError, /expected Proc/)
expect(packer.registered_types).to be_empty
end

it 'returns a Hash which contains map of Class and type' do
packer = MessagePack::Packer.new
packer.register_type(0x01, ValueOne, :to_msgpack_ext)
Expand Down
Loading