From 5e26d246a29797a330a8da29cd1d5f6712b407d6 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Tue, 29 May 2012 20:13:54 -0700 Subject: [PATCH] more Lua functions using the wrong stack index. --- src/bindings/helpers.c | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/src/bindings/helpers.c b/src/bindings/helpers.c index dec73f5f8..f2fbae090 100644 --- a/src/bindings/helpers.c +++ b/src/bindings/helpers.c @@ -37,8 +37,7 @@ without prior permission by the authors of Eressea. #include static int -lua_giveitem(unit * s, unit * d, const item_type * itype, int n, - struct order *ord) +lua_giveitem(unit * s, unit * d, const item_type * itype, int n, struct order *ord) { lua_State *L = (lua_State *) global.vm_state; char fname[64]; @@ -46,11 +45,12 @@ lua_giveitem(unit * s, unit * d, const item_type * itype, int n, const char *iname = itype->rtype->_name[0]; assert(s != NULL); - strcat(strcpy(fname, iname), "_give"); + strlcpy(fname, iname, sizeof(fname)); + strlcat(fname, "_give", sizeof(fname)); lua_pushstring(L, fname); lua_rawget(L, LUA_GLOBALSINDEX); - if (lua_isfunction(L, 1)) { + if (lua_isfunction(L, -1)) { tolua_pushusertype(L, s, TOLUA_CAST "unit"); tolua_pushusertype(L, d, TOLUA_CAST "unit"); tolua_pushstring(L, iname); @@ -83,7 +83,7 @@ static int limit_resource(const region * r, const resource_type * rtype) lua_pushstring(L, fname); lua_rawget(L, LUA_GLOBALSINDEX); - if (lua_isfunction(L, 1)) { + if (lua_isfunction(L, -1)) { tolua_pushusertype(L, (void *)r, TOLUA_CAST "region"); if (lua_pcall(L, 1, 1, 0) != 0) { @@ -113,7 +113,7 @@ produce_resource(region * r, const resource_type * rtype, int norders) lua_pushstring(L, fname); lua_rawget(L, LUA_GLOBALSINDEX); - if (lua_isfunction(L, 1)) { + if (lua_isfunction(L, -1)) { tolua_pushusertype(L, (void *)r, TOLUA_CAST "region"); tolua_pushnumber(L, (lua_Number) norders); @@ -142,7 +142,7 @@ static int lc_age(struct attrib *a) lua_pushstring(L, fname); lua_rawget(L, LUA_GLOBALSINDEX); - if (lua_isfunction(L, 1)) { + if (lua_isfunction(L, -1)) { tolua_pushusertype(L, (void *)b, TOLUA_CAST "building"); if (fparam) { tolua_pushstring(L, fparam); @@ -256,7 +256,7 @@ static int lua_initfamiliar(unit * u) lua_pushstring(L, fname); lua_rawget(L, LUA_GLOBALSINDEX); - if (lua_isfunction(L, 1)) { + if (lua_isfunction(L, -1)) { tolua_pushusertype(L, u, TOLUA_CAST "unit"); if (lua_pcall(L, 1, 1, 0) != 0) { @@ -292,7 +292,7 @@ lua_changeresource(unit * u, const struct resource_type *rtype, int delta) lua_pushstring(L, fname); lua_rawget(L, LUA_GLOBALSINDEX); - if (lua_isfunction(L, 1)) { + if (lua_isfunction(L, -1)) { tolua_pushusertype(L, u, TOLUA_CAST "unit"); tolua_pushnumber(L, (lua_Number) delta); @@ -323,7 +323,7 @@ static int lua_getresource(unit * u, const struct resource_type *rtype) lua_pushstring(L, fname); lua_rawget(L, LUA_GLOBALSINDEX); - if (lua_isfunction(L, 1)) { + if (lua_isfunction(L, -1)) { tolua_pushusertype(L, u, TOLUA_CAST "unit"); if (lua_pcall(L, 1, 1, 0) != 0) { @@ -353,7 +353,7 @@ static boolean lua_canuse_item(const unit * u, const struct item_type *itype) lua_pushstring(L, fname); lua_rawget(L, LUA_GLOBALSINDEX); - if (lua_isfunction(L, 1)) { + if (lua_isfunction(L, -1)) { tolua_pushusertype(L, (void *)u, TOLUA_CAST "unit"); tolua_pushstring(L, itype->rtype->_name[0]); @@ -383,7 +383,7 @@ lua_wage(const region * r, const faction * f, const race * rc, int in_turn) lua_pushstring(L, fname); lua_rawget(L, LUA_GLOBALSINDEX); - if (lua_isfunction(L, 1)) { + if (lua_isfunction(L, -1)) { tolua_pushusertype(L, (void *)r, TOLUA_CAST "region"); tolua_pushusertype(L, (void *)f, TOLUA_CAST "faction"); tolua_pushstring(L, rc ? rc->_name[0] : 0); @@ -415,7 +415,7 @@ static void lua_agebuilding(building * b) lua_pushstring(L, fname); lua_rawget(L, LUA_GLOBALSINDEX); - if (lua_isfunction(L, 1)) { + if (lua_isfunction(L, -1)) { tolua_pushusertype(L, (void *)b, TOLUA_CAST "building"); if (lua_pcall(L, 1, 0, 0) != 0) { @@ -437,7 +437,7 @@ static int lua_building_protection(building * b, unit * u) lua_pushstring(L, fname); lua_rawget(L, LUA_GLOBALSINDEX); - if (lua_isfunction(L, 1)) { + if (lua_isfunction(L, -1)) { tolua_pushusertype(L, (void *)b, TOLUA_CAST "building"); tolua_pushusertype(L, (void *)u, TOLUA_CAST "unit"); @@ -464,7 +464,7 @@ static double lua_building_taxes(building * b, int level) lua_pushstring(L, fname); lua_rawget(L, LUA_GLOBALSINDEX); - if (lua_isfunction(L, 1)) { + if (lua_isfunction(L, -1)) { tolua_pushusertype(L, (void *)b, TOLUA_CAST "building"); tolua_pushnumber(L, level); @@ -491,7 +491,7 @@ static int lua_maintenance(const unit * u) lua_pushstring(L, fname); lua_rawget(L, LUA_GLOBALSINDEX); - if (lua_isfunction(L, 1)) { + if (lua_isfunction(L, -1)) { tolua_pushusertype(L, (void *)u, TOLUA_CAST "unit"); if (lua_pcall(L, 1, 1, 0) != 0) { @@ -521,7 +521,7 @@ static int lua_equipmentcallback(const struct equipment *eq, unit * u) lua_pushstring(L, fname); lua_rawget(L, LUA_GLOBALSINDEX); - if (lua_isfunction(L, 1)) { + if (lua_isfunction(L, -1)) { tolua_pushusertype(L, (void *)u, TOLUA_CAST "unit"); if (lua_pcall(L, 1, 1, 0) != 0) { @@ -553,7 +553,7 @@ lua_useitem(struct unit *u, const struct item_type *itype, int amount, lua_pushstring(L, fname); lua_rawget(L, LUA_GLOBALSINDEX); - if (lua_isfunction(L, 1)) { + if (lua_isfunction(L, -1)) { tolua_pushusertype(L, (void *)u, TOLUA_CAST "unit"); tolua_pushnumber(L, (lua_Number) amount); @@ -584,7 +584,7 @@ static int lua_recruit(struct unit *u, const struct archetype *arch, int amount) lua_pushstring(L, fname); lua_rawget(L, LUA_GLOBALSINDEX); - if (lua_isfunction(L, 1)) { + if (lua_isfunction(L, -1)) { tolua_pushusertype(L, (void *)u, TOLUA_CAST "unit"); tolua_pushnumber(L, (lua_Number) amount);