1
0
mirror of https://github.com/janet-lang/janet synced 2025-10-16 00:07:40 +00:00

Update Makefile and fix fiber issues. When

creating an invalid fiber with the C api, the
program could segfault. This protects against this kind
of segafault.
This commit is contained in:
Calvin Rose
2018-12-02 15:29:21 -05:00
parent 0469bdb06c
commit 11cd1279d7
4 changed files with 56 additions and 31 deletions

View File

@@ -50,7 +50,8 @@ static JanetFiber *make_fiber(int32_t capacity) {
/* Initialize a new fiber */
JanetFiber *janet_fiber(JanetFunction *callee, int32_t capacity) {
JanetFiber *fiber = make_fiber(capacity);
janet_fiber_funcframe(fiber, callee);
if (janet_fiber_funcframe(fiber, callee))
janet_fiber_set_status(fiber, JANET_STATUS_ERROR);
return fiber;
}
@@ -64,7 +65,8 @@ JanetFiber *janet_fiber_n(JanetFunction *callee, int32_t capacity, const Janet *
}
memcpy(fiber->data + fiber->stacktop, argv, argn * sizeof(Janet));
fiber->stacktop = newstacktop;
janet_fiber_funcframe(fiber, callee);
if (janet_fiber_funcframe(fiber, callee))
janet_fiber_set_status(fiber, JANET_STATUS_ERROR);
return fiber;
}
@@ -128,13 +130,7 @@ int janet_fiber_funcframe(JanetFiber *fiber, JanetFunction *func) {
int32_t oldframe = fiber->frame;
int32_t nextframe = fiber->stackstart;
int32_t nextstacktop = nextframe + func->def->slotcount + JANET_FRAME_SIZE;
/* Check strict arity */
if (func->def->flags & JANET_FUNCDEF_FLAG_FIXARITY) {
if (func->def->arity != (fiber->stacktop - fiber->stackstart)) {
return 1;
}
}
int32_t next_arity = fiber->stacktop - fiber->stackstart;
if (fiber->capacity < nextstacktop) {
janet_fiber_setcapacity(fiber, 2 * nextstacktop);
@@ -167,6 +163,13 @@ int janet_fiber_funcframe(JanetFiber *fiber, JanetFunction *func) {
}
}
/* Check strict arity AFTER getting fiber to valid state. */
if (func->def->flags & JANET_FUNCDEF_FLAG_FIXARITY) {
if (func->def->arity != next_arity) {
return 1;
}
}
/* Good return */
return 0;
}
@@ -192,15 +195,9 @@ int janet_fiber_funcframe_tail(JanetFiber *fiber, JanetFunction *func) {
int32_t i;
int32_t nextframetop = fiber->frame + func->def->slotcount;
int32_t nextstacktop = nextframetop + JANET_FRAME_SIZE;
int32_t next_arity = fiber->stacktop - fiber->stackstart;
int32_t stacksize;
/* Check strict arity */
if (func->def->flags & JANET_FUNCDEF_FLAG_FIXARITY) {
if (func->def->arity != (fiber->stacktop - fiber->stackstart)) {
return 1;
}
}
if (fiber->capacity < nextstacktop) {
janet_fiber_setcapacity(fiber, 2 * nextstacktop);
}
@@ -244,6 +241,13 @@ int janet_fiber_funcframe_tail(JanetFiber *fiber, JanetFunction *func) {
janet_fiber_frame(fiber)->pc = func->def->bytecode;
janet_fiber_frame(fiber)->flags |= JANET_STACKFRAME_TAILCALL;
/* Check strict arity AFTER getting fiber to valid state. */
if (func->def->flags & JANET_FUNCDEF_FLAG_FIXARITY) {
if (func->def->arity != next_arity) {
return 1;
}
}
/* Good return */
return 0;
}