'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).
This commit is contained in:
Roberto Ierusalimschy
2025-03-12 15:51:16 -03:00
parent d9e0f64a5d
commit c931d86e98
5 changed files with 25 additions and 22 deletions

36
ldo.c
View File

@@ -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, "<error object is nil>"));
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 */