Unverified Commit c7f4df38 authored by Gregory Hainaut's avatar Gregory Hainaut Committed by GitHub
Browse files

validate the lua stack (#221)

in case of wrong number of push/pop, a message is printed. And the stack is restored properly
parent 4ea399d2
......@@ -51,6 +51,7 @@ void lutro_joystick_init()
void lutro_joystickevent(lua_State* L)
{
ENTER_LUA_STACK
int i, u;
int16_t state;
......@@ -77,6 +78,7 @@ void lutro_joystickevent(lua_State* L)
}
}
lua_pop(L, 1); // pop traceback
EXIT_LUA_STACK
}
/**
......
......@@ -183,6 +183,7 @@ void lutro_keyboard_init()
*/
void lutro_keyboardevent(lua_State* L)
{
ENTER_LUA_STACK
int16_t is_down;
for (unsigned i = 0; i < RETROK_LAST; i++)
......@@ -218,6 +219,7 @@ void lutro_keyboardevent(lua_State* L)
keyboard_cache[i] = is_down;
}
}
EXIT_LUA_STACK
}
/**
......
......@@ -49,6 +49,7 @@
static lua_State *L;
static int16_t input_cache[16];
int g_lua_stack = 0;
lutro_settings_t settings = {
.width = 320,
......@@ -569,7 +570,7 @@ int lutro_load(const char *path)
void lutro_gamepadevent(lua_State* L)
{
int oldtop = lua_gettop(L);
ENTER_LUA_STACK
unsigned i;
for (i = 0; i < 16; i++)
......@@ -597,7 +598,7 @@ void lutro_gamepadevent(lua_State* L)
lua_pop(L, 1); // pop getglobal lutro
}
}
lua_settop(L, oldtop);
EXIT_LUA_STACK
}
void lutro_run(double delta)
......
......@@ -16,6 +16,16 @@
#define VERSION_PATCH 1
#define VERSION_STRING "0.0.1"
extern int g_lua_stack;
#define ENTER_LUA_STACK do { g_lua_stack = lua_gettop(L); } while(0);
#define EXIT_LUA_STACK do { \
int stack = lua_gettop(L); \
if (stack != g_lua_stack) { \
printf("invalid stack setup (got %d expected %d) on %s\n", stack, g_lua_stack, __func__); \
lua_settop(L, g_lua_stack); \
} \
} while (0);
typedef struct lutro_settings_t {
int width;
int height;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment