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

ruby-changes:54301

From: nobu <ko1@a...>
Date: Sun, 23 Dec 2018 20:11:43 +0900 (JST)
Subject: [ruby-changes:54301] nobu:r66510 (trunk): Prohibit circular causes [Bug #15447]

nobu	2018-12-23 20:11:36 +0900 (Sun, 23 Dec 2018)

  New Revision: 66510

  https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=66510

  Log:
    Prohibit circular causes [Bug #15447]

  Modified files:
    trunk/eval.c
    trunk/test/ruby/test_exception.rb
Index: eval.c
===================================================================
--- eval.c	(revision 66509)
+++ eval.c	(revision 66510)
@@ -491,6 +491,7 @@ static inline VALUE https://github.com/ruby/ruby/blob/trunk/eval.c#L491
 exc_setup_message(const rb_execution_context_t *ec, VALUE mesg, VALUE *cause)
 {
     int nocause = 0;
+    int nocircular = 0;
 
     if (NIL_P(mesg)) {
 	mesg = ec->errinfo;
@@ -500,18 +501,32 @@ exc_setup_message(const rb_execution_con https://github.com/ruby/ruby/blob/trunk/eval.c#L501
     if (NIL_P(mesg)) {
 	mesg = rb_exc_new(rb_eRuntimeError, 0, 0);
 	nocause = 0;
+        nocircular = 1;
     }
     if (*cause == Qundef) {
 	if (nocause) {
 	    *cause = Qnil;
+            nocircular = 1;
 	}
 	else if (!rb_ivar_defined(mesg, id_cause)) {
 	    *cause = get_ec_errinfo(ec);
 	}
+        else {
+            nocircular = 1;
+        }
     }
     else if (!NIL_P(*cause) && !rb_obj_is_kind_of(*cause, rb_eException)) {
         rb_raise(rb_eTypeError, "exception object expected");
     }
+
+    if (!nocircular && !NIL_P(*cause) && *cause != Qundef && *cause != mesg) {
+        VALUE c = *cause;
+        while (!NIL_P(c = rb_attr_get(c, id_cause))) {
+            if (c == mesg) {
+                rb_raise(rb_eArgError, "circular causes");
+            }
+        }
+    }
     return mesg;
 }
 
Index: test/ruby/test_exception.rb
===================================================================
--- test/ruby/test_exception.rb	(revision 66509)
+++ test/ruby/test_exception.rb	(revision 66510)
@@ -1352,14 +1352,11 @@ $stderr = $stdout; raise "\x82\xa0"') do https://github.com/ruby/ruby/blob/trunk/test/ruby/test_exception.rb#L1352
   end
 
   def test_circular_cause_handle
-    errs = [/.*error 1.*\n/, /.*error 2.*\n/, /.*error 1.*/m]
-    assert_in_out_err([], "#{<<~"begin;"}\n#{<<~'end;'}", [], errs, success: false, timeout: 2)
-    begin;
+    assert_raise_with_message(ArgumentError, /circular cause/) do
       begin
         raise "error 1"
       rescue => e1
-        raise "error 2" rescue e2 = $!
-        raise e1, cause: e2
+        raise "error 2" rescue raise e1, cause: $!
       end
     end;
   end

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

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