Error object cannot be nil

Lua will change a nil as error object to a string message, so that it
never reports an error with nil as the error object.
This commit is contained in:
Roberto Ierusalimschy
2025-02-28 14:53:58 -03:00
parent 127a8e80fe
commit ee99452158
4 changed files with 23 additions and 8 deletions

10
ldo.c
View File

@@ -112,12 +112,16 @@ void luaD_seterrorobj (lua_State *L, TStatus errcode, StkId oldtop) {
break;
}
default: {
lua_assert(errorstatus(errcode)); /* real error */
setobjs2s(L, oldtop, L->top.p - 1); /* error message on current top */
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;
}
}
L->top.p = oldtop + 1;
L->top.p = oldtop + 1; /* top goes back to old top plus error object */
}