From 72d1a790cfe0e4a457db98c587f1acaa5e39f001 Mon Sep 17 00:00:00 2001 From: Nobuyoshi Nakada Date: Tue, 8 Aug 2023 19:03:38 +0900 Subject: [PATCH] [Bug #19833] Fix index underflow at superclasses of `BasicObject` --- object.c | 4 ++++ test/ruby/test_class.rb | 7 +++++++ 2 files changed, 11 insertions(+) diff --git a/object.c b/object.c index 823b8df2cf..45a571adac 100644 --- a/object.c +++ b/object.c @@ -2165,6 +2165,10 @@ rb_class_superclass(VALUE klass) if (klass == rb_cBasicObject) return Qnil; rb_raise(rb_eTypeError, "uninitialized class"); } + + if (!RCLASS_SUPERCLASS_DEPTH(klass)) { + return Qnil; + } else { super = RCLASS_SUPERCLASSES(klass)[RCLASS_SUPERCLASS_DEPTH(klass) - 1]; RUBY_ASSERT(RB_TYPE_P(klass, T_CLASS)); diff --git a/test/ruby/test_class.rb b/test/ruby/test_class.rb index 98acbaf67b..a8a019cee2 100644 --- a/test/ruby/test_class.rb +++ b/test/ruby/test_class.rb @@ -96,6 +96,13 @@ class TestClass < Test::Unit::TestCase def test_superclass_of_basicobject assert_equal(nil, BasicObject.superclass) + + assert_separately([], "#{<<~"begin;"}\n#{<<~'end;'}") + begin; + module Mod end + BasicObject.include(Mod) + assert_equal(nil, BasicObject.superclass) + end; end def test_module_function