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

Posted by 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]
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.