From 21ac600c167dd04d026a919a14eb9415dfee152f Mon Sep 17 00:00:00 2001 From: Celtic Minstrel Date: Wed, 31 Jul 2024 09:17:26 -0400 Subject: [PATCH] Fix unit tostring conversion causing a crash if the unit is invalid --- src/scripting/lua_unit.cpp | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/src/scripting/lua_unit.cpp b/src/scripting/lua_unit.cpp index de4a4a3a11f..963d1fc62a1 100644 --- a/src/scripting/lua_unit.cpp +++ b/src/scripting/lua_unit.cpp @@ -242,19 +242,26 @@ static int impl_unit_equality(lua_State* L) static int impl_unit_tostring(lua_State* L) { const lua_unit* lu = luaW_tounit_ref(L, 1); - unit &u = *lu->get(); + unit* u = lu->get(); std::ostringstream str; str << "unit: <"; - if(!u.id().empty()) { - str << u.id() << " "; + if(!u) { + str << "invalid"; + } else if(!u->id().empty()) { + str << u->id() << " "; } else { - str << u.type_id() << " "; + str << u->type_id() << " "; } - if(int side = lu->on_recall_list()) { - str << "at (side " << side << " recall list)"; - } else { - str << "at (" << u.get_location() << ")"; + if(u) { + if(int side = lu->on_recall_list()) { + str << "at (side " << side << " recall list)"; + } else { + if(!lu->on_map()) { + str << "private "; + } + str << "at (" << u->get_location() << ")"; + } } str << '>';