From d28265256110a0c5437247d443ddedc2a7aab116 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Sun, 8 Nov 2020 11:52:26 -0300 Subject: [PATCH] Bug when growing a stack When a stack grows, its extra area can be in use, and it becomes part of the common area. So, the extra area must be kept correct all the times. (Bug introduced by commit 5aa36e894f5.) --- ldo.c | 2 +- lgc.c | 4 ++-- lstate.c | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/ldo.c b/ldo.c index a60972b2..4b55c31c 100644 --- a/ldo.c +++ b/ldo.c @@ -192,7 +192,7 @@ int luaD_reallocstack (lua_State *L, int newsize, int raiseerror) { else return 0; /* do not raise an error */ } for (; lim < newsize; lim++) - setnilvalue(s2v(newstack + lim)); /* erase new segment */ + setnilvalue(s2v(newstack + lim + EXTRA_STACK)); /* erase new segment */ correctstack(L, L->stack, newstack); L->stack = newstack; L->stack_last = L->stack + newsize; diff --git a/lgc.c b/lgc.c index 5dba56fc..bab9beb1 100644 --- a/lgc.c +++ b/lgc.c @@ -632,8 +632,8 @@ static int traversethread (global_State *g, lua_State *th) { for (uv = th->openupval; uv != NULL; uv = uv->u.open.next) markobject(g, uv); /* open upvalues cannot be collected */ if (g->gcstate == GCSatomic) { /* final traversal? */ - for (; o < th->stack_last; o++) /* clear not-marked stack slice */ - setnilvalue(s2v(o)); + for (; o < th->stack_last + EXTRA_STACK; o++) + setnilvalue(s2v(o)); /* clear dead stack slice */ /* 'remarkupvals' may have removed thread from 'twups' list */ if (!isintwups(th) && th->openupval != NULL) { th->twups = g->twups; /* link it back to the list */ diff --git a/lstate.c b/lstate.c index 42274292..1c7b8791 100644 --- a/lstate.c +++ b/lstate.c @@ -181,7 +181,7 @@ static void stack_init (lua_State *L1, lua_State *L) { int i; CallInfo *ci; /* initialize stack array */ L1->stack = luaM_newvector(L, BASIC_STACK_SIZE + EXTRA_STACK, StackValue); - for (i = 0; i < BASIC_STACK_SIZE; i++) + for (i = 0; i < BASIC_STACK_SIZE + EXTRA_STACK; i++) setnilvalue(s2v(L1->stack + i)); /* erase new stack */ L1->top = L1->stack; L1->stack_last = L1->stack + BASIC_STACK_SIZE;