From 96ac47c25109abfd846eb45ae6cb6b56c8b178e3 Mon Sep 17 00:00:00 2001 From: nobu Date: Tue, 22 Dec 2015 13:15:58 +0000 Subject: gc.c: do not expose internal singleton class * gc.c (internal_object_p): should not expose singleton classes without a metaclass. based on patches by ko1 and shugo. [Bug #11740] * class.c (rb_singleton_class_object_p): added. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@53243 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- test/ruby/test_class.rb | 20 ++++++++++++++++++++ test/ruby/test_objectspace.rb | 7 +++++++ 2 files changed, 27 insertions(+) (limited to 'test') diff --git a/test/ruby/test_class.rb b/test/ruby/test_class.rb index e60db37c15..81c11e6f88 100644 --- a/test/ruby/test_class.rb +++ b/test/ruby/test_class.rb @@ -556,4 +556,24 @@ class TestClass < Test::Unit::TestCase } end; end + + def test_should_not_expose_singleton_class_without_metaclass + assert_normal_exit %q{ + klass = Class.new(Array) + # The metaclass of +klass+ should handle #bla since it should inherit methods from meta:meta:Array + def (Array.singleton_class).bla; :bla; end + hidden = ObjectSpace.each_object(Class).find { |c| klass.is_a? c and c.inspect.include? klass.inspect } + raise unless hidden.nil? + }, '[Bug #11740]' + + assert_normal_exit %q{ + klass = Class.new(Array) + klass.singleton_class + # The metaclass of +klass+ should handle #bla since it should inherit methods from meta:meta:Array + def (Array.singleton_class).bla; :bla; end + hidden = ObjectSpace.each_object(Class).find { |c| klass.is_a? c and c.inspect.include? klass.inspect } + raise if hidden.nil? + }, '[Bug #11740]' + + end end diff --git a/test/ruby/test_objectspace.rb b/test/ruby/test_objectspace.rb index b701e0fc91..ec561bc405 100644 --- a/test/ruby/test_objectspace.rb +++ b/test/ruby/test_objectspace.rb @@ -149,5 +149,12 @@ End } assert(exist, 'Bug #11360') End + + klass = Class.new + instance = klass.new + sclass = instance.singleton_class + meta = klass.singleton_class + assert_kind_of(meta, sclass) + assert_include(ObjectSpace.each_object(meta).to_a, sclass) end end -- cgit v1.2.3