Skip to content

Commit b5ad2d0

Browse files
authored
Merge pull request cortex-command-community#220 from Architector4/patch-1
Fix use after free with script timing in LuaMan.cpp
2 parents 67b74c5 + d69a26d commit b5ad2d0

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

Source/Managers/LuaMan.cpp

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -614,8 +614,17 @@ int LuaStateWrapper::RunScriptFunctionObject(const LuabindObjectWrapper* functio
614614
functionObjectArgument->GetLuabindObject()->push(m_State);
615615
}
616616
}
617-
618617
const std::string& path = functionObject->GetFilePath();
618+
619+
// Function object may be deleted during the Lua call, making `path` above invalid.
620+
// Find and store the script timings entry now and write to it afterward.
621+
PerformanceMan::ScriptTiming* timing = nullptr;
622+
623+
// only track time in non-MT scripts, for now
624+
if (&g_LuaMan.GetMasterScriptState() == this) {
625+
timing = &m_ScriptTimings[path];
626+
}
627+
619628
std::chrono::steady_clock::time_point begin = std::chrono::steady_clock::now();
620629
{
621630
ZoneScoped;
@@ -631,10 +640,9 @@ int LuaStateWrapper::RunScriptFunctionObject(const LuabindObjectWrapper* functio
631640
}
632641
std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now();
633642

634-
// only track time in non-MT scripts, for now
635-
if (&g_LuaMan.GetMasterScriptState() == this) {
636-
m_ScriptTimings[path].m_Time += std::chrono::duration_cast<std::chrono::microseconds>(end - begin).count();
637-
m_ScriptTimings[path].m_CallCount++;
643+
if (timing) {
644+
timing->m_Time += std::chrono::duration_cast<std::chrono::microseconds>(end - begin).count();
645+
timing->m_CallCount++;
638646
}
639647

640648
lua_pop(m_State, 1);

0 commit comments

Comments
 (0)