Skip to content

Commit

Permalink
Fix LuaRules::CheckStack top = -1 (after 2e9a5de)
Browse files Browse the repository at this point in the history
  • Loading branch information
zerver committed Dec 12, 2011
1 parent 8de7447 commit e021d20
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 64 deletions.
13 changes: 5 additions & 8 deletions rts/Lua/LuaHandle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1445,6 +1445,7 @@ bool CLuaHandle::Explosion(int weaponDefID, const float3& pos, const CUnit* owne

LUA_UNIT_BATCH_PUSH(false, UNIT_EXPLOSION, weaponDefID, pos, owner);
LUA_CALL_IN_CHECK(L);
int top = lua_gettop(L);
lua_checkstack(L, 7);
static const LuaHashString cmdStr("Explosion");
if (!cmdStr.GetGlobalFunc(L)) {
Expand All @@ -1463,11 +1464,8 @@ bool CLuaHandle::Explosion(int weaponDefID, const float3& pos, const CUnit* owne
RunCallIn(cmdStr, (owner == NULL) ? 4 : 5, 1);

// get the results
if (!lua_isboolean(L, -1)) {
LOG_L(L_WARNING, "%s() bad return value", cmdStr.GetString().c_str());
lua_pop(L, 1);
if (!CheckReturnBool(L, top, cmdStr.GetString().c_str()))
return false;
}

const bool retval = !!lua_toboolean(L, -1);
lua_pop(L, 1);
Expand Down Expand Up @@ -2534,6 +2532,7 @@ bool CLuaHandle::CommandNotify(const Command& cmd)
return false;
}
LUA_CALL_IN_CHECK(L);
int top = lua_gettop(L);
lua_checkstack(L, 5);
static const LuaHashString cmdStr("CommandNotify");
if (!PushUnsyncedCallIn(L, cmdStr)) {
Expand Down Expand Up @@ -2565,11 +2564,9 @@ bool CLuaHandle::CommandNotify(const Command& cmd)
}

// get the results
if (!lua_isboolean(L, -1)) {
LOG_L(L_WARNING, "CommandNotify() bad return value");
lua_pop(L, 1);
if (!CheckReturnBool(L, top, cmdStr.GetString().c_str()))
return false;
}


const bool retval = !!lua_toboolean(L, -1);
lua_pop(L, 1);
Expand Down
14 changes: 14 additions & 0 deletions rts/Lua/LuaHandle.h
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,8 @@ class CLuaHandle : public CEventClient

inline bool IsValid() const { return (L_Sim != NULL) && (L_Draw != NULL); }

inline bool CheckReturnBool(lua_State *L, int top, const char *func);

protected:

lua_State* L_Sim;
Expand Down Expand Up @@ -482,6 +484,18 @@ inline bool CLuaHandle::RunCallInUnsynced(const LuaHashString& hs, int inArgs, i
}


inline bool CLuaHandle::CheckReturnBool(lua_State *L, int top, const char *func) {
if (lua_gettop(L) <= top) {
LOG_L(L_WARNING, "%s() no return value", func);
return false;
}
if (!lua_isboolean(L, -1)) {
LOG_L(L_WARNING, "%s() bad return value", func);
lua_pop(L, 1);
return false;
}
return true;
}

/******************************************************************************/
/******************************************************************************/
Expand Down
87 changes: 31 additions & 56 deletions rts/Lua/LuaRules.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ bool CLuaRules::CommandFallback(const CUnit* unit, const Command& cmd)
}

LUA_CALL_IN_CHECK(L);
int top = lua_gettop(L);
lua_checkstack(L, 9);
static const LuaHashString cmdStr("CommandFallback");
if (!cmdStr.GetGlobalFunc(L)) {
Expand All @@ -266,11 +267,8 @@ bool CLuaRules::CommandFallback(const CUnit* unit, const Command& cmd)
}

// get the results
if (!lua_isboolean(L, -1)) {
LOG_L(L_WARNING, "%s() bad return value", cmdStr.GetString().c_str());
lua_pop(L, 1);
if (!CheckReturnBool(L, top, cmdStr.GetString().c_str()))
return true;
}

const bool retval = !!lua_toboolean(L, -1);
lua_pop(L, 1);
Expand All @@ -287,6 +285,7 @@ bool CLuaRules::AllowCommand(const CUnit* unit, const Command& cmd, bool fromSyn
}

LUA_CALL_IN_CHECK(L);
int top = lua_gettop(L);
lua_checkstack(L, 10);
static const LuaHashString cmdStr("AllowCommand");
if (!cmdStr.GetGlobalFunc(L)) {
Expand All @@ -303,11 +302,9 @@ bool CLuaRules::AllowCommand(const CUnit* unit, const Command& cmd, bool fromSyn
}

// get the results
if (!lua_isboolean(L, -1)) {
LOG_L(L_WARNING, "%s() bad return value", cmdStr.GetString().c_str());
lua_pop(L, 1);
if (!CheckReturnBool(L, top, cmdStr.GetString().c_str()))
return true;
}

const bool retval = !!lua_toboolean(L, -1);
lua_pop(L, 1);
return retval;
Expand All @@ -322,6 +319,7 @@ bool CLuaRules::AllowUnitCreation(const UnitDef* unitDef,
}

LUA_CALL_IN_CHECK(L);
int top = lua_gettop(L);
lua_checkstack(L, 8);
static const LuaHashString cmdStr("AllowUnitCreation");
if (!cmdStr.GetGlobalFunc(L)) {
Expand All @@ -343,11 +341,9 @@ bool CLuaRules::AllowUnitCreation(const UnitDef* unitDef,
}

// get the results
if (!lua_isboolean(L, -1)) {
LOG_L(L_WARNING, "%s() bad return value", cmdStr.GetString().c_str());
lua_pop(L, 1);
if (!CheckReturnBool(L, top, cmdStr.GetString().c_str()))
return true;
}

const bool retval = !!lua_toboolean(L, -1);
lua_pop(L, 1);
return retval;
Expand All @@ -362,6 +358,7 @@ bool CLuaRules::AllowUnitTransfer(const CUnit* unit, int newTeam, bool capture)
}

LUA_CALL_IN_CHECK(L);
int top = lua_gettop(L);
lua_checkstack(L, 7);
static const LuaHashString cmdStr("AllowUnitTransfer");
if (!cmdStr.GetGlobalFunc(L)) {
Expand All @@ -380,11 +377,8 @@ bool CLuaRules::AllowUnitTransfer(const CUnit* unit, int newTeam, bool capture)
}

// get the results
if (!lua_isboolean(L, -1)) {
LOG_L(L_WARNING, "%s() bad return value", cmdStr.GetString().c_str());
lua_pop(L, 1);
if (!CheckReturnBool(L, top, cmdStr.GetString().c_str()))
return true;
}

const bool retval = !!lua_toboolean(L, -1);
lua_pop(L, 1);
Expand All @@ -400,6 +394,7 @@ bool CLuaRules::AllowUnitBuildStep(const CUnit* builder,
}

LUA_CALL_IN_CHECK(L);
int top = lua_gettop(L);
lua_checkstack(L, 7);
static const LuaHashString cmdStr("AllowUnitBuildStep");
if (!cmdStr.GetGlobalFunc(L)) {
Expand All @@ -418,11 +413,8 @@ bool CLuaRules::AllowUnitBuildStep(const CUnit* builder,
}

// get the results
if (!lua_isboolean(L, -1)) {
LOG_L(L_WARNING, "%s() bad return value", cmdStr.GetString().c_str());
lua_pop(L, 1);
if (!CheckReturnBool(L, top, cmdStr.GetString().c_str()))
return true;
}

const bool retval = !!lua_toboolean(L, -1);
lua_pop(L, 1);
Expand All @@ -438,6 +430,7 @@ bool CLuaRules::AllowFeatureCreation(const FeatureDef* featureDef,
}

LUA_CALL_IN_CHECK(L);
int top = lua_gettop(L);
lua_checkstack(L, 7);
static const LuaHashString cmdStr("AllowFeatureCreation");
if (!cmdStr.GetGlobalFunc(L)) {
Expand All @@ -456,11 +449,8 @@ bool CLuaRules::AllowFeatureCreation(const FeatureDef* featureDef,
}

// get the results
if (!lua_isboolean(L, -1)) {
LOG_L(L_WARNING, "%s() bad return value", cmdStr.GetString().c_str());
lua_pop(L, 1);
if (!CheckReturnBool(L, top, cmdStr.GetString().c_str()))
return true;
}

const bool retval = !!lua_toboolean(L, -1);
lua_pop(L, 1);
Expand Down Expand Up @@ -513,6 +503,7 @@ bool CLuaRules::AllowResourceLevel(int teamID, const string& type, float level)
}

LUA_CALL_IN_CHECK(L);
int top = lua_gettop(L);
lua_checkstack(L, 5);
static const LuaHashString cmdStr("AllowResourceLevel");
if (!cmdStr.GetGlobalFunc(L)) {
Expand All @@ -529,11 +520,8 @@ bool CLuaRules::AllowResourceLevel(int teamID, const string& type, float level)
}

// get the results
if (!lua_isboolean(L, -1)) {
LOG_L(L_WARNING, "%s() bad return value", cmdStr.GetString().c_str());
lua_pop(L, 1);
if (!CheckReturnBool(L, top, cmdStr.GetString().c_str()))
return true;
}

const bool retval = !!lua_toboolean(L, -1);
lua_pop(L, 1);
Expand All @@ -549,6 +537,7 @@ bool CLuaRules::AllowResourceTransfer(int oldTeam, int newTeam,
}

LUA_CALL_IN_CHECK(L);
int top = lua_gettop(L);
lua_checkstack(L, 6);
static const LuaHashString cmdStr("AllowResourceTransfer");
if (!cmdStr.GetGlobalFunc(L)) {
Expand All @@ -566,11 +555,8 @@ bool CLuaRules::AllowResourceTransfer(int oldTeam, int newTeam,
}

// get the results
if (!lua_isboolean(L, -1)) {
LOG_L(L_WARNING, "%s() bad return value", cmdStr.GetString().c_str());
lua_pop(L, 1);
if (!CheckReturnBool(L, top, cmdStr.GetString().c_str()))
return true;
}

const bool retval = !!lua_toboolean(L, -1);
lua_pop(L, 1);
Expand All @@ -585,6 +571,7 @@ bool CLuaRules::AllowDirectUnitControl(int playerID, const CUnit* unit)
}

LUA_CALL_IN_CHECK(L);
int top = lua_gettop(L);
lua_checkstack(L, 6);
static const LuaHashString cmdStr("AllowDirectUnitControl");
if (!cmdStr.GetGlobalFunc(L)) {
Expand All @@ -602,11 +589,8 @@ bool CLuaRules::AllowDirectUnitControl(int playerID, const CUnit* unit)
}

// get the results
if (!lua_isboolean(L, -1)) {
LOG_L(L_WARNING, "%s() bad return value", cmdStr.GetString().c_str());
lua_pop(L, 1);
if (!CheckReturnBool(L, top, cmdStr.GetString().c_str()))
return true;
}

const bool retval = !!lua_toboolean(L, -1);
lua_pop(L, 1);
Expand All @@ -621,6 +605,7 @@ bool CLuaRules::AllowStartPosition(int playerID, const float3& pos)
}

LUA_CALL_IN_CHECK(L);
int top = lua_gettop(L);
lua_checkstack(L, 9);
static const LuaHashString cmdStr("AllowStartPosition");
if (!cmdStr.GetGlobalFunc(L)) {
Expand All @@ -639,11 +624,9 @@ bool CLuaRules::AllowStartPosition(int playerID, const float3& pos)
}

// get the results
if (!lua_isboolean(L, -1)) {
LOG_L(L_WARNING, "%s() bad return value", cmdStr.GetString().c_str());
lua_pop(L, 1);
if (!CheckReturnBool(L, top, cmdStr.GetString().c_str()))
return true;
}

const bool retval = !!lua_toboolean(L, -1);
lua_pop(L, 1);
return retval;
Expand All @@ -657,6 +640,7 @@ bool CLuaRules::MoveCtrlNotify(const CUnit* unit, int data)
}

LUA_CALL_IN_CHECK(L);
int top = lua_gettop(L);
lua_checkstack(L, 6);
static const LuaHashString cmdStr("MoveCtrlNotify");
if (!cmdStr.GetGlobalFunc(L)) {
Expand All @@ -675,11 +659,8 @@ bool CLuaRules::MoveCtrlNotify(const CUnit* unit, int data)
}

// get the results
if (!lua_isboolean(L, -1)) {
LOG_L(L_WARNING, "%s() bad return value", cmdStr.GetString().c_str());
lua_pop(L, 1);
if (!CheckReturnBool(L, top, cmdStr.GetString().c_str()))
return false;
}

const bool retval = !!lua_toboolean(L, -1);
lua_pop(L, 1);
Expand All @@ -695,6 +676,7 @@ bool CLuaRules::TerraformComplete(const CUnit* unit, const CUnit* build)
}

LUA_CALL_IN_CHECK(L);
int top = lua_gettop(L);
lua_checkstack(L, 8);
static const LuaHashString cmdStr("TerraformComplete");
if (!cmdStr.GetGlobalFunc(L)) {
Expand All @@ -717,11 +699,8 @@ bool CLuaRules::TerraformComplete(const CUnit* unit, const CUnit* build)
}

// get the results
if (!lua_isboolean(L, -1)) {
LOG_L(L_WARNING, "%s() bad return value", cmdStr.GetString().c_str());
lua_pop(L, 1);
if (!CheckReturnBool(L, top, cmdStr.GetString().c_str()))
return false;
}

const bool retval = !!lua_toboolean(L, -1);
lua_pop(L, 1);
Expand Down Expand Up @@ -930,6 +909,7 @@ bool CLuaRules::DrawUnit(int unitID)
}

LUA_CALL_IN_CHECK(L);
int top = lua_gettop(L);
lua_checkstack(L, 4);
static const LuaHashString cmdStr("DrawUnit");
if (!cmdStr.GetRegistryFunc(L)) {
Expand All @@ -950,11 +930,8 @@ bool CLuaRules::DrawUnit(int unitID)
return false;
}

if (!lua_isboolean(L, -1)) {
LOG_L(L_WARNING, "%s() bad return value", cmdStr.GetString().c_str());
lua_pop(L, 1);
if (!CheckReturnBool(L, top, cmdStr.GetString().c_str()))
return false;
}

const bool retval = !!lua_toboolean(L, -1);
lua_pop(L, 1);
Expand All @@ -968,6 +945,7 @@ bool CLuaRules::DrawFeature(int featureID)
}

LUA_CALL_IN_CHECK(L);
int top = lua_gettop(L);
lua_checkstack(L, 4);
static const LuaHashString cmdStr("DrawFeature");
if (!cmdStr.GetRegistryFunc(L)) {
Expand All @@ -988,11 +966,8 @@ bool CLuaRules::DrawFeature(int featureID)
return false;
}

if (!lua_isboolean(L, -1)) {
LOG_L(L_WARNING, "%s() bad return value", cmdStr.GetString().c_str());
lua_pop(L, 1);
if (!CheckReturnBool(L, top, cmdStr.GetString().c_str()))
return false;
}

const bool retval = !!lua_toboolean(L, -1);
lua_pop(L, 1);
Expand Down

2 comments on commit e021d20

@zerver
Copy link
Contributor Author

@zerver zerver commented on e021d20 Dec 12, 2011

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct, it just triggered a bug that was already there

@jk3064
Copy link
Contributor

@jk3064 jk3064 commented on e021d20 Dec 12, 2011

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

erm don't think so ...
Why do you think you need to pass the number of result arguments to RunCallIn()?
When the Lua code pass too less results the stack is filled with nils. So the stacktop is always valid & correct.

The error you try to fix is caused by either wrong arguments to RunCallIn() or too many/too less lua_pop(L,x).

Please sign in to comment.