summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorcharliesome <charliesome@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2012-12-13 14:11:01 +0000
committercharliesome <charliesome@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2012-12-13 14:11:01 +0000
commitc01f06089f233e74b9e4c0d41b395264da87062f (patch)
tree38d32bcdd47056203979d1d622e334286b2e7059
parent6b4687e6057bfcdb82e3530386a7011014fb2111 (diff)
* object.c (Init_Object): use rb_mod_init_copy for Class#initialize_copy
* class.c (rb_class_init_copy): rename to class_init_copy_check, performs type checks on arguments to prevent reinitialization of initialized class [ruby-core:50869] [Bug #7557] * class.c (rb_mod_init_copy): use class_init_copy_check if receiver is T_CLASS * test/ruby/test_class.rb (class TestClass): related test git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@38364 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r--ChangeLog8
-rw-r--r--class.c33
-rw-r--r--object.c2
-rw-r--r--test/ruby/test_class.rb15
4 files changed, 41 insertions, 17 deletions
diff --git a/ChangeLog b/ChangeLog
index de7eb59581..e644ee5c84 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+Thu Dec 13 23:10:52 Charlie Somerville <charlie@charliesomerville.com>
+ * object.c (Init_Object): use rb_mod_init_copy for Class#initialize_copy
+ * class.c (rb_class_init_copy): rename to class_init_copy_check, performs type
+ checks on arguments to prevent reinitialization of initialized class
+ [ruby-core:50869] [Bug #7557]
+ * class.c (rb_mod_init_copy): use class_init_copy_check if receiver is T_CLASS
+ * test/ruby/test_class.rb (class TestClass): related test
+
Thu Dec 13 16:53:10 2012 Eric Hodel <drbrain@segment7.net>
* lib/rdoc/class_module.rb: Fixed duplicate comments for classes and
diff --git a/class.c b/class.c
index cb85580d92..badcb3a5f0 100644
--- a/class.c
+++ b/class.c
@@ -159,10 +159,27 @@ clone_const_i(st_data_t key, st_data_t value, st_data_t data)
return clone_const((ID)key, (const rb_const_entry_t *)value, (st_table *)data);
}
+static void
+class_init_copy_check(VALUE clone, VALUE orig)
+{
+ if (orig == rb_cBasicObject) {
+ rb_raise(rb_eTypeError, "can't copy the root class");
+ }
+ if (RCLASS_SUPER(clone) != 0 || clone == rb_cBasicObject) {
+ rb_raise(rb_eTypeError, "already initialized class");
+ }
+ if (FL_TEST(orig, FL_SINGLETON)) {
+ rb_raise(rb_eTypeError, "can't copy singleton class");
+ }
+}
+
/* :nodoc: */
VALUE
rb_mod_init_copy(VALUE clone, VALUE orig)
{
+ if(RB_TYPE_P(clone, T_CLASS)) {
+ class_init_copy_check(clone, orig);
+ }
rb_obj_init_copy(clone, orig);
if (!FL_TEST(CLASS_OF(clone), FL_SINGLETON)) {
RBASIC(clone)->klass = rb_singleton_class_clone(orig);
@@ -202,22 +219,6 @@ rb_mod_init_copy(VALUE clone, VALUE orig)
return clone;
}
-/* :nodoc: */
-VALUE
-rb_class_init_copy(VALUE clone, VALUE orig)
-{
- if (orig == rb_cBasicObject) {
- rb_raise(rb_eTypeError, "can't copy the root class");
- }
- if (RCLASS_SUPER(clone) != 0 || clone == rb_cBasicObject) {
- rb_raise(rb_eTypeError, "already initialized class");
- }
- if (FL_TEST(orig, FL_SINGLETON)) {
- rb_raise(rb_eTypeError, "can't copy singleton class");
- }
- return rb_mod_init_copy(clone, orig);
-}
-
VALUE
rb_singleton_class_clone(VALUE obj)
{
diff --git a/object.c b/object.c
index 7fdc0e2d02..30ca301c94 100644
--- a/object.c
+++ b/object.c
@@ -3101,7 +3101,7 @@ Init_Object(void)
rb_define_method(rb_cClass, "allocate", rb_obj_alloc, 0);
rb_define_method(rb_cClass, "new", rb_class_new_instance, -1);
rb_define_method(rb_cClass, "initialize", rb_class_initialize, -1);
- rb_define_method(rb_cClass, "initialize_copy", rb_class_init_copy, 1); /* in class.c */
+ rb_define_method(rb_cClass, "initialize_copy", rb_mod_init_copy, 1); /* in class.c */
rb_define_method(rb_cClass, "superclass", rb_class_superclass, 0);
rb_define_alloc_func(rb_cClass, rb_class_s_alloc);
rb_undef_method(rb_cClass, "extend_object");
diff --git a/test/ruby/test_class.rb b/test/ruby/test_class.rb
index a3384fa083..e3047ccd5f 100644
--- a/test/ruby/test_class.rb
+++ b/test/ruby/test_class.rb
@@ -270,4 +270,19 @@ class TestClass < Test::Unit::TestCase
bug5274 = StrClone.new("[ruby-dev:44460]")
assert_equal(bug5274, Marshal.load(Marshal.dump(bug5274)))
end
+
+ def test_cannot_reinitialize_class_with_initialize_copy # [ruby-core:50869]
+ assert_in_out_err([], <<-RUBY, ["Object"], [])
+ class Class
+ def initialize_copy(*); super; end
+ end
+
+ class A; end
+ class B; end
+
+ A.send(:initialize_copy, Class.new(B)) rescue nil
+
+ p A.superclass
+ RUBY
+ end
end