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

ruby-changes:70009

From: Jeremy <ko1@a...>
Date: Thu, 2 Dec 2021 09:22:07 +0900 (JST)
Subject: [ruby-changes:70009] fe1725236c (master): Don't call + and < in Integer.times for !FIXNUM

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

From fe1725236c8a4d6cb780874c470f7f443185ed38 Mon Sep 17 00:00:00 2001
From: Jeremy Evans <code@j...>
Date: Wed, 1 Dec 2021 13:00:11 -0800
Subject: Don't call + and < in Integer.times for !FIXNUM

The methods aren't called for FIXNUM, and it's best to have
consistent behavior.

Fixes [Bug #18377]
---
 numeric.c                 |  4 ++--
 test/ruby/test_integer.rb | 25 +++++++++++++++++++++++++
 2 files changed, 27 insertions(+), 2 deletions(-)

diff --git a/numeric.c b/numeric.c
index 4492af5bb39..d126e16d29c 100644
--- a/numeric.c
+++ b/numeric.c
@@ -5697,9 +5697,9 @@ int_dotimes(VALUE num) https://github.com/ruby/ruby/blob/trunk/numeric.c#L5697
 	VALUE i = INT2FIX(0);
 
 	for (;;) {
-	    if (!RTEST(rb_funcall(i, '<', 1, num))) break;
+            if (!RTEST(int_le(i, num))) break;
 	    rb_yield(i);
-	    i = rb_funcall(i, '+', 1, INT2FIX(1));
+            i = rb_int_plus(i, INT2FIX(1));
 	}
     }
     return num;
diff --git a/test/ruby/test_integer.rb b/test/ruby/test_integer.rb
index 9354514df07..a2b181c6422 100644
--- a/test/ruby/test_integer.rb
+++ b/test/ruby/test_integer.rb
@@ -299,6 +299,31 @@ class TestInteger < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_integer.rb#L299
     end
   end
 
+  def test_times_bignum_redefine_plus_lt
+    assert_separately([], "#{<<-"begin;"}\n#{<<~"end;"}")
+    begin;
+      called = false
+      Integer.class_eval do
+        alias old_plus +
+        undef +
+        define_method(:+){|x| called = true; 1}
+        alias old_lt <
+        undef <
+        define_method(:<){|x| called = true}
+      end
+      big = 2**65
+      big.times{break 0}
+      Integer.class_eval do
+        undef +
+        alias + old_plus
+        undef <
+        alias < old_lt
+      end
+      bug18377 = "[ruby-core:106361]"
+      assert_equal(false, called, bug18377)
+    end;
+  end
+
   def assert_int_equal(expected, result, mesg = nil)
     assert_kind_of(Integer, result, mesg)
     assert_equal(expected, result, mesg)
-- 
cgit v1.2.1


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

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