Project

General

Profile

Actions

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.

Status:
Closed
[ruby-core:50869]

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]


Related issues 1 (0 open1 closed)

Related to Backport193 - Backport #8434: rb_class_init_copy was removed in r38364, but it is still referenced in intern.hClosedusa (Usaku NAKAMURA)05/22/2013Actions
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0