Backport #7557
closedModule#initialize_copy allows to change the superclass of a class
Description
=begin
You can abuse (({Module#initialize_copy})) to change the superclass of a class, because of this line of code in (({rb_mod_init_copy})):
RCLASS_SUPER(clone) = RCLASS_SUPER(orig);
(({Class#initialize_copy})) does perform some checks to stop you calling (({#initialize})) on an already initialized class, but this can be subverted by redefining it to call (({super})) from Ruby-land.
Here's an example:
class Class
def initialize_copy(*)
super
end
def superclass=(klass)
initialize_copy(Class.new(klass))
end
end
Symbol.superclass = String
p :hello.class.ancestors
=> [Symbol, String, Comparable, Object, Kernel, BasicObject]¶
Updated by Anonymous almost 12 years ago
- Status changed from Open to Closed
- % Done changed from 0 to 100
This issue was solved with changeset r38364.
Charlie, thank you for reporting this issue.
Your contribution to Ruby is greatly appreciated.
May Ruby be with you.
- 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
- class.c (rb_class_init_copy): rename to class_init_copy_check, performs type
Updated by nobu (Nobuyoshi Nakada) almost 12 years ago
- Tracker changed from Bug to Backport
- Project changed from Ruby master to Backport193
- Category deleted (
core) - Status changed from Closed to Assigned
- Priority changed from Normal to 5
- Assignee changed from Anonymous to usa (Usaku NAKAMURA)
Updated by usa (Usaku NAKAMURA) almost 12 years ago
- Status changed from Assigned to Closed
This issue was solved with changeset r38507.
Charlie, thank you for reporting this issue.
Your contribution to Ruby is greatly appreciated.
May Ruby be with you.
merge revision(s) 38364,38366: [Backport #7557]
* 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