From 9c9309cdbb053598aaf08506928a4824e78b4622 Mon Sep 17 00:00:00 2001 From: SmallJoker Date: Fri, 7 Apr 2023 12:49:35 +0200 Subject: [PATCH] Lua API: Catch serialization error for chat messages (#13337) Prevents server errors caused by too long chat messages from the Lua API. --- src/script/lua_api/l_server.cpp | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/script/lua_api/l_server.cpp b/src/script/lua_api/l_server.cpp index eff212653..67916e074 100644 --- a/src/script/lua_api/l_server.cpp +++ b/src/script/lua_api/l_server.cpp @@ -85,7 +85,14 @@ int ModApiServer::l_chat_send_all(lua_State *L) // Get server from registry Server *server = getServer(L); // Send - server->notifyPlayers(utf8_to_wide(text)); + try { + server->notifyPlayers(utf8_to_wide(text)); + } catch (PacketError &e) { + warningstream << "Exception caught: " << e.what() << std::endl + << script_get_backtrace(L) << std::endl; + server->notifyPlayers(utf8_to_wide(std::string("Internal error: ") + e.what())); + } + return 0; } @@ -99,7 +106,13 @@ int ModApiServer::l_chat_send_player(lua_State *L) // Get server from registry Server *server = getServer(L); // Send - server->notifyPlayer(name, utf8_to_wide(text)); + try { + server->notifyPlayer(name, utf8_to_wide(text)); + } catch (PacketError &e) { + warningstream << "Exception caught: " << e.what() << std::endl + << script_get_backtrace(L) << std::endl; + server->notifyPlayer(name, utf8_to_wide(std::string("Internal error: ") + e.what())); + } return 0; }