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)
{
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(u) {
if(int side = lu->on_recall_list()) {
str << "at (side " << side << " recall list)";
} else {
str << "at (" << u.get_location() << ")";
if(!lu->on_map()) {
str << "private ";
}
str << "at (" << u->get_location() << ")";
}
}
str << '>';