mirror of
https://github.com/OpenTTD/OpenTTD
synced 2025-12-20 03:32:39 +01:00
Codechange: Range-check rail/road/tram maps by size instead of constant. (#14602)
* Compare against the size of the array instead of the constant used to define the array. * `type_map` alias now uses auto to avoid defining the type each time. * `_cur_gps.grffile->railtype_map` is now aliased to `type_map` to simplify and standardise.
This commit is contained in:
@@ -29,14 +29,15 @@ static ChangeInfoResult RailTypeChangeInfo(uint first, uint last, int prop, Byte
|
|||||||
ChangeInfoResult ret = CIR_SUCCESS;
|
ChangeInfoResult ret = CIR_SUCCESS;
|
||||||
|
|
||||||
extern RailTypeInfo _railtypes[RAILTYPE_END];
|
extern RailTypeInfo _railtypes[RAILTYPE_END];
|
||||||
|
const auto &type_map = _cur_gps.grffile->railtype_map;
|
||||||
|
|
||||||
if (last > RAILTYPE_END) {
|
if (last > std::size(type_map)) {
|
||||||
GrfMsg(1, "RailTypeChangeInfo: Rail type {} is invalid, max {}, ignoring", last, RAILTYPE_END);
|
GrfMsg(1, "RailTypeChangeInfo: Rail type {} is invalid, max {}, ignoring", last, std::size(type_map));
|
||||||
return CIR_INVALID_ID;
|
return CIR_INVALID_ID;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (uint id = first; id < last; ++id) {
|
for (uint id = first; id < last; ++id) {
|
||||||
RailType rt = _cur_gps.grffile->railtype_map[id];
|
RailType rt = type_map[id];
|
||||||
if (rt == INVALID_RAILTYPE) return CIR_INVALID_ID;
|
if (rt == INVALID_RAILTYPE) return CIR_INVALID_ID;
|
||||||
|
|
||||||
RailTypeInfo *rti = &_railtypes[rt];
|
RailTypeInfo *rti = &_railtypes[rt];
|
||||||
@@ -163,9 +164,10 @@ static ChangeInfoResult RailTypeReserveInfo(uint first, uint last, int prop, Byt
|
|||||||
ChangeInfoResult ret = CIR_SUCCESS;
|
ChangeInfoResult ret = CIR_SUCCESS;
|
||||||
|
|
||||||
extern RailTypeInfo _railtypes[RAILTYPE_END];
|
extern RailTypeInfo _railtypes[RAILTYPE_END];
|
||||||
|
auto &type_map = _cur_gps.grffile->railtype_map;
|
||||||
|
|
||||||
if (last > RAILTYPE_END) {
|
if (last > std::size(type_map)) {
|
||||||
GrfMsg(1, "RailTypeReserveInfo: Rail type {} is invalid, max {}, ignoring", last, RAILTYPE_END);
|
GrfMsg(1, "RailTypeReserveInfo: Rail type {} is invalid, max {}, ignoring", last, std::size(type_map));
|
||||||
return CIR_INVALID_ID;
|
return CIR_INVALID_ID;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -182,7 +184,7 @@ static ChangeInfoResult RailTypeReserveInfo(uint first, uint last, int prop, Byt
|
|||||||
rt = AllocateRailType(rtl);
|
rt = AllocateRailType(rtl);
|
||||||
}
|
}
|
||||||
|
|
||||||
_cur_gps.grffile->railtype_map[id] = rt;
|
type_map[id] = rt;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -199,10 +201,10 @@ static ChangeInfoResult RailTypeReserveInfo(uint first, uint last, int prop, Byt
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
case 0x1D: // Alternate rail type label list
|
case 0x1D: // Alternate rail type label list
|
||||||
if (_cur_gps.grffile->railtype_map[id] != INVALID_RAILTYPE) {
|
if (type_map[id] != INVALID_RAILTYPE) {
|
||||||
int n = buf.ReadByte();
|
int n = buf.ReadByte();
|
||||||
for (int j = 0; j != n; j++) {
|
for (int j = 0; j != n; j++) {
|
||||||
_railtypes[_cur_gps.grffile->railtype_map[id]].alternate_labels.push_back(std::byteswap(buf.ReadDWord()));
|
_railtypes[type_map[id]].alternate_labels.push_back(std::byteswap(buf.ReadDWord()));
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -30,10 +30,10 @@ static ChangeInfoResult RoadTypeChangeInfo(uint first, uint last, int prop, Byte
|
|||||||
ChangeInfoResult ret = CIR_SUCCESS;
|
ChangeInfoResult ret = CIR_SUCCESS;
|
||||||
|
|
||||||
extern RoadTypeInfo _roadtypes[ROADTYPE_END];
|
extern RoadTypeInfo _roadtypes[ROADTYPE_END];
|
||||||
std::array<RoadType, ROADTYPE_END> &type_map = (rtt == RTT_TRAM) ? _cur_gps.grffile->tramtype_map : _cur_gps.grffile->roadtype_map;
|
const auto &type_map = (rtt == RTT_TRAM) ? _cur_gps.grffile->tramtype_map : _cur_gps.grffile->roadtype_map;
|
||||||
|
|
||||||
if (last > ROADTYPE_END) {
|
if (last > std::size(type_map)) {
|
||||||
GrfMsg(1, "RoadTypeChangeInfo: Road type {} is invalid, max {}, ignoring", last, ROADTYPE_END);
|
GrfMsg(1, "RoadTypeChangeInfo: Road type {} is invalid, max {}, ignoring", last, std::size(type_map));
|
||||||
return CIR_INVALID_ID;
|
return CIR_INVALID_ID;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -151,10 +151,10 @@ static ChangeInfoResult RoadTypeReserveInfo(uint first, uint last, int prop, Byt
|
|||||||
ChangeInfoResult ret = CIR_SUCCESS;
|
ChangeInfoResult ret = CIR_SUCCESS;
|
||||||
|
|
||||||
extern RoadTypeInfo _roadtypes[ROADTYPE_END];
|
extern RoadTypeInfo _roadtypes[ROADTYPE_END];
|
||||||
std::array<RoadType, ROADTYPE_END> &type_map = (rtt == RTT_TRAM) ? _cur_gps.grffile->tramtype_map : _cur_gps.grffile->roadtype_map;
|
auto &type_map = (rtt == RTT_TRAM) ? _cur_gps.grffile->tramtype_map : _cur_gps.grffile->roadtype_map;
|
||||||
|
|
||||||
if (last > ROADTYPE_END) {
|
if (last > std::size(type_map)) {
|
||||||
GrfMsg(1, "RoadTypeReserveInfo: Road type {} is invalid, max {}, ignoring", last, ROADTYPE_END);
|
GrfMsg(1, "RoadTypeReserveInfo: Road type {} is invalid, max {}, ignoring", last, std::size(type_map));
|
||||||
return CIR_INVALID_ID;
|
return CIR_INVALID_ID;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -268,7 +268,8 @@ struct RailTypeMapSpriteGroupHandler : MapSpriteGroupHandler {
|
|||||||
{
|
{
|
||||||
if (cid >= RTSG_END) return;
|
if (cid >= RTSG_END) return;
|
||||||
|
|
||||||
RailType railtype = local_id < RAILTYPE_END ? _cur_gps.grffile->railtype_map[local_id] : INVALID_RAILTYPE;
|
const auto &type_map = _cur_gps.grffile->railtype_map;
|
||||||
|
RailType railtype = local_id < std::size(type_map) ? type_map[local_id] : INVALID_RAILTYPE;
|
||||||
if (railtype == INVALID_RAILTYPE) return;
|
if (railtype == INVALID_RAILTYPE) return;
|
||||||
|
|
||||||
extern RailTypeInfo _railtypes[RAILTYPE_END];
|
extern RailTypeInfo _railtypes[RAILTYPE_END];
|
||||||
@@ -286,8 +287,8 @@ struct RoadTypeMapSpriteGroupHandler : MapSpriteGroupHandler {
|
|||||||
{
|
{
|
||||||
if (cid >= ROTSG_END) return;
|
if (cid >= ROTSG_END) return;
|
||||||
|
|
||||||
std::array<RoadType, ROADTYPE_END> &type_map = (TRoadTramType == RTT_TRAM) ? _cur_gps.grffile->tramtype_map : _cur_gps.grffile->roadtype_map;
|
const auto &type_map = (TRoadTramType == RTT_TRAM) ? _cur_gps.grffile->tramtype_map : _cur_gps.grffile->roadtype_map;
|
||||||
RoadType roadtype = local_id < ROADTYPE_END ? type_map[local_id] : INVALID_ROADTYPE;
|
RoadType roadtype = local_id < std::size(type_map) ? type_map[local_id] : INVALID_ROADTYPE;
|
||||||
if (roadtype == INVALID_ROADTYPE) return;
|
if (roadtype == INVALID_ROADTYPE) return;
|
||||||
|
|
||||||
extern RoadTypeInfo _roadtypes[ROADTYPE_END];
|
extern RoadTypeInfo _roadtypes[ROADTYPE_END];
|
||||||
|
|||||||
Reference in New Issue
Block a user