Avoid calling 'fprintf' with NULL

Avoid undefined behavior in calls like «fprintf("%s", NULL)».
('lua_writestringerror' is implemented as 'fprintf', and 'lua_tostring'
can return NULL if object is not a string.)
This commit is contained in:
Roberto Ierusalimschy
2020-06-09 16:12:01 -03:00
parent 63295f1f7f
commit 364e569945
3 changed files with 7 additions and 3 deletions

View File

@@ -73,8 +73,10 @@ static void badexit (const char *fmt, const char *s1, const char *s2) {
static int tpanic (lua_State *L) {
const char *msg = lua_tostring(L, -1);
if (msg == NULL) msg = "error object is not a string";
return (badexit("PANIC: unprotected error in call to Lua API (%s)\n",
lua_tostring(L, -1), NULL),
msg, NULL),
0); /* do not return to Lua */
}