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

ruby-changes:66195

From: Jeremy <ko1@a...>
Date: Thu, 13 May 2021 11:30:20 +0900 (JST)
Subject: [ruby-changes:66195] 9ce29c94d8 (master): Avoid improper optimization of case statements mixed integer/rational/complex

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

From 9ce29c94d82c6bf278b1be088435726a9c47e225 Mon Sep 17 00:00:00 2001
From: Jeremy Evans <code@j...>
Date: Wed, 12 May 2021 15:06:12 -0700
Subject: Avoid improper optimization of case statements mixed
 integer/rational/complex

Fixes [Bug #17857]
---
 compile.c              |  3 +++
 test/ruby/test_case.rb | 12 ++++++++++++
 2 files changed, 15 insertions(+)

diff --git a/compile.c b/compile.c
index 2e10659..a9c2f96 100644
--- a/compile.c
+++ b/compile.c
@@ -4537,6 +4537,9 @@ rb_node_case_when_optimizable_literal(const NODE *const node) https://github.com/ruby/ruby/blob/trunk/compile.c#L4537
 	    modf(RFLOAT_VALUE(v), &ival) == 0.0) {
 	    return FIXABLE(ival) ? LONG2FIX((long)ival) : rb_dbl2big(ival);
 	}
+        if (RB_TYPE_P(v, T_RATIONAL) || RB_TYPE_P(v, T_COMPLEX)) {
+            return Qundef;
+        }
 	if (SYMBOL_P(v) || rb_obj_is_kind_of(v, rb_cNumeric)) {
 	    return v;
 	}
diff --git a/test/ruby/test_case.rb b/test/ruby/test_case.rb
index 77612a8..4a0f1bf 100644
--- a/test/ruby/test_case.rb
+++ b/test/ruby/test_case.rb
@@ -102,6 +102,18 @@ class TestCase < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_case.rb#L102
     else
       assert(false)
     end
+    case 0
+    when 0r
+      assert(true)
+    else
+      assert(false)
+    end
+    case 0
+    when 0i
+      assert(true)
+    else
+      assert(false)
+    end
   end
 
   def test_method_missing
-- 
cgit v1.1


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

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