Avoid direct access to error messages as instance variables
ref.
The `SystemCallError#to_s` method also needed to be modified, but since it has no functional difference from the `Exception#to_s` method, it will be removed.
MODIFY
include/mruby/error.h
include/mruby/error.h
@@ -30,6 +30,8 @@ MRB_API mrb_value mrb_exc_new_str(mrb_state *mrb, struct RClass* c, mrb_value st
MRB_API mrb_value mrb_make_exception(mrb_state *mrb, mrb_int argc, const mrb_value *argv);
mrb_value mrb_exc_backtrace(mrb_state *mrb, mrb_value exc);
mrb_value mrb_get_backtrace(mrb_state *mrb);
+void mrb_exc_mesg_set(mrb_state *mrb, struct RException *exc, mrb_value mesg);
+mrb_value mrb_exc_mesg_get(mrb_state *mrb, struct RException *exc);
MRB_API mrb_noreturn void mrb_no_method_error(mrb_state *mrb, mrb_sym id, mrb_value args, const char *fmt, ...);

MODIFY
mrbgems/mruby-errno/src/errno.c
mrbgems/mruby-errno/src/errno.c
#include "mruby.h"
#include "mruby/array.h"
#include "mruby/class.h"
+#include "mruby/error.h"
#include "mruby/hash.h"
#include "mruby/numeric.h"
#include "mruby/string.h"
@@ -51,7 +52,7 @@ mrb_sce_init(mrb_state *mrb, mrb_value self, mrb_value m, mrb_int n)
mrb_str_cat2(mrb, str, " - ");
mrb_str_append(mrb, str, m);
}
- mrb_iv_set(mrb, self, MRB_SYM(mesg), str);
+ mrb_exc_mesg_set(mrb, mrb_exc_ptr(self), str);
}
static mrb_value
@@ -89,12 +90,6 @@ mrb_sce_errno(mrb_state *mrb, mrb_value self)
}
}
-static mrb_value
-mrb_sce_to_s(mrb_state *mrb, mrb_value self)
-{
- return mrb_attr_get(mrb, self, MRB_SYM(mesg));
-}
-
static mrb_value
mrb_sce_sys_fail(mrb_state *mrb, mrb_value cls)
{
@@ -132,7 +127,7 @@ mrb_exxx_init(mrb_state *mrb, mrb_value self)
mrb_str_cat2(mrb, str, " - ");
mrb_str_append(mrb, str, m);
}
- mrb_iv_set(mrb, self, MRB_SYM(mesg), str);
+ mrb_exc_mesg_set(mrb, mrb_exc_ptr(self), str);
return self;
}
@@ -147,7 +142,6 @@ mrb_mruby_errno_gem_init(mrb_state *mrb)
sce = mrb_define_class(mrb, "SystemCallError", ste);
mrb_define_class_method(mrb, sce, "_sys_fail", mrb_sce_sys_fail, MRB_ARGS_REQ(1));
mrb_define_method(mrb, sce, "errno", mrb_sce_errno, MRB_ARGS_NONE());
- mrb_define_method(mrb, sce, "to_s", mrb_sce_to_s, MRB_ARGS_NONE());
mrb_define_method(mrb, sce, "initialize", mrb_sce_init_m, MRB_ARGS_ARG(1, 1));
eno = mrb_define_module_id(mrb, MRB_SYM(Errno));

MODIFY
src/error.c
src/error.c
#include <mruby/throw.h>
#include <mruby/presym.h>
-static void
-exc_mesg_set(mrb_state *mrb, struct RException *exc, mrb_value mesg)
+void
+mrb_exc_mesg_set(mrb_state *mrb, struct RException *exc, mrb_value mesg)
{
if (mrb_string_p(mesg)) {
exc->flags |= MRB_EXC_MESG_STRING_FLAG;
@@ -36,8 +36,8 @@ exc_mesg_set(mrb_state *mrb, struct RException *exc, mrb_value mesg)
}
}
-static mrb_value
-exc_mesg_get(mrb_state *mrb, struct RException *exc)
+mrb_value
+mrb_exc_mesg_get(mrb_state *mrb, struct RException *exc)
{
if ((exc->flags & MRB_EXC_MESG_STRING_FLAG) != 0) {
return mrb_obj_value(exc->mesg);
@@ -52,9 +52,9 @@ mrb_exc_new_str(mrb_state *mrb, struct RClass* c, mrb_value str)
{
mrb_ensure_string_type(mrb, str);
- struct RBasic* e = mrb_obj_alloc(mrb, MRB_TT_EXCEPTION, c);
+ struct RException *e = MRB_OBJ_ALLOC(mrb, MRB_TT_EXCEPTION, c);
mrb_value exc = mrb_obj_value(e);
- mrb_iv_set(mrb, exc, MRB_SYM(mesg), str);
+ mrb_exc_mesg_set(mrb, e, str);
return exc;
}
@@ -78,7 +78,7 @@ exc_initialize(mrb_state *mrb, mrb_value exc)
mrb_value mesg;
if (mrb_get_args(mrb, "|o", &mesg) == 1) {
- exc_mesg_set(mrb, mrb_exc_ptr(exc), mesg);
+ mrb_exc_mesg_set(mrb, mrb_exc_ptr(exc), mesg);
}
return exc;
}
@@ -107,7 +107,7 @@ exc_exception(mrb_state *mrb, mrb_value self)
if (argc == 0) return self;
if (mrb_obj_equal(mrb, self, a)) return self;
exc = mrb_obj_clone(mrb, self);
- exc_mesg_set(mrb, mrb_exc_ptr(exc), a);
+ mrb_exc_mesg_set(mrb, mrb_exc_ptr(exc), a);
return exc;
}
@@ -123,7 +123,7 @@ exc_exception(mrb_state *mrb, mrb_value self)
static mrb_value
exc_to_s(mrb_state *mrb, mrb_value exc)
{
- mrb_value mesg = exc_mesg_get(mrb, mrb_exc_ptr(exc));
+ mrb_value mesg = mrb_exc_mesg_get(mrb, mrb_exc_ptr(exc));
struct RObject *p;
if (!mrb_string_p(mesg)) {
@@ -163,7 +163,7 @@ exc_message(mrb_state *mrb, mrb_value exc)
mrb_value
mrb_exc_inspect(mrb_state *mrb, mrb_value exc)
{
- mrb_value mesg = exc_mesg_get(mrb, mrb_exc_ptr(exc));
+ mrb_value mesg = mrb_exc_mesg_get(mrb, mrb_exc_ptr(exc));
mrb_value cname = mrb_mod_to_s(mrb, mrb_obj_value(mrb_obj_class(mrb, exc)));
mesg = mrb_obj_as_string(mrb, mesg);
return RSTRING_LEN(mesg) == 0 ? cname : mrb_format(mrb, "%v (%v)", mesg, cname);

Avoid direct access to error messages as instance variables

ref. #2485

The `SystemCallError#to_s` method also needed to be modified, but since it has no functional difference from the `Exception#to_s` method, it will be removed.