Fix segmentation fault when Module#name
returns non string value [Bug #17754]
* Add test for NoMethodError#to_s does not segfault * Ensure no segfault even if Module#name is overridden
This commit is contained in:
parent
95d9fe9538
commit
0a544c0c35
Notes:
git
2021-03-28 08:48:17 +09:00
Merged: https://github.com/ruby/ruby/pull/4328 Merged-By: nobu <nobu@ruby-lang.org>
4
error.c
4
error.c
@ -1965,8 +1965,10 @@ name_err_mesg_to_str(VALUE obj)
|
|||||||
d = rb_protect(name_err_mesg_receiver_name, obj, &state);
|
d = rb_protect(name_err_mesg_receiver_name, obj, &state);
|
||||||
if (state || d == Qundef || d == Qnil)
|
if (state || d == Qundef || d == Qnil)
|
||||||
d = rb_protect(rb_inspect, obj, &state);
|
d = rb_protect(rb_inspect, obj, &state);
|
||||||
if (state)
|
if (state) {
|
||||||
rb_set_errinfo(Qnil);
|
rb_set_errinfo(Qnil);
|
||||||
|
}
|
||||||
|
d = rb_check_string_type(d);
|
||||||
if (NIL_P(d)) {
|
if (NIL_P(d)) {
|
||||||
d = rb_any_to_s(obj);
|
d = rb_any_to_s(obj);
|
||||||
}
|
}
|
||||||
|
@ -90,4 +90,20 @@ class TestNoMethodError < Test::Unit::TestCase
|
|||||||
str.__send__(id)
|
str.__send__(id)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_to_s
|
||||||
|
pre = Module.new do
|
||||||
|
def name
|
||||||
|
BasicObject.new
|
||||||
|
end
|
||||||
|
end
|
||||||
|
mod = Module.new
|
||||||
|
mod.singleton_class.prepend(pre)
|
||||||
|
|
||||||
|
err = assert_raise(NoMethodError) do
|
||||||
|
mod.this_method_does_not_exist
|
||||||
|
end
|
||||||
|
|
||||||
|
assert_match(/undefined method.+this_method_does_not_exist.+for.+Module/, err.to_s)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user