Skip to content

Commit 1849288

Browse files
committed
Fix for circular causes
* eval_error.c (show_cause): get rid of infinite recursion on circular causes. [Bug #15447] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@66493 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
1 parent 65dced4 commit 1849288

File tree

2 files changed

+33
-6
lines changed

2 files changed

+33
-6
lines changed

eval_error.c

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -222,23 +222,36 @@ print_backtrace(const VALUE eclass, const VALUE errat, const VALUE str, int reve
222222

223223
VALUE rb_get_message(VALUE exc);
224224

225+
static int
226+
shown_cause_p(VALUE cause, VALUE *shown_causes)
227+
{
228+
VALUE shown = *shown_causes;
229+
if (!shown) {
230+
*shown_causes = shown = rb_obj_hide(rb_ident_hash_new());
231+
}
232+
if (rb_hash_has_key(shown, cause)) return TRUE;
233+
rb_hash_aset(shown, cause, Qtrue);
234+
return FALSE;
235+
}
236+
225237
static void
226-
show_cause(VALUE errinfo, VALUE str, VALUE highlight, VALUE reverse)
238+
show_cause(VALUE errinfo, VALUE str, VALUE highlight, VALUE reverse, VALUE *shown_causes)
227239
{
228240
VALUE cause = rb_attr_get(errinfo, id_cause);
229-
if (!NIL_P(cause) && rb_obj_is_kind_of(cause, rb_eException)) {
241+
if (!NIL_P(cause) && rb_obj_is_kind_of(cause, rb_eException) &&
242+
!shown_cause_p(cause, shown_causes)) {
230243
volatile VALUE eclass = CLASS_OF(cause);
231244
VALUE errat = rb_get_backtrace(cause);
232245
VALUE emesg = rb_get_message(cause);
233246
if (reverse) {
234-
show_cause(cause, str, highlight, reverse);
247+
show_cause(cause, str, highlight, reverse, shown_causes);
235248
print_backtrace(eclass, errat, str, TRUE);
236249
print_errinfo(eclass, errat, emesg, str, highlight!=0);
237250
}
238251
else {
239252
print_errinfo(eclass, errat, emesg, str, highlight!=0);
240253
print_backtrace(eclass, errat, str, FALSE);
241-
show_cause(cause, str, highlight, reverse);
254+
show_cause(cause, str, highlight, reverse, shown_causes);
242255
}
243256
}
244257
}
@@ -247,6 +260,7 @@ void
247260
rb_error_write(VALUE errinfo, VALUE emesg, VALUE errat, VALUE str, VALUE highlight, VALUE reverse)
248261
{
249262
volatile VALUE eclass;
263+
VALUE shown_causes = 0;
250264

251265
if (NIL_P(errinfo))
252266
return;
@@ -277,14 +291,14 @@ rb_error_write(VALUE errinfo, VALUE emesg, VALUE errat, VALUE str, VALUE highlig
277291
len = p - (msg = buff);
278292
}
279293
write_warn2(str, msg, len);
280-
show_cause(errinfo, str, highlight, reverse);
294+
show_cause(errinfo, str, highlight, reverse, &shown_causes);
281295
print_backtrace(eclass, errat, str, TRUE);
282296
print_errinfo(eclass, errat, emesg, str, highlight!=0);
283297
}
284298
else {
285299
print_errinfo(eclass, errat, emesg, str, highlight!=0);
286300
print_backtrace(eclass, errat, str, FALSE);
287-
show_cause(errinfo, str, highlight, reverse);
301+
show_cause(errinfo, str, highlight, reverse, &shown_causes);
288302
}
289303
}
290304

test/ruby/test_exception.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1353,6 +1353,19 @@ def test_non_exception_cause
13531353
assert_in_out_err([], code, [], /foo/, success: false, timeout: 2)
13541354
end
13551355

1356+
def test_circular_cause_handle
1357+
errs = [/.*error 1.*\n/, /.*error 2.*\n/, /.*error 1.*/m]
1358+
assert_in_out_err([], "#{<<~"begin;"}\n#{<<~'end;'}", [], errs, success: false, timeout: 2)
1359+
begin;
1360+
begin
1361+
raise "error 1"
1362+
rescue => e1
1363+
raise "error 2" rescue e2 = $!
1364+
raise e1, cause: e2
1365+
end
1366+
end;
1367+
end
1368+
13561369
def test_super_in_method_missing
13571370
assert_separately([], "#{<<~"begin;"}\n#{<<~'end;'}")
13581371
begin;

0 commit comments

Comments
 (0)