aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNobuyoshi Nakada <nobu@ruby-lang.org>2022-12-19 14:10:58 +0900
committerNobuyoshi Nakada <nobu@ruby-lang.org>2022-12-20 14:12:38 +0900
commit2e7e153a2af1456515d43b6381e38534b069b1c2 (patch)
treea7c1730d6b86170e9e80f2e9705ea824ed517829
parent18ba89093a0b214cd89f1567c037c239f094496d (diff)
downloadruby-2e7e153a2af1456515d43b6381e38534b069b1c2.tar.gz
[Bug #19242] Prohibit circular causes to be loaded
-rw-r--r--error.c4
-rw-r--r--eval.c4
-rw-r--r--eval_error.c11
-rw-r--r--test/ruby/test_exception.rb12
4 files changed, 31 insertions, 0 deletions
diff --git a/error.c b/error.c
index bf3725bc47..4bdd04bbc8 100644
--- a/error.c
+++ b/error.c
@@ -2959,6 +2959,8 @@ ivar_copy_i(st_data_t key, st_data_t val, st_data_t exc)
return ST_CONTINUE;
}
+void rb_exc_check_circular_cause(VALUE exc);
+
static VALUE
exception_loader(VALUE exc, VALUE obj)
{
@@ -2973,6 +2975,8 @@ exception_loader(VALUE exc, VALUE obj)
rb_ivar_foreach(obj, ivar_copy_i, exc);
+ rb_exc_check_circular_cause(exc);
+
if (rb_attr_get(exc, id_bt) == rb_attr_get(exc, id_bt_locations)) {
rb_ivar_set(exc, id_bt_locations, Qnil);
}
diff --git a/eval.c b/eval.c
index e1bbf6f2c3..ff17ad343c 100644
--- a/eval.c
+++ b/eval.c
@@ -537,12 +537,16 @@ exc_setup_message(const rb_execution_context_t *ec, VALUE mesg, VALUE *cause)
}
if (!nocircular && !NIL_P(*cause) && !UNDEF_P(*cause) && *cause != mesg) {
+#if 0 /* maybe critical for some cases */
+ rb_exc_check_circular_cause(*cause);
+#else
VALUE c = *cause;
while (!NIL_P(c = rb_attr_get(c, id_cause))) {
if (c == mesg) {
rb_raise(rb_eArgError, "circular causes");
}
}
+#endif
}
return mesg;
}
diff --git a/eval_error.c b/eval_error.c
index e02f54e8e6..948a205dd9 100644
--- a/eval_error.c
+++ b/eval_error.c
@@ -291,6 +291,17 @@ show_cause(VALUE errinfo, VALUE str, VALUE opt, VALUE highlight, VALUE reverse,
}
void
+rb_exc_check_circular_cause(VALUE exc)
+{
+ VALUE cause = exc, shown_causes = 0;
+ do {
+ if (shown_cause_p(cause, &shown_causes)) {
+ rb_raise(rb_eArgError, "circular causes");
+ }
+ } while (!NIL_P(cause = rb_attr_get(cause, id_cause)));
+}
+
+void
rb_error_write(VALUE errinfo, VALUE emesg, VALUE errat, VALUE str, VALUE opt, VALUE highlight, VALUE reverse)
{
volatile VALUE eclass;
diff --git a/test/ruby/test_exception.rb b/test/ruby/test_exception.rb
index 9bed2c76cd..c2eada0bfb 100644
--- a/test/ruby/test_exception.rb
+++ b/test/ruby/test_exception.rb
@@ -1412,6 +1412,18 @@ $stderr = $stdout; raise "\x82\xa0"') do |outs, errs, status|
end;
end
+ def test_marshal_circular_cause
+ begin
+ raise RuntimeError, "err", [], cause: Exception.new
+ rescue => e
+ end
+ dump = Marshal.dump(e).sub(/o:\x0EException\x08;.0;.0;.0/, "@\x05")
+ assert_raise_with_message(ArgumentError, /circular cause/, ->{dump.inspect}) do
+ e = Marshal.load(dump)
+ assert_same(e, e.cause)
+ end
+ end
+
def test_super_in_method_missing
assert_separately([], "#{<<~"begin;"}\n#{<<~'end;'}")
begin;