Verified Commit 73fc70d0 authored by Michal 'vorner' Vaner's avatar Michal 'vorner' Vaner
Browse files

Lua-convert: ensure large enough stack

Make sure the stack is large enough when pushing multiple values to the
lua stack. Not that it would crash now, but as the code is generic,
there might be problems, in theory.
parent 8b46a998
Pipeline #1681 passed with stage
in 59 seconds
......@@ -71,6 +71,9 @@ template<class T> typename std::enable_if<IsLuaPushable<T>::value>::type push(lu
template<class Container> typename std::enable_if<IsSequence<Container>::value>::type push(lua_State *state, const Container &container) {
lua_newtable(state);
size_t i = 1;
// This could recurse through the types and the pushes. Make sure we have space
// to push to.
lua_checkstack(state, 4);
for (const auto &v: container) {
lua_pushinteger(state, i ++);
push(state, v);
......@@ -79,6 +82,9 @@ template<class Container> typename std::enable_if<IsSequence<Container>::value>:
}
template<class Container> typename std::enable_if<IsMap<Container>::value>::type push(lua_State *state, const Container &container) {
lua_newtable(state);
// This could recurse through the types and the pushes. Make sure we have space
// to push to.
lua_checkstack(state, 4);
for (const auto &p: container) {
push(state, p.first);
push(state, p.second);
......@@ -87,6 +93,9 @@ template<class Container> typename std::enable_if<IsMap<Container>::value>::type
}
template<class Container> typename std::enable_if<IsSet<Container>::value>::type push(lua_State *state, const Container &container) {
lua_newtable(state);
// This could recurse through the types and the pushes. Make sure we have space
// to push to.
lua_checkstack(state, 4);
for (const auto &k: container) {
push(state, k);
lua_pushboolean(state, true);
......@@ -95,6 +104,10 @@ template<class Container> typename std::enable_if<IsSet<Container>::value>::type
}
// Loop through all the parameters, as a helper for variadic templates
template<class Head, class ...Tail> void push(lua_State *state, const Head &head, const Tail &...tail) {
/*
* +1 for head, +2 as a reserve when manipulating with data.
*/
lua_checkstack(state, sizeof...(Tail) + 1 + 2);
push(state, head);
push(state, tail...);
}
......@@ -246,6 +259,12 @@ private:
}
public:
static int push(lua_State *state, const std::tuple<T...> &values) {
/*
* Make sure the stack is large enough for the whole tuple
* Add a bit more, just to make sure we don't use something more
* dunig the pushing.
*/
lua_checkstack(state, sizeof...(T) + 2);
pushOne<0>(state, values);
return sizeof...(T);
}
......
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