From c931d86e98da320c71da70c16d44aa28e9755520 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 12 Mar 2025 15:51:16 -0300 Subject: 'luaD_seterrorobj' should not raise errors This function can be called unprotected, so it should not raise any kind of errors. (It could raise a memory-allocation error when creating a message). --- ldo.c | 36 +++++++++++++++++------------------- 1 file changed, 17 insertions(+), 19 deletions(-) (limited to 'ldo.c') diff --git a/ldo.c b/ldo.c index 84f7bbb2..b0d37bf7 100644 --- a/ldo.c +++ b/ldo.c @@ -102,24 +102,13 @@ struct lua_longjmp { void luaD_seterrorobj (lua_State *L, TStatus errcode, StkId oldtop) { - switch (errcode) { - case LUA_ERRMEM: { /* memory error? */ - setsvalue2s(L, oldtop, G(L)->memerrmsg); /* reuse preregistered msg. */ - break; - } - case LUA_ERRERR: { - setsvalue2s(L, oldtop, luaS_newliteral(L, "error in error handling")); - break; - } - default: { - lua_assert(errorstatus(errcode)); /* must be a real error */ - if (!ttisnil(s2v(L->top.p - 1))) { /* error object is not nil? */ - setobjs2s(L, oldtop, L->top.p - 1); /* move it to 'oldtop' */ - } - else /* change it to a proper message */ - setsvalue2s(L, oldtop, luaS_newliteral(L, "")); - break; - } + if (errcode == LUA_ERRMEM) { /* memory error? */ + setsvalue2s(L, oldtop, G(L)->memerrmsg); /* reuse preregistered msg. */ + } + else { + lua_assert(errorstatus(errcode)); /* must be a real error */ + lua_assert(!ttisnil(s2v(L->top.p - 1))); /* with a non-nil object */ + setobjs2s(L, oldtop, L->top.p - 1); /* move it to 'oldtop' */ } L->top.p = oldtop + 1; /* top goes back to old top plus error object */ } @@ -190,6 +179,15 @@ TStatus luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud) { #define ERRORSTACKSIZE (MAXSTACK + STACKERRSPACE) +/* raise an error while running the message handler */ +l_noret luaD_errerr (lua_State *L) { + TString *msg = luaS_newliteral(L, "error in error handling"); + setsvalue2s(L, L->top.p, msg); + L->top.p++; /* assume EXTRA_STACK */ + luaD_throw(L, LUA_ERRERR); +} + + /* ** In ISO C, any pointer use after the pointer has been deallocated is ** undefined behavior. So, before a stack reallocation, all pointers @@ -317,7 +315,7 @@ int luaD_growstack (lua_State *L, int n, int raiseerror) { a stack error; cannot grow further than that. */ lua_assert(stacksize(L) == ERRORSTACKSIZE); if (raiseerror) - luaD_throw(L, LUA_ERRERR); /* error inside message handler */ + luaD_errerr(L); /* error inside message handler */ return 0; /* if not 'raiseerror', just signal it */ } else if (n < MAXSTACK) { /* avoids arithmetic overflows */ -- cgit v1.2.3-55-g6feb