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

ruby-changes:58591

From: Nobuyoshi <ko1@a...>
Date: Wed, 6 Nov 2019 11:22:45 +0900 (JST)
Subject: [ruby-changes:58591] f72dc407f2 (master): Prohibit calling undefined allocator [Bug #16297]

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

From f72dc407f2f682063cf3d7683b879b430481b4ce Mon Sep 17 00:00:00 2001
From: Nobuyoshi Nakada <nobu@r...>
Date: Wed, 6 Nov 2019 11:17:09 +0900
Subject: Prohibit calling undefined allocator [Bug #16297]


diff --git a/object.c b/object.c
index ab9ff80..65294c3 100644
--- a/object.c
+++ b/object.c
@@ -2106,6 +2106,9 @@ rb_undefined_alloc(VALUE klass) https://github.com/ruby/ruby/blob/trunk/object.c#L2106
 	     klass);
 }
 
+static rb_alloc_func_t class_get_alloc_func(VALUE klass);
+static VALUE class_call_alloc_func(rb_alloc_func_t allocator, VALUE klass);
+
 /*
  *  call-seq:
  *     class.allocate()   ->   obj
@@ -2129,9 +2132,26 @@ rb_undefined_alloc(VALUE klass) https://github.com/ruby/ruby/blob/trunk/object.c#L2132
  */
 
 static VALUE
+rb_class_alloc_m(VALUE klass)
+{
+    rb_alloc_func_t allocator = class_get_alloc_func(klass);
+    if (!rb_obj_respond_to(klass, rb_intern("allocate"), 1)) {
+        rb_raise(rb_eTypeError, "calling %"PRIsVALUE".allocate is prohibited",
+                 klass);
+    }
+    return class_call_alloc_func(allocator, klass);
+}
+
+static VALUE
 rb_class_alloc(VALUE klass)
 {
-    VALUE obj;
+    rb_alloc_func_t allocator = class_get_alloc_func(klass);
+    return class_call_alloc_func(allocator, klass);
+}
+
+static rb_alloc_func_t
+class_get_alloc_func(VALUE klass)
+{
     rb_alloc_func_t allocator;
 
     if (RCLASS_SUPER(klass) == 0 && klass != rb_cBasicObject) {
@@ -2144,6 +2164,13 @@ rb_class_alloc(VALUE klass) https://github.com/ruby/ruby/blob/trunk/object.c#L2164
     if (!allocator) {
 	rb_undefined_alloc(klass);
     }
+    return allocator;
+}
+
+static VALUE
+class_call_alloc_func(rb_alloc_func_t allocator, VALUE klass)
+{
+    VALUE obj;
 
     RUBY_DTRACE_CREATE_HOOK(OBJECT, rb_class2name(klass));
 
@@ -4689,7 +4716,7 @@ InitVM_Object(void) https://github.com/ruby/ruby/blob/trunk/object.c#L4716
     rb_define_method(rb_cModule, "deprecate_constant", rb_mod_deprecate_constant, -1); /* in variable.c */
     rb_define_method(rb_cModule, "singleton_class?", rb_mod_singleton_p, 0);
 
-    rb_define_method(rb_cClass, "allocate", rb_class_alloc, 0);
+    rb_define_method(rb_cClass, "allocate", rb_class_alloc_m, 0);
     rb_define_method(rb_cClass, "new", rb_class_s_new, -1);
     rb_define_method(rb_cClass, "initialize", rb_class_initialize, -1);
     rb_define_method(rb_cClass, "superclass", rb_class_superclass, 0);
diff --git a/test/ruby/test_class.rb b/test/ruby/test_class.rb
index 407b0aa..ca78473 100644
--- a/test/ruby/test_class.rb
+++ b/test/ruby/test_class.rb
@@ -274,6 +274,14 @@ class TestClass < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_class.rb#L274
     assert_raise(TypeError) { Class.allocate.superclass }
     bug6863 = '[ruby-core:47148]'
     assert_raise(TypeError, bug6863) { Class.new(Class.allocate) }
+
+    allocator = Class.instance_method(:allocate)
+    assert_raise_with_message(TypeError, /prohibited/) {
+      allocator.bind(Rational).call
+    }
+    assert_raise_with_message(TypeError, /prohibited/) {
+      allocator.bind_call(Rational)
+    }
   end
 
   def test_nonascii_name
-- 
cgit v0.10.2


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

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