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

ruby-changes:61684

From: Alan <ko1@a...>
Date: Fri, 12 Jun 2020 03:46:29 +0900 (JST)
Subject: [ruby-changes:61684] e100fcbdd1 (master): Prohibit setting class variable on frozen module through inheritance

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

From e100fcbdd1e81c23e3b4c87964aff210232903a8 Mon Sep 17 00:00:00 2001
From: Alan Wu <XrXr@u...>
Date: Wed, 10 Jun 2020 18:44:52 -0400
Subject: Prohibit setting class variable on frozen module through inheritance

Setting class varibles goes through the ancestor list which can
contain iclasses. Iclasses share a lot of information with the
module they are made from, but not the frozen status.

Check the frozen status of the module instead of the iclass.

diff --git a/test/ruby/test_variable.rb b/test/ruby/test_variable.rb
index b053e11..685a062 100644
--- a/test/ruby/test_variable.rb
+++ b/test/ruby/test_variable.rb
@@ -35,6 +35,16 @@ class TestVariable < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_variable.rb#L35
     end
   end
 
+  def test_setting_class_variable_on_module_through_inheritance
+    mod = Module.new
+    mod.class_variable_set(:@@foo, 1)
+    mod.freeze
+    c = Class.new { include(mod) }
+    assert_raise(FrozenError) { c.class_variable_set(:@@foo, 2) }
+    assert_raise(FrozenError) { c.class_eval("@@foo = 2") }
+    assert_equal(1, c.class_variable_get(:@@foo))
+  end
+
   def test_singleton_class_included_class_variable
     c = Class.new
     c.extend(Olympians)
diff --git a/variable.c b/variable.c
index ebf23fa..0d8a424 100644
--- a/variable.c
+++ b/variable.c
@@ -3143,6 +3143,9 @@ rb_cvar_set(VALUE klass, ID id, VALUE val) https://github.com/ruby/ruby/blob/trunk/variable.c#L3143
 	target = tmp;
     }
 
+    if (RB_TYPE_P(target, T_ICLASS)) {
+        target = RBASIC(target)->klass;
+    }
     check_before_mod_set(target, id, val, "class variable");
     if (!RCLASS_IV_TBL(target)) {
 	RCLASS_IV_TBL(target) = st_init_numtable();
-- 
cgit v0.10.2


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

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