[前][次][番号順一覧][スレッド一覧]

ruby-changes:65594

From: Marc-Andre <ko1@a...>
Date: Fri, 19 Mar 2021 13:22:07 +0900 (JST)
Subject: [ruby-changes:65594] a85ed626f1 (master): Fix Enumerable#inject with high negative fixnums [Bug #17731]

https://git.ruby-lang.org/ruby.git/commit/?id=a85ed626f1

From a85ed626f18d1014d09fb37eb0a703976c3d2b53 Mon Sep 17 00:00:00 2001
From: Marc-Andre Lafortune <github@m...>
Date: Thu, 18 Mar 2021 23:29:49 -0400
Subject: 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 d8eefc8..e0fcab0 100644
--- a/enum.c
+++ b/enum.c
@@ -805,7 +805,7 @@ ary_inject_op(VALUE ary, VALUE init, VALUE op) https://github.com/ruby/ruby/blob/trunk/enum.c#L805
                 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 fc02203..3b0e0f7 100644
--- a/test/ruby/test_enum.rb
+++ b/test/ruby/test_enum.rb
@@ -248,11 +248,13 @@ class TestEnumerable < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_enum.rb#L248
     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(:+))
-- 
cgit v1.1


--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]