Skip to content

Commit

Permalink
Protect ensure clause lambdas from GC; fix #3491
Browse files Browse the repository at this point in the history
  • Loading branch information
matz committed Apr 3, 2017
1 parent 6a992a2 commit b8461c8
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 6 deletions.
8 changes: 3 additions & 5 deletions src/gc.c
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,7 @@ mark_context_stack(mrb_state *mrb, struct mrb_context *c)
static void
mark_context(mrb_state *mrb, struct mrb_context *c)
{
int i, e = 0;
int i;
mrb_callinfo *ci;

/* mark stack */
Expand All @@ -574,16 +574,14 @@ mark_context(mrb_state *mrb, struct mrb_context *c)
/* mark VM stack */
if (c->cibase) {
for (ci = c->cibase; ci <= c->ci; ci++) {
if (ci->eidx > e) {
e = ci->eidx;
}
mrb_gc_mark(mrb, (struct RBasic*)ci->env);
mrb_gc_mark(mrb, (struct RBasic*)ci->proc);
mrb_gc_mark(mrb, (struct RBasic*)ci->target_class);
}
}
/* mark ensure stack */
for (i=0; i<e; i++) {
for (i=0; i<c->esize; i++) {
if (c->ensure[i] == NULL) break;
mrb_gc_mark(mrb, (struct RBasic*)c->ensure[i]);
}
/* mark fibers */
Expand Down
3 changes: 2 additions & 1 deletion src/vm.c
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@ ecall(mrb_state *mrb, int i)
}
p = mrb->c->ensure[i];
if (!p) return;
mrb->c->ensure[i] = NULL;
if (mrb->c->ci->eidx > i)
mrb->c->ci->eidx = i;
cioff = mrb->c->ci - mrb->c->cibase;
Expand All @@ -310,7 +311,6 @@ ecall(mrb_state *mrb, int i)
mrb->c->stack = mrb->c->stack + ci[-1].nregs;
exc = mrb->exc; mrb->exc = 0;
mrb_run(mrb, p, *self);
mrb->c->ensure[i] = NULL;
mrb->c->ci = mrb->c->cibase + cioff;
if (!mrb->exc) mrb->exc = exc;
}
Expand Down Expand Up @@ -1148,6 +1148,7 @@ mrb_vm_exec(mrb_state *mrb, struct RProc *proc, mrb_code *pc)
mrb->c->ensure = (struct RProc **)mrb_realloc(mrb, mrb->c->ensure, sizeof(struct RProc*) * mrb->c->esize);
}
mrb->c->ensure[mrb->c->ci->eidx++] = p;
mrb->c->ensure[mrb->c->ci->eidx] = NULL;
ARENA_RESTORE(mrb, ai);
NEXT;
}
Expand Down

0 comments on commit b8461c8

Please sign in to comment.