ruby-changes:54284
From: nobu <ko1@a...>
Date: Sat, 22 Dec 2018 16:14:18 +0900 (JST)
Subject: [ruby-changes:54284] nobu:r66493 (trunk): Fix for circular causes
nobu 2018-12-22 16:14:14 +0900 (Sat, 22 Dec 2018) New Revision: 66493 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=66493 Log: Fix for circular causes * eval_error.c (show_cause): get rid of infinite recursion on circular causes. [Bug #15447] Modified files: trunk/eval_error.c trunk/test/ruby/test_exception.rb Index: eval_error.c =================================================================== --- eval_error.c (revision 66492) +++ eval_error.c (revision 66493) @@ -222,23 +222,36 @@ print_backtrace(const VALUE eclass, cons https://github.com/ruby/ruby/blob/trunk/eval_error.c#L222 VALUE rb_get_message(VALUE exc); +static int +shown_cause_p(VALUE cause, VALUE *shown_causes) +{ + VALUE shown = *shown_causes; + if (!shown) { + *shown_causes = shown = rb_obj_hide(rb_ident_hash_new()); + } + if (rb_hash_has_key(shown, cause)) return TRUE; + rb_hash_aset(shown, cause, Qtrue); + return FALSE; +} + static void -show_cause(VALUE errinfo, VALUE str, VALUE highlight, VALUE reverse) +show_cause(VALUE errinfo, VALUE str, VALUE highlight, VALUE reverse, VALUE *shown_causes) { VALUE cause = rb_attr_get(errinfo, id_cause); - if (!NIL_P(cause) && rb_obj_is_kind_of(cause, rb_eException)) { + if (!NIL_P(cause) && rb_obj_is_kind_of(cause, rb_eException) && + !shown_cause_p(cause, shown_causes)) { volatile VALUE eclass = CLASS_OF(cause); VALUE errat = rb_get_backtrace(cause); VALUE emesg = rb_get_message(cause); if (reverse) { - show_cause(cause, str, highlight, reverse); + show_cause(cause, str, highlight, reverse, shown_causes); print_backtrace(eclass, errat, str, TRUE); print_errinfo(eclass, errat, emesg, str, highlight!=0); } else { print_errinfo(eclass, errat, emesg, str, highlight!=0); print_backtrace(eclass, errat, str, FALSE); - show_cause(cause, str, highlight, reverse); + show_cause(cause, str, highlight, reverse, shown_causes); } } } @@ -247,6 +260,7 @@ void https://github.com/ruby/ruby/blob/trunk/eval_error.c#L260 rb_error_write(VALUE errinfo, VALUE emesg, VALUE errat, VALUE str, VALUE highlight, VALUE reverse) { volatile VALUE eclass; + VALUE shown_causes = 0; if (NIL_P(errinfo)) return; @@ -277,14 +291,14 @@ rb_error_write(VALUE errinfo, VALUE emes https://github.com/ruby/ruby/blob/trunk/eval_error.c#L291 len = p - (msg = buff); } write_warn2(str, msg, len); - show_cause(errinfo, str, highlight, reverse); + show_cause(errinfo, str, highlight, reverse, &shown_causes); print_backtrace(eclass, errat, str, TRUE); print_errinfo(eclass, errat, emesg, str, highlight!=0); } else { print_errinfo(eclass, errat, emesg, str, highlight!=0); print_backtrace(eclass, errat, str, FALSE); - show_cause(errinfo, str, highlight, reverse); + show_cause(errinfo, str, highlight, reverse, &shown_causes); } } Index: test/ruby/test_exception.rb =================================================================== --- test/ruby/test_exception.rb (revision 66492) +++ test/ruby/test_exception.rb (revision 66493) @@ -1353,6 +1353,19 @@ $stderr = $stdout; raise "\x82\xa0"') do https://github.com/ruby/ruby/blob/trunk/test/ruby/test_exception.rb#L1353 assert_in_out_err([], code, [], /foo/, success: false, timeout: 2) 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; + begin + raise "error 1" + rescue => e1 + raise "error 2" rescue e2 = $! + raise e1, cause: e2 + end + end; + end + def test_super_in_method_missing assert_separately([], "#{<<~"begin;"}\n#{<<~'end;'}") begin; -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/