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
7 changes: 7 additions & 0 deletions ext/java/org/msgpack/jruby/Factory.java
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
5 changes: 5 additions & 0 deletions ext/java/org/msgpack/jruby/Packer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()) {
Expand Down
4 changes: 4 additions & 0 deletions ext/java/org/msgpack/jruby/Unpacker.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
8 changes: 8 additions & 0 deletions ext/msgpack/factory_class.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 5 additions & 1 deletion 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 Expand Up @@ -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);
}
4 changes: 4 additions & 0 deletions ext/msgpack/unpacker_class.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);

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