1
0
mirror of https://github.com/OpenTTD/OpenTTD synced 2026-01-21 19:32:54 +01:00

Fix 3ac1a2f1e4: Game crash due to invalid vehicle type information. (#14628)

Use std::variant instead of union for vehicle info.

RailVehicleInfo is now non-POD so using in a union causes undefined behaviour.
This commit is contained in:
Peter Nelson
2025-09-24 22:44:41 +01:00
committed by GitHub
parent ecb761fc69
commit 42c9f84d74
26 changed files with 208 additions and 198 deletions

View File

@@ -43,12 +43,12 @@ StringID GetEngineCategoryName(EngineID engine)
switch (e->type) {
default: NOT_REACHED();
case VEH_ROAD:
return GetRoadTypeInfo(e->u.road.roadtype)->strings.new_engine;
return GetRoadTypeInfo(e->VehInfo<RoadVehicleInfo>().roadtype)->strings.new_engine;
case VEH_AIRCRAFT: return STR_ENGINE_PREVIEW_AIRCRAFT;
case VEH_SHIP: return STR_ENGINE_PREVIEW_SHIP;
case VEH_TRAIN:
assert(e->u.rail.railtypes.Any());
return GetRailTypeInfo(e->u.rail.railtypes.GetNthSetBit(0).value())->strings.new_loco;
assert(e->VehInfo<RailVehicleInfo>().railtypes.Any());
return GetRailTypeInfo(e->VehInfo<RailVehicleInfo>().railtypes.GetNthSetBit(0).value())->strings.new_loco;
}
}
@@ -182,12 +182,12 @@ static std::string GetTrainEngineInfoString(const Engine &e)
res << GetString(STR_ENGINE_PREVIEW_COST_WEIGHT, e.GetCost(), e.GetDisplayWeight());
res << '\n';
if (e.u.rail.railtypes.Count() > 1) {
if (e.VehInfo<RailVehicleInfo>().railtypes.Count() > 1) {
std::string railtypes{};
std::string_view list_separator = GetListSeparator();
for (const auto &rt : _sorted_railtypes) {
if (!e.u.rail.railtypes.Test(rt)) continue;
if (!e.VehInfo<RailVehicleInfo>().railtypes.Test(rt)) continue;
if (!railtypes.empty()) railtypes += list_separator;
AppendStringInPlace(railtypes, GetRailTypeInfo(rt)->strings.name);
@@ -197,7 +197,7 @@ static std::string GetTrainEngineInfoString(const Engine &e)
}
bool is_maglev = true;
for (RailType rt : e.u.rail.railtypes) {
for (RailType rt : e.VehInfo<RailVehicleInfo>().railtypes) {
is_maglev &= GetRailTypeInfo(rt)->acceleration_type == VehicleAccelerationModel::Maglev;
}