Log all Lua errors

Wesnoth's Lua submodule is built with LUAI_TRY() defined to catch a
std::exception, push its what() string onto the Lua stack, and call the
error handler given via lua_pcall() (which push_error_handler() and
luaW_pcall_internal() set to Lua's debug.traceback()) or in Lua via
xpcall().

If lua_pcall() returns an error code, luaW_pcall() logs an error, but
only if it finds an exception string on the stack (due to an apparent
oversight in commit 3820b14eb8, version 1.9.5) and that exception
string doesn't contain "~lua:" (oversight in commit e5562a1b52,
version 1.9.0).  But stock LUAI_TRY() doesn't push an exception string
onto the stack, so luaW_pcall() won't log the error.

Either way, luaW_pcall() always returns false when lua_pcall() returns
an error, so the calling C++ code works the same with either Wesnoth
or stock LUAI_TRY().  The only consequence is that strict mode doesn't
break, because the error isn't logged.

This will cause the filter_formula_unit_error test to return a result
of "PASS TEST (0)" instead of "BROKE STRICT (PASS) (9)" with stock
LUAI_TRY().  In other words, the test passes normally either way, but
strict mode doesn't break.

Fix this by making luaW_pcall() log all errors, including those without
exception strings on the stack, as well as those with exception strings
containing "~lua:".
This commit is contained in:
P. J. McDermott 2024-01-19 22:07:53 -05:00 committed by Pentarctagon
parent 83ad348037
commit 5a0a3d0d72
2 changed files with 5 additions and 0 deletions

View file

@ -1,3 +1,4 @@
### Miscellaneous and Bug Fixes
* Fix delayed handling of Lua jailbreak exceptions (quit to menu or desktop, wesnothd connection errors, etc.) thrown during Lua `pcall()` and `xpcall()`, which could accumulate and cause wesnoth to abort. (PR #8234)
* This bug has existed since jailbreak exceptions were introduced in version 1.9.5.
* Fix strict mode not catching Lua errors without exception strings (bug since 1.9.5) or with exception strings containing "~lua:" (bug since 1.9.0). (PR #8234)

View file

@ -1140,6 +1140,8 @@ bool luaW_pcall(lua_State *L, int nArgs, int nRets, bool allow_wml_error)
/*
* When an exception is thrown which doesn't derive from
* std::exception m will be nullptr pointer.
* When adding a new conditional branch, remember to log the
* error with ERR_LUA or ERR_WML.
*/
char const *m = lua_tostring(L, -1);
if(m) {
@ -1156,12 +1158,14 @@ bool luaW_pcall(lua_State *L, int nArgs, int nRets, bool allow_wml_error)
#pragma warning (pop)
#endif
e = em;
ERR_LUA << std::string(m, e ? e - m : strlen(m));
chat_message("Lua error", std::string(m, e ? e - m : strlen(m)));
} else {
ERR_LUA << m;
chat_message("Lua error", m);
}
} else {
ERR_LUA << "Lua caught unknown exception";
chat_message("Lua caught unknown exception", "");
}
lua_pop(L, 1);