From fc4f69ed4360c247f70646c44a94e2041a59468f Mon Sep 17 00:00:00 2001 From: Marcus Tillmanns Date: Mon, 6 Jan 2025 11:55:00 +0100 Subject: [PATCH] Lua: Fix Crash when debug category is enabled Not all keys / values have to be strings, but we tried to always convert to string which results in an assert. Fixes: QTCREATORBUG-32206 Change-Id: Ibad83383e22d2bfb68a2801c3139d7c447a8b76a Reviewed-by: David Schulz --- src/plugins/lua/luaengine.cpp | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/src/plugins/lua/luaengine.cpp b/src/plugins/lua/luaengine.cpp index dca1bb25733..a241ed428fe 100644 --- a/src/plugins/lua/luaengine.cpp +++ b/src/plugins/lua/luaengine.cpp @@ -30,6 +30,32 @@ Utils::expected_str connectHooks( static Q_LOGGING_CATEGORY(logLuaEngine, "qtc.lua.engine", QtWarningMsg); +QString luaToString(lua_State *state, int index) +{ + size_t l; + const char *s = luaL_tolstring(state, index, &l); + if (s == nullptr) + return {}; + + // Remove from stack what tolstring pushed onto it. + sol::stack::pop_n(state, 1); + + return QString::fromUtf8(s, l); +} + +template +QString refToString(const T &ref) +{ + if (ref.template is()) + return ref.template as(); + + if (ref.template is()) + return toJsonString(ref.template as()); + + auto pp = sol::stack::push_pop(ref); + return luaToString(ref.lua_state(), -1); +} + class LuaInterfaceImpl final : public QObject, public LuaInterface { public: @@ -315,8 +341,8 @@ expected_str prepareSetup( if (logLuaEngine().isDebugEnabled()) { qCDebug(logLuaEngine) << "Script returned table with keys:"; for (const auto &[key, value] : *pluginTable) { - qCDebug(logLuaEngine) << "Key:" << key.as(); - qCDebug(logLuaEngine) << "Value:" << value.as(); + qCDebug(logLuaEngine) << "Key:" << refToString(key); + qCDebug(logLuaEngine) << "Value:" << refToString(value); } }