Check iseq buffer size before code emission; fix #4090

The type of `s->pc` is now `uint16_t` that can be overflowed easily.
Need more checks.
parent 814b7b5e
...@@ -151,11 +151,11 @@ new_label(codegen_scope *s) ...@@ -151,11 +151,11 @@ new_label(codegen_scope *s)
static void static void
emit_B(codegen_scope *s, uint32_t pc, uint8_t i) emit_B(codegen_scope *s, uint32_t pc, uint8_t i)
{ {
if (pc >= MAXARG_S || s->icapa >= MAXARG_S) {
codegen_error(s, "too big code block");
}
if (pc >= s->icapa) { if (pc >= s->icapa) {
s->icapa *= 2; s->icapa *= 2;
if (pc >= MAXARG_S) {
codegen_error(s, "too big code block");
}
if (s->icapa > MAXARG_S) { if (s->icapa > MAXARG_S) {
s->icapa = MAXARG_S; s->icapa = MAXARG_S;
} }
...@@ -184,7 +184,8 @@ emit_S(codegen_scope *s, int pc, uint16_t i) ...@@ -184,7 +184,8 @@ emit_S(codegen_scope *s, int pc, uint16_t i)
static void static void
gen_B(codegen_scope *s, uint8_t i) gen_B(codegen_scope *s, uint8_t i)
{ {
emit_B(s, s->pc++, i); emit_B(s, s->pc, i);
s->pc++;
} }
static void static void
...@@ -248,7 +249,6 @@ genop_2(codegen_scope *s, mrb_code i, uint16_t a, uint16_t b) ...@@ -248,7 +249,6 @@ genop_2(codegen_scope *s, mrb_code i, uint16_t a, uint16_t b)
static void static void
genop_3(codegen_scope *s, mrb_code i, uint16_t a, uint16_t b, uint8_t c) genop_3(codegen_scope *s, mrb_code i, uint16_t a, uint16_t b, uint8_t c)
{ {
s->lastpc = s->pc;
genop_2(s, i, a, b); genop_2(s, i, a, b);
gen_B(s, c); gen_B(s, c);
} }
...@@ -256,7 +256,6 @@ genop_3(codegen_scope *s, mrb_code i, uint16_t a, uint16_t b, uint8_t c) ...@@ -256,7 +256,6 @@ genop_3(codegen_scope *s, mrb_code i, uint16_t a, uint16_t b, uint8_t c)
static void static void
genop_2S(codegen_scope *s, mrb_code i, uint16_t a, uint16_t b) genop_2S(codegen_scope *s, mrb_code i, uint16_t a, uint16_t b)
{ {
s->lastpc = s->pc;
genop_1(s, i, a); genop_1(s, i, a);
gen_S(s, b); gen_S(s, b);
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment