summaryrefslogtreecommitdiff
path: root/eval_error.c
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2018-12-22 07:14:14 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2018-12-22 07:14:14 +0000
commit18492887224cd1aa1aa862ed0fa5237df31ead81 (patch)
treeb5c777fc0f95e552ef5412958b37a7a765e721fe /eval_error.c
parent65dced4320996dc6a2c20f3dc60061106bb75963 (diff)
downloadruby-18492887224cd1aa1aa862ed0fa5237df31ead81.tar.gz
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
Diffstat (limited to 'eval_error.c')
-rw-r--r--eval_error.c26
1 files changed, 20 insertions, 6 deletions
diff --git a/eval_error.c b/eval_error.c
index f9756f65ac..8ea58a4742 100644
--- a/eval_error.c
+++ b/eval_error.c
@@ -222,23 +222,36 @@ print_backtrace(const VALUE eclass, const VALUE errat, const VALUE str, int reve
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
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 emesg, VALUE errat, VALUE str, VALUE highlig
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);
}
}