Skip to content

Commit

Permalink
luv_cfpcall: Fix stack balancing after an uncaught error
Browse files Browse the repository at this point in the history
The default error handling code for printing uncaught errors uses
luaL_tolstring(). This returns the resulting string, but also pushes it onto
the stack. After we have printed it, it is safe to pop from the stack.

Leaving it on the stack causes the stack to be unbalanced.

Fixes #735 (where a test case can be found)
  • Loading branch information
mwild1 committed Nov 14, 2024
1 parent e2d3d18 commit 219c88d
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/luv.c
Original file line number Diff line number Diff line change
Expand Up @@ -726,9 +726,11 @@ LUALIB_API int luv_cfpcall(lua_State* L, int nargs, int nresult, int flags) {
case LUA_ERRRUN:
case LUA_ERRERR:
default:
if ((flags & LUVF_CALLBACK_NOERRMSG) == 0)
if ((flags & LUVF_CALLBACK_NOERRMSG) == 0) {
fprintf(stderr, "Uncaught Error: %s\n",
luaL_tolstring(L, lua_absindex(L, -1), NULL));
lua_pop(L, 1); // Remove error string pushed by luaL_tolstring()
}
if ((flags & LUVF_CALLBACK_NOEXIT) == 0)
exit(-1);
lua_pop(L, 1);
Expand Down

0 comments on commit 219c88d

Please sign in to comment.