Avoid possible infinite recursion in mrb_print_error(); ref
MODIFY
src/error.c
src/error.c
@@ -148,11 +148,12 @@ exc_inspect(mrb_state *mrb, mrb_value exc)
append_mesg = RSTRING_LEN(mesg) > 0;
}
- if (!mrb_nil_p(file) && !mrb_nil_p(line)) {
+ if (mrb_string_p(file) && mrb_fixnum_p(line)) {
+ char buf[32];
+
str = mrb_str_dup(mrb, file);
- mrb_str_cat_lit(mrb, str, ":");
- mrb_str_append(mrb, str, line);
- mrb_str_cat_lit(mrb, str, ": ");
+ snprintf(buf, sizeof(buf), ":%d: ", mrb_fixnum(line));
+ mrb_str_cat_cstr(mrb, str, buf);
if (append_mesg) {
mrb_str_cat_str(mrb, str, mesg);
mrb_str_cat_lit(mrb, str, " (");
@@ -317,9 +318,7 @@ mrb_exc_raise(mrb_state *mrb, mrb_value exc)
MRB_API mrb_noreturn void
mrb_raise(mrb_state *mrb, struct RClass *c, const char *msg)
{
- mrb_value mesg;
- mesg = mrb_str_new_cstr(mrb, msg);
- mrb_exc_raise(mrb, mrb_exc_new_str(mrb, c, mesg));
+ mrb_exc_raise(mrb, mrb_exc_new_str(mrb, c, mrb_str_new_cstr(mrb, msg)));
}
MRB_API mrb_value
@@ -381,32 +380,41 @@ mrb_format(mrb_state *mrb, const char *format, ...)
return str;
}
+static mrb_noreturn void
+raise_va(mrb_state *mrb, struct RClass *c, const char *fmt, va_list ap, int argc, mrb_value *argv)
+{
+ mrb_value mesg;
+
+ mesg = mrb_vformat(mrb, fmt, ap);
+ if (argv == NULL) {
+ argv = &mesg;
+ }
+ else {
+ argv[0] = mesg;
+ }
+ mrb_exc_raise(mrb, mrb_obj_new(mrb, c, argc+1, argv));
+}
+
MRB_API mrb_noreturn void
mrb_raisef(mrb_state *mrb, struct RClass *c, const char *fmt, ...)
{
va_list args;
- mrb_value mesg;
va_start(args, fmt);
- mesg = mrb_vformat(mrb, fmt, args);
+ raise_va(mrb, c, fmt, args, 0, NULL);
va_end(args);
- mrb_exc_raise(mrb, mrb_exc_new_str(mrb, c, mesg));
}
MRB_API mrb_noreturn void
mrb_name_error(mrb_state *mrb, mrb_sym id, const char *fmt, ...)
{
- mrb_value exc;
mrb_value argv[2];
va_list args;
va_start(args, fmt);
- argv[0] = mrb_vformat(mrb, fmt, args);
- va_end(args);
-
argv[1] = mrb_symbol_value(id);
- exc = mrb_obj_new(mrb, E_NAME_ERROR, 2, argv);
- mrb_exc_raise(mrb, exc);
+ raise_va(mrb, E_NAME_ERROR, fmt, args, 1, argv);
+ va_end(args);
}
MRB_API void

Avoid possible infinite recursion in mrb_print_error(); ref #3517