Restrict total recursion number of ecall(); fix #3789

This commit is contained in:
Yukihiro "Matz" Matsumoto
2018-11-20 09:14:34 +09:00
parent afd46eccd1
commit 86d102350b
2 changed files with 6 additions and 3 deletions
+2 -1
View File
@@ -267,7 +267,8 @@ typedef struct mrb_state {
#else
mrb_atexit_func *atexit_stack;
#endif
mrb_int atexit_stack_len;
uint16_t atexit_stack_len;
uint16_t ecall_nest; /* prevent infinite recursive ecall() */
} mrb_state;
/**
+4 -2
View File
@@ -55,7 +55,7 @@ void abort(void);
/* Maximum depth of ecall() recursion. */
#ifndef MRB_ECALL_DEPTH_MAX
#define MRB_ECALL_DEPTH_MAX 32
#define MRB_ECALL_DEPTH_MAX 512
#endif
/* Maximum stack depth. Should be set lower on memory constrained systems.
@@ -337,7 +337,8 @@ ecall(mrb_state *mrb)
int nregs;
if (i<0) return;
if (ci - c->cibase > MRB_ECALL_DEPTH_MAX) {
/* restrict total call depth of ecall() */
if (++mrb->ecall_nest > MRB_ECALL_DEPTH_MAX) {
mrb_exc_raise(mrb, mrb_obj_value(mrb->stack_err));
}
p = c->ensure[i];
@@ -372,6 +373,7 @@ ecall(mrb_state *mrb)
c->ci = c->cibase + cioff;
if (!mrb->exc) mrb->exc = exc;
mrb_gc_arena_restore(mrb, ai);
mrb->ecall_nest--;
}
#ifndef MRB_FUNCALL_ARGC_MAX