From 965935a0e60c1b46371f2220583f93a7b02f0fda Mon Sep 17 00:00:00 2001 From: Lion Kortlepel Date: Wed, 2 Mar 2022 13:25:29 +0100 Subject: [PATCH] catch invalid json to JsonDeserialize, use lua_nil instead of nil --- src/TLuaEngine.cpp | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/TLuaEngine.cpp b/src/TLuaEngine.cpp index 5212e0e..69b3c11 100644 --- a/src/TLuaEngine.cpp +++ b/src/TLuaEngine.cpp @@ -531,7 +531,10 @@ static void JsonDeserializeRecursive(sol::state_view& StateView, sol::table& tab sol::table TLuaEngine::StateThreadData::Lua_JsonDeserialize(const std::string& str) { sol::state_view StateView(mState); auto table = StateView.create_table(); - // TODO: try / catch + if (!nlohmann::json::accept(str)) { + beammp_lua_error("string given to JsonDeserialize is not valid json."); + return sol::lua_nil; + } nlohmann::json json = nlohmann::json::parse(str); if (json.is_object()) { for (const auto& entry : json.items()) { @@ -543,7 +546,7 @@ sol::table TLuaEngine::StateThreadData::Lua_JsonDeserialize(const std::string& s } } else { beammp_lua_error("JsonDeserialize expected array or object json, instead got " + std::string(json.type_name())); - return sol::nil; + return sol::lua_nil; } return table; }