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

View file

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