Skip to content

Commit

Permalink
Array#pack(cC) should not raise RangeError when argument is bignum
Browse files Browse the repository at this point in the history
  • Loading branch information
atambo committed Apr 12, 2014
1 parent 58d056f commit e631976
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
12 changes: 10 additions & 2 deletions core/src/main/java/org/jruby/util/Pack.java
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,11 @@ public IRubyObject decode(Ruby runtime, ByteBuffer enc) {
return runtime.newFixnum(c > (char) 127 ? c-256 : c);
}
public void encode(Ruby runtime, IRubyObject o, ByteList result) {
byte c = (byte) (RubyNumeric.num2long(o) & 0xff);
byte c = (byte) (num2quad(o) & 0xff);
result.append(c);
}
public void encode19(Ruby runtime, IRubyObject o, ByteList result) {
byte c = (byte) (num2quad19(o) & 0xff);
result.append(c);
}
};
Expand All @@ -384,7 +388,11 @@ public IRubyObject decode(Ruby runtime, ByteBuffer enc) {
return runtime.newFixnum(enc.get() & 0xFF);
}
public void encode(Ruby runtime, IRubyObject o, ByteList result){
byte c = o == runtime.getNil() ? 0 : (byte) (RubyNumeric.num2long(o) & 0xff);
byte c = o == runtime.getNil() ? 0 : (byte) (num2quad(o) & 0xff);
result.append(c);
}
public void encode19(Ruby runtime, IRubyObject o, ByteList result){
byte c = o == runtime.getNil() ? 0 : (byte) (num2quad19(o) & 0xff);
result.append(c);
}
};
Expand Down
11 changes: 11 additions & 0 deletions spec/regression/GH-1478_pack_c_bignum_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
describe "Array#pack('c')" do
it 'does not raise RangeError when argument is bignum' do
[0xfffffffffffffffff].pack("c").should == "\xFF"
end
end

describe "Array#pack('C')" do
it 'does not raise RangeError when argument is bignum' do
[0xfffffffffffffffff].pack("C").should == "\xFF"
end
end

0 comments on commit e631976

Please sign in to comment.