diff --git a/ext/java/org/msgpack/jruby/Factory.java b/ext/java/org/msgpack/jruby/Factory.java index bd31e685..5040250c 100644 --- a/ext/java/org/msgpack/jruby/Factory.java +++ b/ext/java/org/msgpack/jruby/Factory.java @@ -91,6 +91,13 @@ public IRubyObject registerTypeInternal(ThreadContext ctx, IRubyObject type, IRu IRubyObject packerProc = options.fastARef(runtime.newSymbol("packer")); IRubyObject unpackerProc = options.fastARef(runtime.newSymbol("unpacker")); + if (packerProc != null && !packerProc.isNil() && !(packerProc instanceof RubyProc)) { + throw runtime.newTypeError(String.format("expected Proc but found %s.", packerProc.getType().getName())); + } + if (unpackerProc != null && !unpackerProc.isNil() && !(unpackerProc instanceof RubyProc)) { + throw runtime.newTypeError(String.format("expected Proc but found %s.", unpackerProc.getType().getName())); + } + long typeId = ((RubyFixnum) type).getLongValue(); if (typeId < -128 || typeId > 127) { throw runtime.newRangeError(String.format("integer %d too big to convert to `signed char'", typeId)); diff --git a/ext/java/org/msgpack/jruby/Packer.java b/ext/java/org/msgpack/jruby/Packer.java index 040ec588..cdc9feee 100644 --- a/ext/java/org/msgpack/jruby/Packer.java +++ b/ext/java/org/msgpack/jruby/Packer.java @@ -11,6 +11,7 @@ import org.jruby.RubyNumeric; import org.jruby.RubyInteger; import org.jruby.RubyFixnum; +import org.jruby.RubyProc; import org.jruby.runtime.Block; import org.jruby.runtime.builtin.IRubyObject; import org.jruby.anno.JRubyClass; @@ -109,6 +110,10 @@ public IRubyObject registerType(ThreadContext ctx, IRubyObject type, IRubyObject } RubyModule extModule = (RubyModule) mod; + if (proc != null && !proc.isNil() && !(proc instanceof RubyProc)) { + throw runtime.newTypeError(String.format("expected Proc but found %s.", proc.getType().getName())); + } + registry.put(extModule, (int) typeId, false, proc, null); if (extModule == runtime.getSymbol() && !proc.isNil()) { diff --git a/ext/java/org/msgpack/jruby/Unpacker.java b/ext/java/org/msgpack/jruby/Unpacker.java index 52af531f..d25a098b 100644 --- a/ext/java/org/msgpack/jruby/Unpacker.java +++ b/ext/java/org/msgpack/jruby/Unpacker.java @@ -137,6 +137,10 @@ public IRubyObject registerTypeInternal(ThreadContext ctx, IRubyObject type, IRu throw runtime.newRangeError(String.format("integer %d too big to convert to `signed char'", typeId)); } + if (proc != null && !proc.isNil() && !(proc instanceof RubyProc)) { + throw runtime.newTypeError(String.format("expected Proc but found %s.", proc.getType().getName())); + } + RubyModule extModule = null; if (mod != runtime.getNil()) { extModule = (RubyModule)mod; diff --git a/ext/msgpack/factory_class.c b/ext/msgpack/factory_class.c index 8033ac7b..23e51eb8 100644 --- a/ext/msgpack/factory_class.c +++ b/ext/msgpack/factory_class.c @@ -227,6 +227,14 @@ static VALUE Factory_register_type_internal(VALUE self, VALUE rb_ext_type, VALUE rb_raise(rb_eFrozenError, "can't modify frozen MessagePack::Factory"); } + if (!NIL_P(packer_proc) && !rb_obj_is_proc(packer_proc)) { + rb_raise(rb_eTypeError, "wrong argument type %"PRIsVALUE" (expected Proc)", rb_obj_class(packer_proc)); + } + + if (!NIL_P(unpacker_proc) && !rb_obj_is_proc(unpacker_proc)) { + rb_raise(rb_eTypeError, "wrong argument type %"PRIsVALUE" (expected Proc)", rb_obj_class(unpacker_proc)); + } + int ext_type = NUM2INT(rb_ext_type); if(ext_type < -128 || ext_type > 127) { rb_raise(rb_eRangeError, "integer %d too big to convert to `signed char'", ext_type); diff --git a/ext/msgpack/packer_class.c b/ext/msgpack/packer_class.c index b3654a36..8dfa8a75 100644 --- a/ext/msgpack/packer_class.c +++ b/ext/msgpack/packer_class.c @@ -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) { @@ -436,7 +440,7 @@ void MessagePack_Packer_module_init(VALUE mMessagePack) rb_define_method(cMessagePack_Packer, "to_a", Packer_to_a, 0); rb_define_private_method(cMessagePack_Packer, "registered_types_internal", Packer_registered_types_internal, 0); - rb_define_method(cMessagePack_Packer, "register_type_internal", Packer_register_type_internal, 3); + rb_define_private_method(cMessagePack_Packer, "register_type_internal", Packer_register_type_internal, 3); rb_define_method(cMessagePack_Packer, "full_pack", Packer_full_pack, 0); } diff --git a/ext/msgpack/unpacker_class.c b/ext/msgpack/unpacker_class.c index 53c4764f..bbecccad 100644 --- a/ext/msgpack/unpacker_class.c +++ b/ext/msgpack/unpacker_class.c @@ -366,6 +366,10 @@ static VALUE Unpacker_register_type_internal(VALUE self, VALUE rb_ext_type, VALU 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_unpacker_t *uk = MessagePack_Unpacker_get(self); msgpack_unpacker_ext_registry_put(self, &uk->ext_registry, ext_module, ext_type, 0, proc); diff --git a/spec/packer_spec.rb b/spec/packer_spec.rb index 09268acc..0a17a40a 100644 --- a/spec/packer_spec.rb +++ b/spec/packer_spec.rb @@ -361,6 +361,15 @@ 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.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)