Fix unit tostring conversion causing a crash if the unit is invalid

This commit is contained in:
Celtic Minstrel 2024-07-31 09:17:26 -04:00 committed by Celtic Minstrel
parent c8a47c0bca
commit 21ac600c16

View File

@ -242,19 +242,26 @@ static int impl_unit_equality(lua_State* L)
static int impl_unit_tostring(lua_State* L) static int impl_unit_tostring(lua_State* L)
{ {
const lua_unit* lu = luaW_tounit_ref(L, 1); const lua_unit* lu = luaW_tounit_ref(L, 1);
unit &u = *lu->get(); unit* u = lu->get();
std::ostringstream str; std::ostringstream str;
str << "unit: <"; str << "unit: <";
if(!u.id().empty()) { if(!u) {
str << u.id() << " "; str << "invalid";
} else if(!u->id().empty()) {
str << u->id() << " ";
} else { } else {
str << u.type_id() << " "; str << u->type_id() << " ";
} }
if(int side = lu->on_recall_list()) { if(u) {
str << "at (side " << side << " recall list)"; if(int side = lu->on_recall_list()) {
} else { str << "at (side " << side << " recall list)";
str << "at (" << u.get_location() << ")"; } else {
if(!lu->on_map()) {
str << "private ";
}
str << "at (" << u->get_location() << ")";
}
} }
str << '>'; str << '>';