Skip to content

Commit

Permalink
Merge pull request #31 from SeisSol/fix/stack-overflow
Browse files Browse the repository at this point in the history
Fix stack overflow.
  • Loading branch information
krenzland committed Feb 23, 2022
2 parents 91199ba + 3b1486b commit a05f647
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/component/LuaMap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ double LuaMap::executeLuaFunction(Matrix<double> x,
std::abort();
}
}

// Save stack size
const auto top = lua_gettop(luaState);


// Push function and arguments to stack
lua_getglobal(luaState, "f"); // the function
Expand All @@ -68,7 +72,14 @@ double LuaMap::executeLuaFunction(Matrix<double> x,
<< std::endl;
std::abort();
}
return getField(luaState, idxToOutputName[funcIdx]);

const auto retVal = getField(luaState, idxToOutputName[funcIdx]);

// Reset stack size to value before function call
// This should avoid stack overflows
lua_settop(luaState, top);

return retVal;
}

Matrix<double> LuaMap::map(Matrix<double>& x) {
Expand Down

0 comments on commit a05f647

Please sign in to comment.