From a4187aa95155638524fb2b29649abe7ea4d43f71 Mon Sep 17 00:00:00 2001 From: Schmappie Eldress Date: Fri, 20 Aug 2021 01:14:35 -0500 Subject: [PATCH] lua client API: Fix crash in get_server_info() When running on a local machine in Docker, calling `minetest.get_server_info()` in lua can throw an exception, resulting in client shutdown. ERROR[Main]: ModError: Failed to load and run mod "tchat": ERROR[Main]: No address for peer found! ERROR[Main]: stack traceback: ERROR[Main]: [C]: in function 'get_server_info' ERROR[Main]: tchat:init.lua:118: in main chunk ERROR[Main]: Check debug.txt for details. --- src/script/lua_api/l_client.cpp | 37 ++++++++++++++++++++++++--------- 1 file changed, 27 insertions(+), 10 deletions(-) diff --git a/src/script/lua_api/l_client.cpp b/src/script/lua_api/l_client.cpp index 93bdac4b9..dfb64efa0 100644 --- a/src/script/lua_api/l_client.cpp +++ b/src/script/lua_api/l_client.cpp @@ -322,16 +322,33 @@ int ModApiClient::l_sound_fade(lua_State *L) int ModApiClient::l_get_server_info(lua_State *L) { Client *client = getClient(L); - Address serverAddress = client->getServerAddress(); - lua_newtable(L); - lua_pushstring(L, client->getAddressName().c_str()); - lua_setfield(L, -2, "address"); - lua_pushstring(L, serverAddress.serializeString().c_str()); - lua_setfield(L, -2, "ip"); - lua_pushinteger(L, serverAddress.getPort()); - lua_setfield(L, -2, "port"); - lua_pushinteger(L, client->getProtoVersion()); - lua_setfield(L, -2, "protocol_version"); + lua_newtable(L); + try { + Address serverAddress = client->getServerAddress(); + } catch (const con::PeerNotFoundException &) { + // Local connection? + lua_pushstring(L, "unknown"); + lua_setfield(L, -2, "address"); + lua_pushstring(L, "unknown"); + lua_setfield(L, -2, "ip"); + lua_pushinteger(L, 0); + lua_setfield(L, -2, "port"); + lua_pushinteger(L, 0); + lua_setfield(L, -2, "port"); + lua_pushinteger(L, 0); + lua_setfield(L, -2, "protocol_version"); + return 1; + } + + lua_pushstring(L, client->getAddressName().c_str()); + lua_setfield(L, -2, "address"); + lua_pushstring(L, serverAddress.serializeString().c_str()); + lua_setfield(L, -2, "ip"); + lua_pushinteger(L, serverAddress.getPort()); + lua_setfield(L, -2, "port"); + lua_pushinteger(L, client->getProtoVersion()); + lua_setfield(L, -2, "protocol_version"); + return 1; }