Skip to content

Commit 26453df

Browse files
authored
Don't crash if a Lua error occurs inside get_staticdata
1 parent d71872a commit 26453df

4 files changed

Lines changed: 35 additions & 17 deletions

File tree

src/client/game.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4536,7 +4536,7 @@ void the_game(bool *kill,
45364536
error_message = e.what();
45374537
errorstream << "ServerError: " << error_message << std::endl;
45384538
} catch (ModError &e) {
4539-
// DO NOT TRANSLATE the `ModError`, it's used by ui.lua
4539+
// DO NOT TRANSLATE the `ModError`, it's used by `ui.lua`
45404540
error_message = std::string("ModError: ") + e.what() +
45414541
strgettext("\nCheck debug.txt for details.");
45424542
errorstream << error_message << std::endl;

src/server.cpp

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
7474
#include "database/database-dummy.h"
7575
#include "gameparams.h"
7676
#include "particles.h"
77+
#include "gettext.h"
7778

7879
class ClientNotFoundException : public BaseException
7980
{
@@ -235,7 +236,7 @@ Server::Server(
235236
Address bind_addr,
236237
bool dedicated,
237238
ChatInterface *iface,
238-
std::string *on_shutdown_errmsg
239+
std::string *shutdown_errmsg
239240
):
240241
m_bind_addr(bind_addr),
241242
m_path_world(path_world),
@@ -254,7 +255,7 @@ Server::Server(
254255
m_thread(new ServerThread(this)),
255256
m_clients(m_con),
256257
m_admin_chat(iface),
257-
m_on_shutdown_errmsg(on_shutdown_errmsg),
258+
m_shutdown_errmsg(shutdown_errmsg),
258259
m_modchannel_mgr(new ModChannelMgr())
259260
{
260261
if (m_path_world.empty())
@@ -353,14 +354,7 @@ Server::~Server()
353354
try {
354355
m_script->on_shutdown();
355356
} catch (ModError &e) {
356-
errorstream << "ModError: " << e.what() << std::endl;
357-
if (m_on_shutdown_errmsg) {
358-
if (m_on_shutdown_errmsg->empty()) {
359-
*m_on_shutdown_errmsg = std::string("ModError: ") + e.what();
360-
} else {
361-
*m_on_shutdown_errmsg += std::string("\nModError: ") + e.what();
362-
}
363-
}
357+
addShutdownError(e);
364358
}
365359

366360
infostream << "Server: Saving environment metadata" << std::endl;
@@ -3759,6 +3753,23 @@ std::string Server::getBuiltinLuaPath()
37593753
return porting::path_share + DIR_DELIM + "builtin";
37603754
}
37613755

3756+
// Not thread-safe.
3757+
void Server::addShutdownError(const ModError &e)
3758+
{
3759+
// DO NOT TRANSLATE the `ModError`, it's used by `ui.lua`
3760+
std::string msg = fmtgettext("%s while shutting down: ", "ModError") +
3761+
e.what() + strgettext("\nCheck debug.txt for details.");
3762+
errorstream << msg << std::endl;
3763+
3764+
if (m_shutdown_errmsg) {
3765+
if (m_shutdown_errmsg->empty()) {
3766+
*m_shutdown_errmsg = msg;
3767+
} else {
3768+
*m_shutdown_errmsg += "\n\n" + msg;
3769+
}
3770+
}
3771+
}
3772+
37623773
v3f Server::findSpawnPos()
37633774
{
37643775
ServerMap &map = m_env->getServerMap();

src/server.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ class Server : public con::PeerHandler, public MapEventReceiver,
150150
Address bind_addr,
151151
bool dedicated,
152152
ChatInterface *iface = nullptr,
153-
std::string *on_shutdown_errmsg = nullptr
153+
std::string *shutdown_errmsg = nullptr
154154
);
155155
~Server();
156156
DISABLE_CLASS_COPY(Server);
@@ -300,6 +300,9 @@ class Server : public con::PeerHandler, public MapEventReceiver,
300300
setAsyncFatalError(std::string("Lua: ") + e.what());
301301
}
302302

303+
// Not thread-safe.
304+
void addShutdownError(const ModError &e);
305+
303306
bool showFormspec(const char *name, const std::string &formspec, const std::string &formname);
304307
Map & getMap() { return m_env->getMap(); }
305308
ServerEnvironment & getEnv() { return *m_env; }
@@ -655,9 +658,9 @@ class Server : public con::PeerHandler, public MapEventReceiver,
655658
ChatInterface *m_admin_chat;
656659
std::string m_admin_nick;
657660

658-
// if a mod-error occurs in the on_shutdown callback, the error message will
659-
// be written into this
660-
std::string *const m_on_shutdown_errmsg;
661+
// If a mod error occurs while shutting down, the error message will be
662+
// written into this.
663+
std::string *const m_shutdown_errmsg;
661664

662665
/*
663666
Map edit event queue. Automatically receives all map edits.

src/serverenvironment.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -510,8 +510,12 @@ ServerEnvironment::~ServerEnvironment()
510510
// This makes the next one delete all active objects.
511511
m_active_blocks.clear();
512512

513-
// Convert all objects to static and delete the active objects
514-
deactivateFarObjects(true);
513+
try {
514+
// Convert all objects to static and delete the active objects
515+
deactivateFarObjects(true);
516+
} catch (ModError &e) {
517+
m_server->addShutdownError(e);
518+
}
515519

516520
// Drop/delete map
517521
if (m_map)

0 commit comments

Comments
 (0)