Commit d6cdaf55 authored by mercury233's avatar mercury233 Committed by GitHub

check lua stack before pushing (#378)

parent e52629db
...@@ -47,6 +47,8 @@ interpreter::~interpreter() { ...@@ -47,6 +47,8 @@ interpreter::~interpreter() {
} }
int32 interpreter::register_card(card *pcard) { int32 interpreter::register_card(card *pcard) {
//create a card in by userdata //create a card in by userdata
luaL_checkstack(lua_state, 1, NULL);
luaL_checkstack(current_state, 1, NULL);
card ** ppcard = (card**) lua_newuserdata(lua_state, sizeof(card*)); card ** ppcard = (card**) lua_newuserdata(lua_state, sizeof(card*));
*ppcard = pcard; *ppcard = pcard;
pcard->ref_handle = luaL_ref(lua_state, LUA_REGISTRYINDEX); pcard->ref_handle = luaL_ref(lua_state, LUA_REGISTRYINDEX);
...@@ -74,6 +76,7 @@ void interpreter::register_effect(effect *peffect) { ...@@ -74,6 +76,7 @@ void interpreter::register_effect(effect *peffect) {
if (!peffect) if (!peffect)
return; return;
//create a effect in by userdata //create a effect in by userdata
luaL_checkstack(lua_state, 3, NULL);
effect ** ppeffect = (effect**) lua_newuserdata(lua_state, sizeof(effect*)); effect ** ppeffect = (effect**) lua_newuserdata(lua_state, sizeof(effect*));
*ppeffect = peffect; *ppeffect = peffect;
peffect->ref_handle = luaL_ref(lua_state, LUA_REGISTRYINDEX); peffect->ref_handle = luaL_ref(lua_state, LUA_REGISTRYINDEX);
...@@ -103,6 +106,7 @@ void interpreter::register_group(group *pgroup) { ...@@ -103,6 +106,7 @@ void interpreter::register_group(group *pgroup) {
if (!pgroup) if (!pgroup)
return; return;
//create a group in by userdata //create a group in by userdata
luaL_checkstack(lua_state, 3, NULL);
group ** ppgroup = (group**) lua_newuserdata(lua_state, sizeof(group*)); group ** ppgroup = (group**) lua_newuserdata(lua_state, sizeof(group*));
*ppgroup = pgroup; *ppgroup = pgroup;
pgroup->ref_handle = luaL_ref(lua_state, LUA_REGISTRYINDEX); pgroup->ref_handle = luaL_ref(lua_state, LUA_REGISTRYINDEX);
...@@ -138,9 +142,11 @@ int32 interpreter::load_script(const char* script_name) { ...@@ -138,9 +142,11 @@ int32 interpreter::load_script(const char* script_name) {
int32 interpreter::load_card_script(uint32 code) { int32 interpreter::load_card_script(uint32 code) {
char class_name[20]; char class_name[20];
sprintf(class_name, "c%d", code); sprintf(class_name, "c%d", code);
luaL_checkstack(current_state, 1, NULL);
lua_getglobal(current_state, class_name); lua_getglobal(current_state, class_name);
//if script is not loaded, create and load it //if script is not loaded, create and load it
if (lua_isnil(current_state, -1)) { if (lua_isnil(current_state, -1)) {
luaL_checkstack(current_state, 5, NULL);
lua_pop(current_state, 1); lua_pop(current_state, 1);
//create a table & set metatable //create a table & set metatable
lua_createtable(current_state, 0, 0); lua_createtable(current_state, 0, 0);
...@@ -174,6 +180,7 @@ void interpreter::add_param(ptr param, int32 type, bool front) { ...@@ -174,6 +180,7 @@ void interpreter::add_param(ptr param, int32 type, bool front) {
void interpreter::push_param(lua_State* L, bool is_coroutine) { void interpreter::push_param(lua_State* L, bool is_coroutine) {
int32 pushed = 0; int32 pushed = 0;
for (const auto& it : params) { for (const auto& it : params) {
luaL_checkstack(L, 1, NULL);
uint32 type = it.second; uint32 type = it.second;
switch(type) { switch(type) {
case PARAM_TYPE_INT: case PARAM_TYPE_INT:
...@@ -283,6 +290,7 @@ int32 interpreter::call_card_function(card* pcard, const char* f, uint32 param_c ...@@ -283,6 +290,7 @@ int32 interpreter::call_card_function(card* pcard, const char* f, uint32 param_c
return OPERATION_FAIL; return OPERATION_FAIL;
} }
card2value(current_state, pcard); card2value(current_state, pcard);
luaL_checkstack(current_state, 1, NULL);
lua_getfield(current_state, -1, f); lua_getfield(current_state, -1, f);
if (!lua_isfunction(current_state, -1)) { if (!lua_isfunction(current_state, -1)) {
sprintf(pduel->strbuffer, "\"CallCardFunction\"(c%d.%s): attempt to call an error function", pcard->data.code, f); sprintf(pduel->strbuffer, "\"CallCardFunction\"(c%d.%s): attempt to call an error function", pcard->data.code, f);
...@@ -323,6 +331,7 @@ int32 interpreter::call_code_function(uint32 code, const char* f, uint32 param_c ...@@ -323,6 +331,7 @@ int32 interpreter::call_code_function(uint32 code, const char* f, uint32 param_c
return OPERATION_FAIL; return OPERATION_FAIL;
} }
load_card_script(code); load_card_script(code);
luaL_checkstack(current_state, 1, NULL);
lua_getfield(current_state, -1, f); lua_getfield(current_state, -1, f);
if (!lua_isfunction(current_state, -1)) { if (!lua_isfunction(current_state, -1)) {
sprintf(pduel->strbuffer, "\"CallCodeFunction\": attempt to call an error function"); sprintf(pduel->strbuffer, "\"CallCodeFunction\": attempt to call an error function");
...@@ -386,6 +395,7 @@ int32 interpreter::check_matching(card* pcard, int32 findex, int32 extraargs) { ...@@ -386,6 +395,7 @@ int32 interpreter::check_matching(card* pcard, int32 findex, int32 extraargs) {
return TRUE; return TRUE;
no_action++; no_action++;
call_depth++; call_depth++;
luaL_checkstack(current_state, 1 + extraargs, NULL);
lua_pushvalue(current_state, findex); lua_pushvalue(current_state, findex);
interpreter::card2value(current_state, pcard); interpreter::card2value(current_state, pcard);
for(int32 i = 0; i < extraargs; ++i) for(int32 i = 0; i < extraargs; ++i)
...@@ -417,6 +427,7 @@ int32 interpreter::get_operation_value(card* pcard, int32 findex, int32 extraarg ...@@ -417,6 +427,7 @@ int32 interpreter::get_operation_value(card* pcard, int32 findex, int32 extraarg
return 0; return 0;
no_action++; no_action++;
call_depth++; call_depth++;
luaL_checkstack(current_state, 1 + extraargs, NULL);
lua_pushvalue(current_state, findex); lua_pushvalue(current_state, findex);
interpreter::card2value(current_state, pcard); interpreter::card2value(current_state, pcard);
for(int32 i = 0; i < extraargs; ++i) for(int32 i = 0; i < extraargs; ++i)
...@@ -579,6 +590,7 @@ int32 interpreter::call_coroutine(int32 f, uint32 param_count, uint32 * yield_va ...@@ -579,6 +590,7 @@ int32 interpreter::call_coroutine(int32 f, uint32 param_count, uint32 * yield_va
} }
} }
int32 interpreter::clone_function_ref(int32 func_ref) { int32 interpreter::clone_function_ref(int32 func_ref) {
luaL_checkstack(current_state, 1, NULL);
lua_rawgeti(current_state, LUA_REGISTRYINDEX, func_ref); lua_rawgeti(current_state, LUA_REGISTRYINDEX, func_ref);
int32 ref = luaL_ref(current_state, LUA_REGISTRYINDEX); int32 ref = luaL_ref(current_state, LUA_REGISTRYINDEX);
return ref; return ref;
...@@ -586,6 +598,7 @@ int32 interpreter::clone_function_ref(int32 func_ref) { ...@@ -586,6 +598,7 @@ int32 interpreter::clone_function_ref(int32 func_ref) {
void* interpreter::get_ref_object(int32 ref_handler) { void* interpreter::get_ref_object(int32 ref_handler) {
if(ref_handler == 0) if(ref_handler == 0)
return nullptr; return nullptr;
luaL_checkstack(current_state, 1, NULL);
lua_rawgeti(current_state, LUA_REGISTRYINDEX, ref_handler); lua_rawgeti(current_state, LUA_REGISTRYINDEX, ref_handler);
void* p = *(void**)lua_touserdata(current_state, -1); void* p = *(void**)lua_touserdata(current_state, -1);
lua_pop(current_state, 1); lua_pop(current_state, 1);
...@@ -593,30 +606,35 @@ void* interpreter::get_ref_object(int32 ref_handler) { ...@@ -593,30 +606,35 @@ void* interpreter::get_ref_object(int32 ref_handler) {
} }
//Convert a pointer to a lua value, +1 -0 //Convert a pointer to a lua value, +1 -0
void interpreter::card2value(lua_State* L, card* pcard) { void interpreter::card2value(lua_State* L, card* pcard) {
luaL_checkstack(L, 1, NULL);
if (!pcard || pcard->ref_handle == 0) if (!pcard || pcard->ref_handle == 0)
lua_pushnil(L); lua_pushnil(L);
else else
lua_rawgeti(L, LUA_REGISTRYINDEX, pcard->ref_handle); lua_rawgeti(L, LUA_REGISTRYINDEX, pcard->ref_handle);
} }
void interpreter::group2value(lua_State* L, group* pgroup) { void interpreter::group2value(lua_State* L, group* pgroup) {
luaL_checkstack(L, 1, NULL);
if (!pgroup || pgroup->ref_handle == 0) if (!pgroup || pgroup->ref_handle == 0)
lua_pushnil(L); lua_pushnil(L);
else else
lua_rawgeti(L, LUA_REGISTRYINDEX, pgroup->ref_handle); lua_rawgeti(L, LUA_REGISTRYINDEX, pgroup->ref_handle);
} }
void interpreter::effect2value(lua_State* L, effect* peffect) { void interpreter::effect2value(lua_State* L, effect* peffect) {
luaL_checkstack(L, 1, NULL);
if (!peffect || peffect->ref_handle == 0) if (!peffect || peffect->ref_handle == 0)
lua_pushnil(L); lua_pushnil(L);
else else
lua_rawgeti(L, LUA_REGISTRYINDEX, peffect->ref_handle); lua_rawgeti(L, LUA_REGISTRYINDEX, peffect->ref_handle);
} }
void interpreter::function2value(lua_State* L, int32 func_ref) { void interpreter::function2value(lua_State* L, int32 func_ref) {
luaL_checkstack(L, 1, NULL);
if (!func_ref) if (!func_ref)
lua_pushnil(L); lua_pushnil(L);
else else
lua_rawgeti(L, LUA_REGISTRYINDEX, func_ref); lua_rawgeti(L, LUA_REGISTRYINDEX, func_ref);
} }
int32 interpreter::get_function_handle(lua_State* L, int32 index) { int32 interpreter::get_function_handle(lua_State* L, int32 index) {
luaL_checkstack(L, 1, NULL);
lua_pushvalue(L, index); lua_pushvalue(L, index);
int32 ref = luaL_ref(L, LUA_REGISTRYINDEX); int32 ref = luaL_ref(L, LUA_REGISTRYINDEX);
return ref; return ref;
...@@ -626,6 +644,7 @@ void interpreter::set_duel_info(lua_State* L, duel* pduel) { ...@@ -626,6 +644,7 @@ void interpreter::set_duel_info(lua_State* L, duel* pduel) {
luaL_ref(L, LUA_REGISTRYINDEX); luaL_ref(L, LUA_REGISTRYINDEX);
} }
duel* interpreter::get_duel_info(lua_State * L) { duel* interpreter::get_duel_info(lua_State * L) {
luaL_checkstack(L, 1, NULL);
lua_rawgeti(L, LUA_REGISTRYINDEX, 3); lua_rawgeti(L, LUA_REGISTRYINDEX, 3);
duel* pduel = (duel*)lua_topointer(L, -1); duel* pduel = (duel*)lua_topointer(L, -1);
lua_pop(L, 1); lua_pop(L, 1);
......
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