Skip to content

Commit 5f92d52

Browse files
committed
Merge branch 'mainmembers' into 'master'
Fix more potential use-after-free issues See merge request OpenMW/openmw!4566
2 parents c5a1ca7 + 9bf6a15 commit 5f92d52

15 files changed

+52
-43
lines changed

apps/openmw/mwlua/itemdata.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ namespace MWLua
7171
{
7272
SelfObject* obj = mObject.asSelfObject();
7373
addStatUpdateAction(context.mLuaManager, *obj);
74-
obj->mStatsCache[SelfObject::CachedStat{ &ItemData::setValue, std::monostate{}, prop }] = value;
74+
obj->mStatsCache[SelfObject::CachedStat{ &ItemData::setValue, std::monostate{}, prop }]
75+
= sol::main_object(value);
7576
}
7677
else
7778
throw std::runtime_error("Only global or self scripts can set the value");

apps/openmw/mwlua/localscripts.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ namespace MWLua
5454
{
5555
}
5656
MWBase::LuaManager::ActorControls mControls;
57-
std::map<CachedStat, sol::object> mStatsCache;
57+
std::map<CachedStat, sol::main_object> mStatsCache;
5858
bool mIsActive;
5959
};
6060

apps/openmw/mwlua/stats.cpp

+12-8
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,8 @@ namespace MWLua
123123

124124
SelfObject* obj = mObject.asSelfObject();
125125
addStatUpdateAction(context.mLuaManager, *obj);
126-
obj->mStatsCache[SelfObject::CachedStat{ &setNpcValue, attributeId, "skillIncreasesForAttribute" }] = value;
126+
obj->mStatsCache[SelfObject::CachedStat{ &setNpcValue, attributeId, "skillIncreasesForAttribute" }]
127+
= sol::main_object(value);
127128
}
128129
};
129130

@@ -159,7 +160,7 @@ namespace MWLua
159160
SelfObject* obj = mObject.asSelfObject();
160161
addStatUpdateAction(context.mLuaManager, *obj);
161162
obj->mStatsCache[SelfObject::CachedStat{ &setNpcValue, specialization, "skillIncreasesForSpecialization" }]
162-
= value;
163+
= sol::main_object(value);
163164
}
164165
};
165166

@@ -183,7 +184,8 @@ namespace MWLua
183184
{
184185
SelfObject* obj = mObject.asSelfObject();
185186
addStatUpdateAction(context.mLuaManager, *obj);
186-
obj->mStatsCache[SelfObject::CachedStat{ &setCreatureValue, std::monostate{}, "current" }] = value;
187+
obj->mStatsCache[SelfObject::CachedStat{ &setCreatureValue, std::monostate{}, "current" }]
188+
= sol::main_object(value);
187189
}
188190

189191
sol::object getProgress(const Context& context) const
@@ -204,7 +206,8 @@ namespace MWLua
204206

205207
SelfObject* obj = mObject.asSelfObject();
206208
addStatUpdateAction(context.mLuaManager, *obj);
207-
obj->mStatsCache[SelfObject::CachedStat{ &setNpcValue, std::monostate{}, "progress" }] = value;
209+
obj->mStatsCache[SelfObject::CachedStat{ &setNpcValue, std::monostate{}, "progress" }]
210+
= sol::main_object(value);
208211
}
209212

210213
SkillIncreasesForAttributeStats getSkillIncreasesForAttributeStats() const
@@ -258,7 +261,7 @@ namespace MWLua
258261
{
259262
SelfObject* obj = mObject.asSelfObject();
260263
addStatUpdateAction(context.mLuaManager, *obj);
261-
obj->mStatsCache[SelfObject::CachedStat{ &DynamicStat::setValue, mIndex, prop }] = value;
264+
obj->mStatsCache[SelfObject::CachedStat{ &DynamicStat::setValue, mIndex, prop }] = sol::main_object(value);
262265
}
263266

264267
static void setValue(Index i, std::string_view prop, const MWWorld::Ptr& ptr, const sol::object& value)
@@ -318,7 +321,7 @@ namespace MWLua
318321
{
319322
SelfObject* obj = mObject.asSelfObject();
320323
addStatUpdateAction(context.mLuaManager, *obj);
321-
obj->mStatsCache[SelfObject::CachedStat{ &AttributeStat::setValue, mId, prop }] = value;
324+
obj->mStatsCache[SelfObject::CachedStat{ &AttributeStat::setValue, mId, prop }] = sol::main_object(value);
322325
}
323326

324327
static void setValue(Index i, std::string_view prop, const MWWorld::Ptr& ptr, const sol::object& value)
@@ -402,7 +405,7 @@ namespace MWLua
402405
{
403406
SelfObject* obj = mObject.asSelfObject();
404407
addStatUpdateAction(context.mLuaManager, *obj);
405-
obj->mStatsCache[SelfObject::CachedStat{ &SkillStat::setValue, mId, prop }] = value;
408+
obj->mStatsCache[SelfObject::CachedStat{ &SkillStat::setValue, mId, prop }] = sol::main_object(value);
406409
}
407410

408411
static void setValue(Index index, std::string_view prop, const MWWorld::Ptr& ptr, const sol::object& value)
@@ -465,7 +468,8 @@ namespace MWLua
465468
{
466469
SelfObject* obj = mObject.asSelfObject();
467470
addStatUpdateAction(context.mLuaManager, *obj);
468-
obj->mStatsCache[SelfObject::CachedStat{ &AIStat::setValue, static_cast<int>(mIndex), prop }] = value;
471+
obj->mStatsCache[SelfObject::CachedStat{ &AIStat::setValue, static_cast<int>(mIndex), prop }]
472+
= sol::main_object(value);
469473
}
470474

471475
static void setValue(Index i, std::string_view prop, const MWWorld::Ptr& ptr, const sol::object& value)

apps/openmw/mwlua/uibindings.cpp

+8-5
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,8 @@ namespace MWLua
168168
if (index == LuaUi::Layer::count())
169169
throw std::logic_error(std::string("Layer not found"));
170170
index++;
171-
context.mLuaManager->addAction([=]() { LuaUi::Layer::insert(index, name, options); }, "Insert UI layer");
171+
context.mLuaManager->addAction(
172+
[=, name = std::string(name)]() { LuaUi::Layer::insert(index, name, options); }, "Insert UI layer");
172173
};
173174
layersTable["insertBefore"] = [context](
174175
std::string_view beforename, std::string_view name, const sol::object& opt) {
@@ -177,7 +178,8 @@ namespace MWLua
177178
size_t index = LuaUi::Layer::indexOf(beforename);
178179
if (index == LuaUi::Layer::count())
179180
throw std::logic_error(std::string("Layer not found"));
180-
context.mLuaManager->addAction([=]() { LuaUi::Layer::insert(index, name, options); }, "Insert UI layer");
181+
context.mLuaManager->addAction(
182+
[=, name = std::string(name)]() { LuaUi::Layer::insert(index, name, options); }, "Insert UI layer");
181183
};
182184
sol::table layers = LuaUtil::makeReadOnly(layersTable);
183185
sol::table layersMeta = layers[sol::metatable_key];
@@ -285,8 +287,9 @@ namespace MWLua
285287
return res;
286288
};
287289
api["_setWindowDisabled"]
288-
= [windowManager, luaManager = context.mLuaManager](std::string_view window, bool disabled) {
289-
luaManager->addAction([=]() { windowManager->setDisabledByLua(window, disabled); });
290+
= [windowManager, luaManager = context.mLuaManager](std::string window, bool disabled) {
291+
luaManager->addAction(
292+
[=, window = std::move(window)]() { windowManager->setDisabledByLua(window, disabled); });
290293
};
291294

292295
// TODO
@@ -308,7 +311,7 @@ namespace MWLua
308311
return res.str();
309312
};
310313
element["layout"] = sol::property([](const LuaUi::Element& element) { return element.mLayout; },
311-
[](LuaUi::Element& element, const sol::table& layout) { element.mLayout = layout; });
314+
[](LuaUi::Element& element, const sol::main_table& layout) { element.mLayout = layout; });
312315
element["update"] = [luaManager = context.mLuaManager](const std::shared_ptr<LuaUi::Element>& element) {
313316
if (element->mState != LuaUi::Element::Created)
314317
return;

components/lua/luastate.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,8 @@ namespace LuaUtil
343343
}
344344

345345
sol::protected_function_result LuaState::runInNewSandbox(const VFS::Path::Normalized& path,
346-
const std::string& envName, const std::map<std::string, sol::object>& packages, const sol::object& hiddenData)
346+
const std::string& envName, const std::map<std::string, sol::main_object>& packages,
347+
const sol::main_object& hiddenData)
347348
{
348349
// TODO
349350
sol::protected_function script = loadScriptAndCache(path);

components/lua/luastate.hpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,8 @@ namespace LuaUtil
148148
// should be either a sol::table or a sol::function. If it is a function, it will be evaluated
149149
// (once per sandbox) with the argument 'hiddenData' the first time when requested.
150150
sol::protected_function_result runInNewSandbox(const VFS::Path::Normalized& path,
151-
const std::string& envName = "unnamed", const std::map<std::string, sol::object>& packages = {},
152-
const sol::object& hiddenData = sol::nil);
151+
const std::string& envName = "unnamed", const std::map<std::string, sol::main_object>& packages = {},
152+
const sol::main_object& hiddenData = sol::nil);
153153

154154
void dropScriptCache() { mCompiledScripts.clear(); }
155155

components/lua/scriptscontainer.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ namespace LuaUtil
5252
Log(Debug::Error) << mNamePrefix << "[" << scriptPath(scriptId) << "] " << msg << ": " << e.what();
5353
}
5454

55-
void ScriptsContainer::addPackage(std::string packageName, sol::object package)
55+
void ScriptsContainer::addPackage(std::string packageName, sol::main_object package)
5656
{
5757
if (!package.is<sol::userdata>())
5858
throw std::logic_error("Expected package to be read-only: " + packageName);
@@ -312,7 +312,7 @@ namespace LuaUtil
312312
{
313313
if (next->mOnOverride)
314314
{
315-
sol::object prevInterface = sol::nil;
315+
sol::main_object prevInterface = sol::nil;
316316
if (prev)
317317
prevInterface = *prev->mInterface;
318318
try

components/lua/scriptscontainer.hpp

+8-8
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ namespace LuaUtil
9090

9191
// Adds package that will be available (via `require`) for all scripts in the container.
9292
// Automatically applies LuaUtil::makeReadOnly to the package.
93-
void addPackage(std::string packageName, sol::object package);
93+
void addPackage(std::string packageName, sol::main_object package);
9494

9595
// Gets script with given id from ScriptsConfiguration, finds the source in the virtual file system, starts as a
9696
// new script, adds it to the container, and calls onInit for this script. Returns `true` if the script was
@@ -168,7 +168,7 @@ namespace LuaUtil
168168
struct Handler
169169
{
170170
int mScriptId;
171-
sol::function mFn;
171+
sol::main_function mFn;
172172
};
173173

174174
struct EngineHandlerList
@@ -212,11 +212,11 @@ namespace LuaUtil
212212
private:
213213
struct Script
214214
{
215-
std::optional<sol::function> mOnSave;
216-
std::optional<sol::function> mOnOverride;
217-
std::optional<sol::table> mInterface;
215+
std::optional<sol::main_function> mOnSave;
216+
std::optional<sol::main_function> mOnOverride;
217+
std::optional<sol::main_table> mInterface;
218218
std::string mInterfaceName;
219-
sol::table mHiddenData;
219+
sol::main_table mHiddenData;
220220
std::map<std::string, sol::main_protected_function> mRegisteredCallbacks;
221221
std::map<int64_t, sol::main_protected_function> mTemporaryCallbacks;
222222
VFS::Path::Normalized mPath;
@@ -268,11 +268,11 @@ namespace LuaUtil
268268
const UserdataSerializer* mSerializer = nullptr;
269269
const UserdataSerializer* mSavedDataDeserializer = nullptr;
270270

271-
std::map<std::string, sol::object> mAPI;
271+
std::map<std::string, sol::main_object> mAPI;
272272
struct LoadedData
273273
{
274274
std::map<int, Script> mScripts;
275-
sol::table mPublicInterfaces;
275+
sol::main_table mPublicInterfaces;
276276

277277
std::map<std::string, EventHandlerList, std::less<>> mEventHandlers;
278278

components/lua/storage.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ namespace LuaUtil
3737
sol::object LuaStorage::Value::getReadOnly(lua_State* L) const
3838
{
3939
if (mReadOnlyValue == sol::nil && !mSerializedValue.empty())
40-
mReadOnlyValue = deserialize(L, mSerializedValue, nullptr, true);
40+
mReadOnlyValue = sol::main_object(deserialize(L, mSerializedValue, nullptr, true));
4141
return mReadOnlyValue;
4242
}
4343

components/lua/storage.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ namespace LuaUtil
7474

7575
private:
7676
std::string mSerializedValue;
77-
mutable sol::object mReadOnlyValue = sol::nil;
77+
mutable sol::main_object mReadOnlyValue = sol::nil;
7878
};
7979

8080
struct Section

components/lua_ui/content.hpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ namespace LuaUi
1818
{
1919
public:
2020
// accepts only Lua tables returned by ui.content
21-
explicit ContentView(sol::table table)
21+
explicit ContentView(sol::main_table table)
2222
: mTable(std::move(table))
2323
{
2424
if (!isValidContent(mTable))
@@ -94,7 +94,7 @@ namespace LuaUi
9494
sol::table getMetatable() const { return mTable[sol::metatable_key].get<sol::table>(); }
9595

9696
private:
97-
sol::table mTable;
97+
sol::main_table mTable;
9898

9999
template <typename... Arg>
100100
sol::object callMethod(std::string_view name, Arg&&... arg) const

components/lua_ui/element.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ namespace LuaUi
324324
destroyRoot(mRoot);
325325
mRoot = nullptr;
326326
}
327-
mLayout = sol::make_object(mLayout.lua_state(), sol::nil);
327+
mLayout.reset();
328328
}
329329
mState = Destroyed;
330330
}

components/lua_ui/element.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ namespace LuaUi
1919
}
2020

2121
WidgetExtension* mRoot;
22-
sol::object mLayout;
22+
sol::main_object mLayout;
2323
std::string mLayer;
2424

2525
enum State

components/lua_ui/widget.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ namespace LuaUi
281281
updateChildrenCoord();
282282
}
283283

284-
void WidgetExtension::setProperties(const sol::object& props)
284+
void WidgetExtension::setProperties(const sol::main_object& props)
285285
{
286286
mProperties = props;
287287
updateProperties();

components/lua_ui/widget.hpp

+8-8
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,10 @@ namespace LuaUi
5050
void setCallback(const std::string&, const LuaUtil::Callback&);
5151
void clearCallbacks();
5252

53-
void setProperties(const sol::object& props);
54-
void setTemplateProperties(const sol::object& props) { mTemplateProperties = props; }
53+
void setProperties(const sol::main_object& props);
54+
void setTemplateProperties(const sol::main_object& props) { mTemplateProperties = props; }
5555

56-
void setExternal(const sol::object& external) { mExternal = external; }
56+
void setExternal(const sol::main_object& external) { mExternal = external; }
5757

5858
MyGUI::IntCoord forcedCoord();
5959
void forceCoord(const MyGUI::IntCoord& offset);
@@ -63,7 +63,7 @@ namespace LuaUi
6363

6464
virtual void updateCoord();
6565

66-
const sol::table& getLayout() { return mLayout; }
66+
const sol::main_table& getLayout() { return mLayout; }
6767
void setLayout(const sol::table& layout) { mLayout = layout; }
6868

6969
template <typename T>
@@ -150,10 +150,10 @@ namespace LuaUi
150150
std::vector<WidgetExtension*> mTemplateChildren;
151151
WidgetExtension* mSlot;
152152
std::map<std::string, LuaUtil::Callback, std::less<>> mCallbacks;
153-
sol::table mLayout;
154-
sol::object mProperties;
155-
sol::object mTemplateProperties;
156-
sol::object mExternal;
153+
sol::main_table mLayout;
154+
sol::main_object mProperties;
155+
sol::main_object mTemplateProperties;
156+
sol::main_object mExternal;
157157
WidgetExtension* mParent;
158158
bool mTemplateChild;
159159
bool mElementRoot;

0 commit comments

Comments
 (0)