mruby/mruby — pull request #447 by tsahara-iij (2012-09-04 07:16:00+00:00)
Title: fix out-of-bound access on compiling empty blocks.

When we run "mruby -e 'p {}'" on OpenBSD, the mruby process will die on SIGSEGV.
gdb says:

#0 lambda_body (s=0x2006e0020, tree=0x20d7406c0, blk=1) at codegen.c:596
596 c = s->iseq[s->pc-1];
(gdb) bt
#0 lambda_body (s=0x2006e0020, tree=0x20d7406c0, blk=1) at codegen.c:596
#1 0x0000000000447b7a in codegen (s=0x208204020, tree=0x20d7406d8, val=1)
at codegen.c:1065
#2 0x000000000044ad58 in gen_call (s=0x208204020, tree=Variable "tree" is not available.
) at codegen.c:734
#3 0x00000000004457bf in codegen (s=0x208204020, tree=0x20d740768, val=1)
at codegen.c:1222
#4 0x0000000000445269 in codegen (s=0x208204020, tree=0x20d740798, val=1)
at codegen.c:951
#5 0x000000000044bb27 in scope_body (s=0x210075020, tree=0x20d7407c8)
at codegen.c:620
#6 0x0000000000447b68 in codegen (s=0x210075020, tree=0x20d7407c8, val=0)
at codegen.c:1217
#7 0x000000000044aac5 in codegen_start (mrb=0x206a9e000, p=0x20d740020)
at codegen.c:2513
#8 0x000000000044aaea in mrb_generate_code (mrb=Variable "mrb" is not available.
) at codegen.c:2524
#9 0x000000000042e2a8 in load_exec (mrb=0x206a9e000, p=0x20d740020,
c=0x204c2f640) at parse.y:4870
#10 0x00000000004437b2 in mrb_load_string_cxt (mrb=0x206a9e000,
s=0x2065011b0 "p {}", c=0x204c2f640) at parse.y:4901
#11 0x00000000004024bb in main (argc=Variable "argc" is not available.
) at ../../src/../tools/mruby/mruby.c:206

On the first line of the backtrace:

596 c = s->iseq[s->pc-1];

s->pc can be 0 (when compiling block has no statement), so s->iseq[s->pc-1] can be s->iseq[-1].