Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix SEGV that will happen with MRI 2.4.0 because of unification of Fixnum and Bignum into Integer #115

Merged
merged 2 commits into from
May 25, 2016
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
21 changes: 19 additions & 2 deletions ext/msgpack/core_ext.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,17 @@ static VALUE FalseClass_to_msgpack(int argc, VALUE* argv, VALUE self)
return packer;
}

static VALUE Integer_to_msgpack(int argc, VALUE* argv, VALUE self)
{
ENSURE_PACKER(argc, argv, packer, pk);
if (FIXNUM_P(self)) {
msgpack_packer_write_fixnum_value(pk, self);
} else {
msgpack_packer_write_bignum_value(pk, self);
}
return packer;
}

static VALUE Fixnum_to_msgpack(int argc, VALUE* argv, VALUE self)
{
ENSURE_PACKER(argc, argv, packer, pk);
Expand Down Expand Up @@ -132,8 +143,14 @@ void MessagePack_core_ext_module_init()
rb_define_method(rb_cNilClass, "to_msgpack", NilClass_to_msgpack, -1);
rb_define_method(rb_cTrueClass, "to_msgpack", TrueClass_to_msgpack, -1);
rb_define_method(rb_cFalseClass, "to_msgpack", FalseClass_to_msgpack, -1);
rb_define_method(rb_cFixnum, "to_msgpack", Fixnum_to_msgpack, -1);
rb_define_method(rb_cBignum, "to_msgpack", Bignum_to_msgpack, -1);
if (rb_cFixnum == rb_cBignum) {
// MRI Ruby >= 2.4 unified Fixnum and Bignum into Integer since Feature #12005
// https://github.com/ruby/ruby/commit/f9727c12cc8fbc5f752f5983be1f14bb976e5a13
rb_define_method(rb_cFixnum, "to_msgpack", Integer_to_msgpack, -1);
} else {
rb_define_method(rb_cFixnum, "to_msgpack", Fixnum_to_msgpack, -1);
rb_define_method(rb_cBignum, "to_msgpack", Bignum_to_msgpack, -1);
}
rb_define_method(rb_cFloat, "to_msgpack", Float_to_msgpack, -1);
rb_define_method(rb_cString, "to_msgpack", String_to_msgpack, -1);
rb_define_method(rb_cArray, "to_msgpack", Array_to_msgpack, -1);
Expand Down
28 changes: 28 additions & 0 deletions spec/packer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,34 @@ def self.from_msgpack_ext(data)
end
end

describe "fixnum and bignum" do
it "fixnum.to_msgpack" do
23.to_msgpack.should == "\x17"
end

it "fixnum.to_msgpack(packer)" do
23.to_msgpack(packer)
packer.to_s.should == "\x17"
end

it "bignum.to_msgpack" do
-4294967296.to_msgpack.should == "\xD3\xFF\xFF\xFF\xFF\x00\x00\x00\x00"
end

it "bignum.to_msgpack(packer)" do
-4294967296.to_msgpack(packer)
packer.to_s.should == "\xD3\xFF\xFF\xFF\xFF\x00\x00\x00\x00"
end

it "unpack(fixnum)" do
MessagePack.unpack("\x17").should == 23
end

it "unpack(bignum)" do
MessagePack.unpack("\xD3\xFF\xFF\xFF\xFF\x00\x00\x00\x00").should == -4294967296
end
end

describe "ext formats" do
[1, 2, 4, 8, 16].zip([0xd4, 0xd5, 0xd6, 0xd7, 0xd8]).each do |n,b|
it "msgpack fixext #{n} format" do
Expand Down