Backport #7557
closed
Module#initialize_copy allows to change the superclass of a class
Added by Anonymous almost 12 years ago.
Updated almost 12 years ago.
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]¶
- 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
- 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)
- 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
Also available in: Atom
PDF
Like0
Like0Like0Like0