Skip to content

Commit 2b2333f

Browse files
authored
Ensure sufficient stack size for Lua values (#4482)
* Ensure sufficient stack size * Fix Warning * Fix formatting * Rename variable and add docs
1 parent 4a819d7 commit 2b2333f

File tree

5 files changed

+16
-7
lines changed

5 files changed

+16
-7
lines changed

code/scripting/lua/LuaFunction.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,12 +172,15 @@ LuaValueList LuaFunction::call(lua_State* L, const LuaValueList& args) const {
172172
stackTop = lua_gettop(L);
173173
}
174174

175+
if(!lua_checkstack(L, (int)args.size() + 1))
176+
throw LuaException("Lua Stack Overflow!");
177+
175178
// Push the function onto the stack
176-
this->pushValue(L);
179+
this->pushValue(L, true);
177180

178181
// Push the arguments onto the stack
179182
for (const auto& arg : args) {
180-
arg.pushValue(L);
183+
arg.pushValue(L, true);
181184
}
182185

183186
// actually call the function now!

code/scripting/lua/LuaReference.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,14 @@ bool UniqueLuaReference::isValid() const {
8080
return true;
8181
}
8282

83-
void UniqueLuaReference::pushValue(lua_State* thread) const
83+
void UniqueLuaReference::pushValue(lua_State* thread, bool manualStackAllocation) const
8484
{
8585
Assertion(thread != nullptr, "Valid thread state must be specified!");
8686

87+
if (!manualStackAllocation) {
88+
lua_checkstack(thread, 1);
89+
}
90+
8791
if (this->isValid()) {
8892
lua_rawgeti(thread, LUA_REGISTRYINDEX, this->getReference());
8993
}

code/scripting/lua/LuaReference.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,9 @@ class UniqueLuaReference {
100100
/**
101101
* @brief Pushes the referenced value onto the stack.
102102
* @param thread A specific thread state to push the value to. nullptr for the default state of this reference
103+
* @param manualStackAllocation Set to true if you manually allocate sufficient stack size before calling this function. Keep false unless you know what you are doing.
103104
*/
104-
void pushValue(lua_State* thread) const;
105+
void pushValue(lua_State* thread, bool manualStackAllocation = false) const;
105106
};
106107
}
107108

code/scripting/lua/LuaValue.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,10 @@ bool LuaValue::isValid() const {
8888
return _reference && _reference->isValid();
8989
}
9090

91-
bool LuaValue::pushValue(lua_State* thread) const
91+
bool LuaValue::pushValue(lua_State* thread, bool manualStackAllocation) const
9292
{
9393
if (this->_reference->isValid()) {
94-
this->_reference->pushValue(thread);
94+
this->_reference->pushValue(thread, manualStackAllocation);
9595
return true;
9696
} else {
9797
return false;

code/scripting/lua/LuaValue.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,8 +185,9 @@ class LuaValue {
185185
* @brief Pushes this lua value onto the stack.
186186
* @param thread The thread stack onto which this value should be pushed. May be nullptr for the default state of
187187
* this value
188+
* @param manualStackAllocation Set to true if you manually allocate sufficient stack size before calling this function. Keep false unless you know what you are doing.
188189
*/
189-
bool pushValue(lua_State* thread) const;
190+
bool pushValue(lua_State* thread, bool manualStackAllocation = false) const;
190191

191192
lua_State* getLuaState() const;
192193

0 commit comments

Comments
 (0)