Project

General

Profile

Bug #6863

Class#initialize infinite loops when passed self

Added by charliesome (Charlie Somerville) about 7 years ago. Updated about 7 years ago.

Status:
Closed
Priority:
Normal
Assignee:
-
Target version:
-
ruby -v:
2.0.0dev
Backport:
[ruby-core:47148]

Description

=begin
The following code sends Ruby into an infinite loop:

x = Class.allocate
x.send :initialize, x

The problem appears to be in rb_class_initialize (object.c:1564) when Ruby attempts to call (({#inherited})) on the newly created class. The method lookup infinite loops as it tries to descend the ancestor chain.

This should probably throw a (({TypeError})) instead.
=end


Files

type-error-on-class-inheriting-from-self.patch (832 Bytes) type-error-on-class-inheriting-from-self.patch charliesome (Charlie Somerville), 08/13/2012 10:35 PM

Associated revisions

Revision 95283581
Added by nobu (Nobuyoshi Nakada) about 7 years ago

object.c: forbid uninitialized class

  • object.c (rb_class_initialize): forbid inheriting uninitialized class. another class tree not based on BasicObject cannot exist. [ruby-core:47148][Bug #6863]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@36714 b2dd03c8-39d4-4d8f-98ff-823fe69b080e

Revision 36714
Added by nobu (Nobuyoshi Nakada) about 7 years ago

object.c: forbid uninitialized class

  • object.c (rb_class_initialize): forbid inheriting uninitialized class. another class tree not based on BasicObject cannot exist. [ruby-core:47148][Bug #6863]

Revision 36714
Added by nobu (Nobuyoshi Nakada) about 7 years ago

object.c: forbid uninitialized class

  • object.c (rb_class_initialize): forbid inheriting uninitialized class. another class tree not based on BasicObject cannot exist. [ruby-core:47148][Bug #6863]

Revision 36714
Added by nobu (Nobuyoshi Nakada) about 7 years ago

object.c: forbid uninitialized class

  • object.c (rb_class_initialize): forbid inheriting uninitialized class. another class tree not based on BasicObject cannot exist. [ruby-core:47148][Bug #6863]

Revision 36714
Added by nobu (Nobuyoshi Nakada) about 7 years ago

object.c: forbid uninitialized class

  • object.c (rb_class_initialize): forbid inheriting uninitialized class. another class tree not based on BasicObject cannot exist. [ruby-core:47148][Bug #6863]

Revision 36714
Added by nobu (Nobuyoshi Nakada) about 7 years ago

object.c: forbid uninitialized class

  • object.c (rb_class_initialize): forbid inheriting uninitialized class. another class tree not based on BasicObject cannot exist. [ruby-core:47148][Bug #6863]

Revision 36714
Added by nobu (Nobuyoshi Nakada) about 7 years ago

object.c: forbid uninitialized class

  • object.c (rb_class_initialize): forbid inheriting uninitialized class. another class tree not based on BasicObject cannot exist. [ruby-core:47148][Bug #6863]

History

Updated by charliesome (Charlie Somerville) about 7 years ago

I've attached a patch that raises a TypeError with the message "circular inheritance detected" when it detects a class either inheriting from itself, or a class inheriting from a subclass of itself.

Updated by shyouhei (Shyouhei Urabe) about 7 years ago

charliesome (Charlie Somerville) wrote:

x = Class.allocate

Why on earth is this allowed? I have no idea how useful an non-initiaized class is.

So instead of checking circular inheritance, I would propose to simply disable Class.allocate.

Updated by charliesome (Charlie Somerville) about 7 years ago

The problem with disabling Class.allocate is that we don't know if anyone is using it out in the wild. Detecting circular inheritance and throwing might be more ideal since it has a smaller breakage potential.

#4

Updated by nobu (Nobuyoshi Nakada) about 7 years ago

  • Status changed from Open to Closed
  • % Done changed from 0 to 100

This issue was solved with changeset r36714.
Charlie, thank you for reporting this issue.
Your contribution to Ruby is greatly appreciated.
May Ruby be with you.


object.c: forbid uninitialized class

  • object.c (rb_class_initialize): forbid inheriting uninitialized class. another class tree not based on BasicObject cannot exist. [ruby-core:47148][Bug #6863]

Also available in: Atom PDF