From a85ed626f18d1014d09fb37eb0a703976c3d2b53 Mon Sep 17 00:00:00 2001 From: Marc-Andre Lafortune Date: Thu, 18 Mar 2021 23:29:49 -0400 Subject: [PATCH] Fix Enumerable#inject with high negative fixnums [Bug #17731] --- enum.c | 2 +- test/ruby/test_enum.rb | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) 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(:+))