Skip to content

Commit

Permalink
Failed to realloc irep->syms in certain condition.
Browse files Browse the repository at this point in the history
Also msym size changed to 512 from 256.
  • Loading branch information
matz committed Dec 12, 2016
1 parent 9cef265 commit a4ae22a
Showing 1 changed file with 20 additions and 11 deletions.
31 changes: 20 additions & 11 deletions mrbgems/mruby-compiler/core/codegen.c
Original file line number Diff line number Diff line change
Expand Up @@ -500,42 +500,51 @@ new_lit(codegen_scope *s, mrb_value val)
return i;
}

static inline int
/* method symbols should be fit in 9 bits */
#define MAXMSYMLEN 512
/* maximum symbol numbers */
#define MAXSYMLEN 65536

static int
new_msym(codegen_scope *s, mrb_sym sym)
{
size_t i, len;

mrb_assert(s->irep);

len = s->irep->slen;
if (len > 256) len = 256;
if (len > MAXMSYMLEN) len = MAXMSYMLEN;
for (i=0; i<len; i++) {
if (s->irep->syms[i] == sym) return i;
if (s->irep->syms[i] == 0) break;
}
if (i == 256) {
codegen_error(s, "too many symbols (max 256)");
if (i == MAXMSYMLEN) {
codegen_error(s, "too many symbols (max " MRB_STRINGIZE(MAXMSYMLEN) ")");
}
s->irep->syms[i] = sym;
if (i == s->irep->slen) s->irep->slen++;
return i;
}

static inline int
static int
new_sym(codegen_scope *s, mrb_sym sym)
{
size_t i;

for (i=0; i<s->irep->slen; i++) {
if (s->irep->syms[i] == sym) return i;
}
if (s->irep->slen > 125 && s->irep->slen < 256) {
s->irep->syms = (mrb_sym *)codegen_realloc(s, s->irep->syms, sizeof(mrb_sym)*65536);
for (i = 0; i < 256 - s->irep->slen; i++) {
if (s->irep->slen == MAXSYMLEN) {
codegen_error(s, "too many symbols (max " MRB_STRINGIZE(MAXSYMLEN) ")");
}
if (s->scapa == MAXMSYMLEN) {
s->scapa = MAXSYMLEN;
s->irep->syms = (mrb_sym *)codegen_realloc(s, s->irep->syms, sizeof(mrb_sym)*MAXSYMLEN);
for (i = s->irep->slen; i < MAXMSYMLEN; i++) {
static const mrb_sym mrb_sym_zero = { 0 };
s->irep->syms[i + s->irep->slen] = mrb_sym_zero;
s->irep->syms[i] = mrb_sym_zero;
}
s->irep->slen = 256;
s->irep->slen = MAXMSYMLEN;
}
s->irep->syms[s->irep->slen] = sym;
return s->irep->slen++;
Expand Down Expand Up @@ -2760,7 +2769,7 @@ scope_new(mrb_state *mrb, codegen_scope *prev, node *lv)
p->irep->pool = (mrb_value*)mrb_malloc(mrb, sizeof(mrb_value)*p->pcapa);
p->irep->plen = 0;

p->scapa = 256;
p->scapa = MAXMSYMLEN;
p->irep->syms = (mrb_sym*)mrb_malloc(mrb, sizeof(mrb_sym)*p->scapa);
p->irep->slen = 0;

Expand Down

0 comments on commit a4ae22a

Please sign in to comment.