[前][次][番号順一覧][スレッド一覧]

ruby-changes:65667

From: Kenichi <ko1@a...>
Date: Sun, 28 Mar 2021 08:48:08 +0900 (JST)
Subject: [ruby-changes:65667] 0a544c0c35 (master): Fix segmentation fault when `Module#name` returns non string value [Bug #17754]

https://git.ruby-lang.org/ruby.git/commit/?id=0a544c0c35

From 0a544c0c35f7445d69402d7c53d825384c728017 Mon Sep 17 00:00:00 2001
From: Kenichi Kamiya <kachick1@g...>
Date: Sun, 28 Mar 2021 08:47:42 +0900
Subject: 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
---
 error.c                          |  4 +++-
 test/ruby/test_nomethod_error.rb | 16 ++++++++++++++++
 2 files changed, 19 insertions(+), 1 deletion(-)

diff --git a/error.c b/error.c
index 9da51a3..5dd6b2b 100644
--- a/error.c
+++ b/error.c
@@ -1965,8 +1965,10 @@ name_err_mesg_to_str(VALUE obj) https://github.com/ruby/ruby/blob/trunk/error.c#L1965
 	    d = rb_protect(name_err_mesg_receiver_name, obj, &state);
 	    if (state || d == Qundef || d == Qnil)
 		d = rb_protect(rb_inspect, obj, &state);
-	    if (state)
+	    if (state) {
 		rb_set_errinfo(Qnil);
+	    }
+	    d = rb_check_string_type(d);
 	    if (NIL_P(d)) {
 		d = rb_any_to_s(obj);
 	    }
diff --git a/test/ruby/test_nomethod_error.rb b/test/ruby/test_nomethod_error.rb
index 170a6c6..8b81052 100644
--- a/test/ruby/test_nomethod_error.rb
+++ b/test/ruby/test_nomethod_error.rb
@@ -90,4 +90,20 @@ class TestNoMethodError < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_nomethod_error.rb#L90
       str.__send__(id)
     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
-- 
cgit v1.1


--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]