proc.c: fix segfault when no singleton class

* proc.c (rb_obj_singleton_method): bail out if the receiver does
  not have the singleton class without accessing the origin class
  not to segfault.  [Bug #14658]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@63068 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2018-04-03 06:57:44 +00:00
parent c7770f90bd
commit 60f0e763a4
2 changed files with 14 additions and 9 deletions

20
proc.c
View File

@ -1771,21 +1771,23 @@ VALUE
rb_obj_singleton_method(VALUE obj, VALUE vid)
{
const rb_method_entry_t *me;
VALUE klass = RCLASS_ORIGIN(rb_singleton_class_get(obj));
VALUE klass = rb_singleton_class_get(obj);
ID id = rb_check_id(&vid);
if (!id) {
if (!NIL_P(klass) &&
respond_to_missing_p(klass, obj, vid, FALSE)) {
id = rb_intern_str(vid);
return mnew_missing(klass, obj, id, rb_cMethod);
}
if (NIL_P(klass) || NIL_P(klass = RCLASS_ORIGIN(klass))) {
undef:
rb_name_err_raise("undefined singleton method `%1$s' for `%2$s'",
obj, vid);
}
if (NIL_P(klass) ||
UNDEFINED_METHOD_ENTRY_P(me = rb_method_entry_at(klass, id)) ||
if (!id) {
if (respond_to_missing_p(klass, obj, vid, FALSE)) {
id = rb_intern_str(vid);
return mnew_missing(klass, obj, id, rb_cMethod);
}
goto undef;
}
me = rb_method_entry_at(klass, id);
if (UNDEFINED_METHOD_ENTRY_P(me) ||
UNDEFINED_REFINED_METHOD_P(me->def)) {
vid = ID2SYM(id);
goto undef;

View File

@ -790,6 +790,9 @@ class TestMethod < Test::Unit::TestCase
class << o; prepend Module.new; end
m = assert_nothing_raised(NameError, bug14658) {o.singleton_method(:bar)}
assert_equal(:bar, m.call, bug14658)
o = Object.new
assert_raise(NameError, bug14658) {o.singleton_method(:bar)}
end
Feature9783 = '[ruby-core:62212] [Feature #9783]'