Fix reporting of Lua stack items from various hook calls (#395)

Some hooks can be called after OnHttpRequest, which may leave an
anchored item on stack, so this have to be taken into account to
avoid spurious reports.
This commit is contained in:
Paul Kulchenko 2022-04-27 21:57:52 -07:00 committed by GitHub
parent e3a7ab1804
commit 9a6bd304a5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 12 deletions

View file

@ -7,13 +7,15 @@
#if !(__ASSEMBLER__ + __LINKER__ + 0) #if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_ COSMOPOLITAN_C_START_
#define AssertLuaStackIsEmpty(L) \ #define AssertLuaStackIsAt(L, level) \
do { \ do { \
if (lua_gettop(L)) { \ if (lua_gettop(L) > level) { \
char *s = LuaFormatStack(L); \ char *s = LuaFormatStack(L); \
WARNF("lua stack should be empty!\n%s", s); \ WARNF("lua stack should be at %d;" \
free(s); \ " extra values ignored:\n%s", \
} \ level, s); \
free(s); \
} \
} while (0) } while (0)
COSMOPOLITAN_C_END_ COSMOPOLITAN_C_END_

View file

@ -1071,7 +1071,7 @@ static bool LuaEvalCode(const char *code) {
lua_pop(L, 1); // pop error lua_pop(L, 1); // pop error
return false; return false;
} }
AssertLuaStackIsEmpty(L); AssertLuaStackIsAt(L, 0);
return true; return true;
} }
@ -1099,7 +1099,7 @@ static bool LuaOnClientConnection(void) {
dropit = false; dropit = false;
} }
lua_pop(L, 1); // pop result or error lua_pop(L, 1); // pop result or error
AssertLuaStackIsEmpty(L); AssertLuaStackIsAt(L, 0);
return dropit; return dropit;
#else #else
return false; return false;
@ -1123,7 +1123,7 @@ static void LuaOnProcessCreate(int pid) {
LogLuaError("OnProcessCreate", lua_tostring(L, -1)); LogLuaError("OnProcessCreate", lua_tostring(L, -1));
lua_pop(L, 1); // pop error lua_pop(L, 1); // pop error
} }
AssertLuaStackIsEmpty(L); AssertLuaStackIsAt(L, 0);
#endif #endif
} }
@ -1136,7 +1136,7 @@ static void LuaOnProcessDestroy(int pid) {
LogLuaError("OnProcessDestroy", lua_tostring(L, -1)); LogLuaError("OnProcessDestroy", lua_tostring(L, -1));
lua_pop(L, 1); // pop error lua_pop(L, 1); // pop error
} }
AssertLuaStackIsEmpty(L); AssertLuaStackIsAt(L, 0);
#endif #endif
} }
@ -1154,12 +1154,13 @@ static inline bool IsHookDefined(const char *s) {
static void CallSimpleHook(const char *s) { static void CallSimpleHook(const char *s) {
#ifndef STATIC #ifndef STATIC
lua_State *L = GL; lua_State *L = GL;
int n = lua_gettop(L);
lua_getglobal(L, s); lua_getglobal(L, s);
if (LuaCallWithTrace(L, 0, 0, NULL) != LUA_OK) { if (LuaCallWithTrace(L, 0, 0, NULL) != LUA_OK) {
LogLuaError(s, lua_tostring(L, -1)); LogLuaError(s, lua_tostring(L, -1));
lua_pop(L, 1); // pop error lua_pop(L, 1); // pop error
} }
AssertLuaStackIsEmpty(L); AssertLuaStackIsAt(L, n);
#endif #endif
} }