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

ruby-changes:65361

From: usa <ko1@a...>
Date: Sun, 28 Feb 2021 23:02:09 +0900 (JST)
Subject: [ruby-changes:65361] aa3dda8bce (ruby_2_6): merge revision(s) f72dc407: [Backport #16297]

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

From aa3dda8bcef6d053c07c1b60f61857f5b5e23b7c Mon Sep 17 00:00:00 2001
From: usa <usa@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>
Date: Sun, 28 Feb 2021 14:01:55 +0000
Subject: merge revision(s) f72dc407: [Backport #16297]

	Prohibit calling undefined allocator [Bug #16297]

	---
	 object.c                | 31 +++++++++++++++++++++++++++++--
	 test/ruby/test_class.rb |  8 ++++++++
	 2 files changed, 37 insertions(+), 2 deletions(-)

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_2_6@67896 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
---
 object.c                | 31 +++++++++++++++++++++++++++++--
 test/ruby/test_class.rb |  5 +++++
 version.h               |  2 +-
 3 files changed, 35 insertions(+), 3 deletions(-)

diff --git a/object.c b/object.c
index 454d357..f746c90 100644
--- a/object.c
+++ b/object.c
@@ -2089,6 +2089,9 @@ rb_undefined_alloc(VALUE klass) https://github.com/ruby/ruby/blob/trunk/object.c#L2089
 	     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
@@ -2112,9 +2115,26 @@ rb_undefined_alloc(VALUE klass) https://github.com/ruby/ruby/blob/trunk/object.c#L2115
  */
 
 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) {
@@ -2127,6 +2147,13 @@ rb_class_alloc(VALUE klass) https://github.com/ruby/ruby/blob/trunk/object.c#L2147
     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));
 
@@ -4268,7 +4295,7 @@ InitVM_Object(void) https://github.com/ruby/ruby/blob/trunk/object.c#L4295
     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 ad2caeb..7903a7c 100644
--- a/test/ruby/test_class.rb
+++ b/test/ruby/test_class.rb
@@ -232,6 +232,11 @@ class TestClass < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_class.rb#L232
     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
+    }
   end
 
   def test_nonascii_name
diff --git a/version.h b/version.h
index cb482e2..c3a2539 100644
--- a/version.h
+++ b/version.h
@@ -1,6 +1,6 @@ https://github.com/ruby/ruby/blob/trunk/version.h#L1
 #define RUBY_VERSION "2.6.7"
 #define RUBY_RELEASE_DATE "2021-02-28"
-#define RUBY_PATCHLEVEL 155
+#define RUBY_PATCHLEVEL 156
 
 #define RUBY_RELEASE_YEAR 2021
 #define RUBY_RELEASE_MONTH 2
-- 
cgit v1.1


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

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