From 3228a97c6a953dcf397944161bb64b12f1ff5384 Mon Sep 17 00:00:00 2001 From: Roberto I Date: Thu, 23 Apr 2026 17:57:42 -0300 Subject: Bug: 'lua_load' does not preserve the stack 'lua_load' does not preserve the stack through the calls to the reader function, as it should. Immediately after the first call (to detect whether chunk is binary) it adds stuff, and it also adds a new table when starting the compilation of each new function. --- lparser.c | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) (limited to 'lparser.c') diff --git a/lparser.c b/lparser.c index 6b87773e..1850d6dc 100644 --- a/lparser.c +++ b/lparser.c @@ -821,8 +821,7 @@ static void open_func (LexState *ls, FuncState *fs, BlockCnt *bl) { luaC_objbarrier(L, f, f->source); f->maxstacksize = 2; /* registers 0/1 are always valid */ fs->kcache = luaH_new(L); /* create table for function */ - sethvalue2s(L, L->top.p, fs->kcache); /* anchor it */ - luaD_inctop(L); + luaD_anchorobj(L, ls->h, obj2gco(fs->kcache)); /* anchor it */ enterblock(fs, bl, 0); } @@ -831,6 +830,7 @@ static void close_func (LexState *ls) { lua_State *L = ls->L; FuncState *fs = ls->fs; Proto *f = fs->f; + TValue temp; luaK_ret(fs, luaY_nvarstack(fs), 0); /* final return */ leaveblock(fs); lua_assert(fs->bl == NULL); @@ -843,8 +843,10 @@ static void close_func (LexState *ls) { luaM_shrinkvector(L, f->p, f->sizep, fs->np, Proto *); luaM_shrinkvector(L, f->locvars, f->sizelocvars, fs->ndebugvars, LocVar); luaM_shrinkvector(L, f->upvalues, f->sizeupvalues, fs->nups, Upvaldesc); + /* remove kcache table from scanner table ("weigh" its anchor) */ + sethvalue(L, &temp, fs->kcache); /* key to be set to nil */ + luaH_set(L, ls->h, &temp, &G(L)->nilvalue); ls->fs = fs->prev; - L->top.p--; /* pop kcache table */ luaC_checkGC(L); } @@ -2174,16 +2176,14 @@ static void mainfunc (LexState *ls, FuncState *fs) { } -LClosure *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff, +LClosure *luaY_parser (lua_State *L, ZIO *z, Table *anchor, Mbuffer *buff, Dyndata *dyd, const char *name, int firstchar) { LexState lexstate; FuncState funcstate; - LClosure *cl = luaF_newLclosure(L, 1); /* create main closure */ - setclLvalue2s(L, L->top.p, cl); /* anchor it (to avoid being collected) */ - luaD_inctop(L); - lexstate.h = luaH_new(L); /* create table for scanner */ - sethvalue2s(L, L->top.p, lexstate.h); /* anchor it */ - luaD_inctop(L); + LClosure *cl; + lexstate.h = anchor; /* table for scanner */ + cl = luaF_newLclosure(L, 1); /* create main closure */ + luaD_anchorobj(L, anchor, obj2gco(cl)); /* anchor it in scanner table */ funcstate.f = cl->p = luaF_newproto(L); luaC_objbarrier(L, cl, cl->p); funcstate.f->source = luaS_new(L, name); /* create and anchor TString */ @@ -2196,7 +2196,6 @@ LClosure *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff, lua_assert(!funcstate.prev && funcstate.nups == 1 && !lexstate.fs); /* all scopes should be correctly finished */ lua_assert(dyd->actvar.n == 0 && dyd->gt.n == 0 && dyd->label.n == 0); - L->top.p--; /* remove scanner's table */ - return cl; /* closure is on the stack, too */ + return cl; } -- cgit v1.2.3-55-g6feb