Skip to content

Commit 44922da

Browse files
authored
fix: Invalid string pointers passed by SetResult due to C++ memory lifecycle (#1032)
1 parent 1ca8ff2 commit 44922da

1 file changed

Lines changed: 22 additions & 2 deletions

File tree

src/scripting/script_engine.h

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,10 +199,30 @@ class ScriptContext
199199
*reinterpret_cast<uint64_t*>(&functionData[0]) = 0;
200200
}
201201

202-
*reinterpret_cast<T*>(&functionData[0]) = value;
203-
204202
m_numResults = 1;
205203
m_numArguments = 0;
204+
205+
if constexpr (std::is_same_v<T, const char*> || std::is_same_v<T, std::string>)
206+
{
207+
if constexpr (std::is_same_v<T, const char*>)
208+
{
209+
if (!value)
210+
{
211+
*reinterpret_cast<const char**>(&functionData[0]) = nullptr;
212+
return;
213+
}
214+
}
215+
216+
static thread_local std::string lastResult;
217+
lastResult = value;
218+
const char* persistentPtr = lastResult.c_str();
219+
220+
*reinterpret_cast<const char**>(&functionData[0]) = persistentPtr;
221+
}
222+
else
223+
{
224+
*reinterpret_cast<T*>(&functionData[0]) = value;
225+
}
206226
}
207227

208228
template <typename T> inline T GetResult()

0 commit comments

Comments
 (0)