aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMarc-Andre Lafortune <github@marc-andre.ca>2021-03-18 23:29:49 -0400
committerMarc-André Lafortune <github@marc-andre.ca>2021-03-19 00:21:41 -0400
commita85ed626f18d1014d09fb37eb0a703976c3d2b53 (patch)
tree73727d54be68b75b8b04b4a6446f1ba88ce3f831
parentc576e63ee752f2c7ce865b1cb1398d013d55f153 (diff)
downloadruby-a85ed626f18d1014d09fb37eb0a703976c3d2b53.tar.gz
Fix Enumerable#inject with high negative fixnums [Bug #17731]
-rw-r--r--enum.c2
-rw-r--r--test/ruby/test_enum.rb2
2 files changed, 3 insertions, 1 deletions
diff --git a/enum.c b/enum.c
index d8eefc8cbf..e0fcab049c 100644
--- a/enum.c
+++ b/enum.c
@@ -805,7 +805,7 @@ ary_inject_op(VALUE ary, VALUE init, VALUE op)
if (FIXNUM_P(e)) {
n += FIX2LONG(e); /* should not overflow long type */
if (!FIXABLE(n)) {
- v = rb_big_plus(ULONG2NUM(n), v);
+ v = rb_big_plus(LONG2NUM(n), v);
n = 0;
}
}
diff --git a/test/ruby/test_enum.rb b/test/ruby/test_enum.rb
index fc0220368b..3b0e0f79b2 100644
--- a/test/ruby/test_enum.rb
+++ b/test/ruby/test_enum.rb
@@ -248,11 +248,13 @@ class TestEnumerable < Test::Unit::TestCase
assert_equal(15, [3, 5, 7].inject(:+))
assert_float_equal(15.0, [3, 5, 7.0].inject(:+))
assert_equal(2*FIXNUM_MAX, Array.new(2, FIXNUM_MAX).inject(:+))
+ assert_equal(3*FIXNUM_MAX, Array.new(3, FIXNUM_MAX).inject(:+))
assert_equal(2*(FIXNUM_MAX+1), Array.new(2, FIXNUM_MAX+1).inject(:+))
assert_equal(10*FIXNUM_MAX, Array.new(10, FIXNUM_MAX).inject(:+))
assert_equal(0, ([FIXNUM_MAX, 1, -FIXNUM_MAX, -1]*10).inject(:+))
assert_equal(FIXNUM_MAX*10, ([FIXNUM_MAX+1, -1]*10).inject(:+))
assert_equal(2*FIXNUM_MIN, Array.new(2, FIXNUM_MIN).inject(:+))
+ assert_equal(3*FIXNUM_MIN, Array.new(3, FIXNUM_MIN).inject(:+))
assert_equal((FIXNUM_MAX+1).to_f, [FIXNUM_MAX, 1, 0.0].inject(:+))
assert_float_equal(10.0, [3.0, 5].inject(2.0, :+))
assert_float_equal((FIXNUM_MAX+1).to_f, [0.0, FIXNUM_MAX+1].inject(:+))