Forum: Ruby-core [ruby-trunk - Bug #7557][Open] Module#initialize_copy allows to change the superclass of a class

Bcb6acc9d0d9bef99e033b36c3d32ca9?d=identicon&s=25 charliesome (Charlie Somerville) (Guest)
on 2012-12-13 13:51
(Received via mailing list)
Issue #7557 has been reported by charliesome (Charlie Somerville).

----------------------------------------
Bug #7557: Module#initialize_copy allows to change the superclass of a
class
https://bugs.ruby-lang.org/issues/7557

Author: charliesome (Charlie Somerville)
Status: Open
Priority: Normal
Assignee: charliesome (Charlie Somerville)
Category: core
Target version:
ruby -v: ruby 2.0.0dev (2012-12-13 trunk 38363) [x86_64-darwin11.4.0]


=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]
This topic is locked and can not be replied to.