mruby/mruby — pull request #1303 by kyab (2013-06-24 17:15:50+00:00)
Title: Fix crash on load *mrb

If call mrb_dump_irep_binary() with start irep with non-zero , load/run crashes.

test code:
```
#include
#include

#include "mruby.h"
#include "mruby/dump.h"
#include "mruby/compile.h"
#include "mruby/proc.h"

void dumpMRB(mrb_state *mrb, const char *filename, const char *code){
mrbc_context *cxt;

cxt = mrbc_context_new(mrb);
cxt->capture_errors = 1;
//cxt->no_exec = 1;
cxt->filename = (char *)filename;

struct mrb_parser_state *parser = mrb_parser_new(mrb);
parser->s = code;
parser->send = code + strlen(code);
//parser->lineno = 1;
mrb_parser_parse(parser, cxt);

int n = mrb_generate_code(mrb,parser);
if (n < 0) {
printf("failed to generate code. err=%d\n",n);
return;
}

FILE *f = fopen(filename,"w+b");
int ret = mrb_dump_irep_binary(mrb, n, 0, f);
if (ret != MRB_DUMP_OK){
printf("failed to create %s\n",filename);
fclose(f);
return;
}
fclose(f);
mrb_parser_free(parser);
mrbc_context_free(mrb,cxt);

}

void runMRB(mrb_state *mrb, const char *filename){
FILE *f = fopen(filename, "rb");
int n = mrb_read_irep_file(mrb, f);

if (n < 0){
printf("failed to load mrb:%s\n", filename);
return;
}
mrb_run(mrb, mrb_proc_new(mrb, mrb->irep[n]), mrb_top_self(mrb));
fclose(f);
}

int main(int argc, const char * argv[])
{
mrb_state *mrb1,*mrb2;
mrb1 = mrb_open();
mrb2 = mrb_open();
dumpMRB(mrb1,"one.mrb","puts :one; def func1;puts :func1;end");
dumpMRB(mrb1,"two.mrb","puts :two; func1 ");
runMRB(mrb2, "one.mrb");
runMRB(mrb2, "two.mrb");

mrb_close(mrb1);
mrb_close(mrb2);
return 0;
}

```