Skip to content

Commit

Permalink
Raises Exception if raising exception class is redefined
Browse files Browse the repository at this point in the history
close #3384
This issue was reported by https://hackerone.com/brakhane
  • Loading branch information
matz committed Jan 11, 2017
1 parent 7523cdf commit 44edc51
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 18 deletions.
44 changes: 26 additions & 18 deletions include/mruby.h
Original file line number Diff line number Diff line change
Expand Up @@ -607,6 +607,14 @@ MRB_API mrb_bool mrb_class_defined(mrb_state *mrb, const char *name);
*/
MRB_API struct RClass * mrb_class_get(mrb_state *mrb, const char *name);

/**
* Gets a exception class.
* @param [mrb_state*] mrb The current mruby state.
* @param [const char *] name The name of the class.
* @return [struct RClass *] A reference to the class.
*/
MRB_API struct RClass * mrb_exc_get(mrb_state *mrb, const char *name);

/**
* Returns an mrb_bool. True if inner class was defined, and false if the inner class was not defined.
*
Expand Down Expand Up @@ -1091,23 +1099,23 @@ MRB_API void mrb_print_error(mrb_state *mrb);
+ those E_* macros requires mrb_state* variable named mrb.
+ exception objects obtained from those macros are local to mrb
*/
#define E_RUNTIME_ERROR (mrb_class_get(mrb, "RuntimeError"))
#define E_TYPE_ERROR (mrb_class_get(mrb, "TypeError"))
#define E_ARGUMENT_ERROR (mrb_class_get(mrb, "ArgumentError"))
#define E_INDEX_ERROR (mrb_class_get(mrb, "IndexError"))
#define E_RANGE_ERROR (mrb_class_get(mrb, "RangeError"))
#define E_NAME_ERROR (mrb_class_get(mrb, "NameError"))
#define E_NOMETHOD_ERROR (mrb_class_get(mrb, "NoMethodError"))
#define E_SCRIPT_ERROR (mrb_class_get(mrb, "ScriptError"))
#define E_SYNTAX_ERROR (mrb_class_get(mrb, "SyntaxError"))
#define E_LOCALJUMP_ERROR (mrb_class_get(mrb, "LocalJumpError"))
#define E_REGEXP_ERROR (mrb_class_get(mrb, "RegexpError"))
#define E_SYSSTACK_ERROR (mrb_class_get(mrb, "SystemStackError"))

#define E_NOTIMP_ERROR (mrb_class_get(mrb, "NotImplementedError"))
#define E_FLOATDOMAIN_ERROR (mrb_class_get(mrb, "FloatDomainError"))

#define E_KEY_ERROR (mrb_class_get(mrb, "KeyError"))
#define E_RUNTIME_ERROR (mrb_exc_get(mrb, "RuntimeError"))
#define E_TYPE_ERROR (mrb_exc_get(mrb, "TypeError"))
#define E_ARGUMENT_ERROR (mrb_exc_get(mrb, "ArgumentError"))
#define E_INDEX_ERROR (mrb_exc_get(mrb, "IndexError"))
#define E_RANGE_ERROR (mrb_exc_get(mrb, "RangeError"))
#define E_NAME_ERROR (mrb_exc_get(mrb, "NameError"))
#define E_NOMETHOD_ERROR (mrb_exc_get(mrb, "NoMethodError"))
#define E_SCRIPT_ERROR (mrb_exc_get(mrb, "ScriptError"))
#define E_SYNTAX_ERROR (mrb_exc_get(mrb, "SyntaxError"))
#define E_LOCALJUMP_ERROR (mrb_exc_get(mrb, "LocalJumpError"))
#define E_REGEXP_ERROR (mrb_exc_get(mrb, "RegexpError"))
#define E_SYSSTACK_ERROR (mrb_exc_get(mrb, "SystemStackError"))

#define E_NOTIMP_ERROR (mrb_exc_get(mrb, "NotImplementedError"))
#define E_FLOATDOMAIN_ERROR (mrb_exc_get(mrb, "FloatDomainError"))

#define E_KEY_ERROR (mrb_exc_get(mrb, "KeyError"))

MRB_API mrb_value mrb_yield(mrb_state *mrb, mrb_value b, mrb_value arg);
MRB_API mrb_value mrb_yield_argv(mrb_state *mrb, mrb_value b, mrb_int argc, const mrb_value *argv);
Expand Down Expand Up @@ -1160,7 +1168,7 @@ MRB_API mrb_value mrb_fiber_yield(mrb_state *mrb, mrb_int argc, const mrb_value
*
* @mrbgem mruby-fiber
*/
#define E_FIBER_ERROR (mrb_class_get(mrb, "FiberError"))
#define E_FIBER_ERROR (mrb_exc_get(mrb, "FiberError"))

/* memory pool implementation */
typedef struct mrb_pool mrb_pool;
Expand Down
14 changes: 14 additions & 0 deletions src/class.c
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,20 @@ mrb_class_get(mrb_state *mrb, const char *name)
return mrb_class_get_under(mrb, mrb->object_class, name);
}

MRB_API struct RClass *
mrb_exc_get(mrb_state *mrb, const char *name)
{
struct RClass *exc = mrb_class_get_under(mrb, mrb->object_class, name);
struct RClass *e = exc;

while (e) {
if (e == mrb->eException_class)
return exc;
e = e->super;
}
return mrb->eException_class;
}

MRB_API struct RClass *
mrb_module_get_under(mrb_state *mrb, struct RClass *outer, const char *name)
{
Expand Down

0 comments on commit 44edc51

Please sign in to comment.