Project

General

Profile

Actions

Backport #7557

closed

Module#initialize_copy allows to change the superclass of a class

Added by Anonymous over 11 years ago. Updated over 11 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 #1

Updated by Anonymous over 11 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
Actions #2

Updated by nobu (Nobuyoshi Nakada) over 11 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)
Actions #3

Updated by usa (Usaku NAKAMURA) over 11 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
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0