Permalink
Browse files

fix an overflow by multiplying

if multiply Fixnum value between about half of max of long type (32bit), calculated value is broken.

Test Script
----
require 'test/unit/assertions.rb'
include Test::Unit::Assertions

a = 2147483648
b = 2147483647

assert_equal(4611686016279904256, a * b)
assert_equal(-4611686016279904256, (-a) * b)
assert_equal(-4611686016279904256, a * (-b))
assert_equal(4611686016279904256, (-a) * (-b))
  • Loading branch information...
1 parent a3b91c6 commit 9130bd366d5dd62aa847a8cef8a02c1c3f82f4d2 @Watson1978 Watson1978 committed May 15, 2012
Showing with 3 additions and 0 deletions.
  1. +3 −0 kernel.c
View
@@ -403,6 +403,9 @@ vm_fast_mult(VALUE left, VALUE right, unsigned char overriden)
if (FIXABLE(res) && res / a == b) {
return LONG2FIX(res);
}
+ else {
+ return rb_big_mul(rb_int2big(a), rb_int2big(b));
+ }
}
else {
const double res = IMM2DBL(left) * IMM2DBL(right);

0 comments on commit 9130bd3

Please sign in to comment.