diff --git a/aircraft_cmd.c b/aircraft_cmd.c --- a/aircraft_cmd.c +++ b/aircraft_cmd.c @@ -426,7 +426,7 @@ int32 CmdSellAircraft(TileIndex tile, ui { Vehicle *v; - if (!IsVehicleIndex(p1)) return CMD_ERROR; + if (!IsValidVehicleID(p1)) return CMD_ERROR; v = GetVehicle(p1); @@ -456,7 +456,7 @@ int32 CmdStartStopAircraft(TileIndex til Vehicle *v; uint16 callback; - if (!IsVehicleIndex(p1)) return CMD_ERROR; + if (!IsValidVehicleID(p1)) return CMD_ERROR; v = GetVehicle(p1); @@ -500,7 +500,7 @@ int32 CmdSendAircraftToHangar(TileIndex { Vehicle *v; - if (!IsVehicleIndex(p1)) return CMD_ERROR; + if (!IsValidVehicleID(p1)) return CMD_ERROR; v = GetVehicle(p1); @@ -568,7 +568,7 @@ int32 CmdRefitAircraft(TileIndex tile, u const AircraftVehicleInfo *avi; uint16 callback = CALLBACK_FAILED; - if (!IsVehicleIndex(p1)) return CMD_ERROR; + if (!IsValidVehicleID(p1)) return CMD_ERROR; v = GetVehicle(p1); diff --git a/depot.h b/depot.h --- a/depot.h +++ b/depot.h @@ -35,11 +35,6 @@ static inline uint16 GetDepotPoolSize(vo return _depot_pool.total_items; } -static inline bool IsDepotIndex(uint index) -{ - return index < GetDepotPoolSize(); -} - /** * Check if a depot really exists. */ @@ -48,6 +43,11 @@ static inline bool IsValidDepot(const De return depot->xy != 0; } +static inline bool IsValidDepotID(uint index) +{ + return index < GetDepotPoolSize() && IsValidDepot(GetDepot(index)); +} + #define FOR_ALL_DEPOTS_FROM(d, start) for (d = GetDepot(start); d != NULL; d = (d->index + 1 < GetDepotPoolSize()) ? GetDepot(d->index + 1) : NULL) if (IsValidDepot(d)) #define FOR_ALL_DEPOTS(d) FOR_ALL_DEPOTS_FROM(d, 0) diff --git a/order_cmd.c b/order_cmd.c --- a/order_cmd.c +++ b/order_cmd.c @@ -179,9 +179,11 @@ int32 CmdInsertOrder(TileIndex tile, uin OrderID sel_ord = GB(p1, 16, 16); Order new_order = UnpackOrder(p2); - if (!IsVehicleIndex(veh)) return CMD_ERROR; + if (!IsValidVehicleID(veh)) return CMD_ERROR; + v = GetVehicle(veh); - if (!IsValidVehicle(v) || !CheckOwnership(v->owner)) return CMD_ERROR; + + if (!CheckOwnership(v->owner)) return CMD_ERROR; /* Check if the inserted order is to the correct destination (owner, type), * and has the correct flags if any */ @@ -189,11 +191,10 @@ int32 CmdInsertOrder(TileIndex tile, uin case OT_GOTO_STATION: { const Station *st; - if (!IsStationIndex(new_order.station)) return CMD_ERROR; + if (!IsValidStationID(new_order.station)) return CMD_ERROR; st = GetStation(new_order.station); - if (!IsValidStation(st) || - (st->airport_type != AT_OILRIG && !IsBuoy(st) && !CheckOwnership(st->owner))) { + if (st->airport_type != AT_OILRIG && !IsBuoy(st) && !CheckOwnership(st->owner)) { return CMD_ERROR; } @@ -251,11 +252,10 @@ int32 CmdInsertOrder(TileIndex tile, uin if (v->type == VEH_Aircraft) { const Station* st; - if (!IsStationIndex(new_order.station)) return CMD_ERROR; + if (!IsValidStationID(new_order.station)) return CMD_ERROR; st = GetStation(new_order.station); - if (!IsValidStation(st) || - (st->airport_type != AT_OILRIG && !CheckOwnership(st->owner)) || + if ((st->airport_type != AT_OILRIG && !CheckOwnership(st->owner)) || !(st->facilities & FACIL_AIRPORT) || GetAirport(st->airport_type)->nof_depots == 0) { return CMD_ERROR; @@ -263,11 +263,10 @@ int32 CmdInsertOrder(TileIndex tile, uin } else { const Depot* dp; - if (!IsDepotIndex(new_order.station)) return CMD_ERROR; + if (!IsValidDepotID(new_order.station)) return CMD_ERROR; dp = GetDepot(new_order.station); - if (!IsValidDepot(dp) || !CheckOwnership(GetTileOwner(dp->xy))) - return CMD_ERROR; + if (!CheckOwnership(GetTileOwner(dp->xy))) return CMD_ERROR; switch (v->type) { case VEH_Train: @@ -309,7 +308,7 @@ int32 CmdInsertOrder(TileIndex tile, uin if (v->type != VEH_Train) return CMD_ERROR; - if (!IsWaypointIndex(new_order.station)) return CMD_ERROR; + if (!IsValidWaypointID(new_order.station)) return CMD_ERROR; wp = GetWaypoint(new_order.station); if (!CheckOwnership(GetTileOwner(wp->xy))) return CMD_ERROR; @@ -442,9 +441,11 @@ int32 CmdDeleteOrder(TileIndex tile, uin OrderID sel_ord = p2; Order *order; - if (!IsVehicleIndex(veh_id)) return CMD_ERROR; + if (!IsValidVehicleID(veh_id)) return CMD_ERROR; + v = GetVehicle(veh_id); - if (!IsValidVehicle(v) || !CheckOwnership(v->owner)) return CMD_ERROR; + + if (!CheckOwnership(v->owner)) return CMD_ERROR; /* If we did not select an order, we maybe want to de-clone the orders */ if (sel_ord >= v->num_orders) @@ -514,9 +515,11 @@ int32 CmdSkipOrder(TileIndex tile, uint3 Vehicle *v; VehicleID veh_id = p1; - if (!IsVehicleIndex(veh_id)) return CMD_ERROR; + if (!IsValidVehicleID(veh_id)) return CMD_ERROR; + v = GetVehicle(veh_id); - if (!IsValidVehicle(v) || !CheckOwnership(v->owner)) return CMD_ERROR; + + if (!CheckOwnership(v->owner)) return CMD_ERROR; if (flags & DC_EXEC) { /* Goto next order */ @@ -561,11 +564,12 @@ int32 CmdModifyOrder(TileIndex tile, uin OrderID sel_ord = GB(p1, 16, 16); // XXX - automatically truncated to 8 bits. VehicleID veh = GB(p1, 0, 16); - if (!IsVehicleIndex(veh)) return CMD_ERROR; + if (!IsValidVehicleID(veh)) return CMD_ERROR; if (p2 != OFB_FULL_LOAD && p2 != OFB_UNLOAD && p2 != OFB_NON_STOP && p2 != OFB_TRANSFER) return CMD_ERROR; v = GetVehicle(veh); - if (!IsValidVehicle(v) || !CheckOwnership(v->owner)) return CMD_ERROR; + + if (!CheckOwnership(v->owner)) return CMD_ERROR; /* Is it a valid order? */ if (sel_ord >= v->num_orders) return CMD_ERROR; @@ -628,22 +632,22 @@ int32 CmdCloneOrder(TileIndex tile, uint VehicleID veh_src = GB(p1, 16, 16); VehicleID veh_dst = GB(p1, 0, 16); - if (!IsVehicleIndex(veh_dst)) return CMD_ERROR; + if (!IsValidVehicleID(veh_dst)) return CMD_ERROR; dst = GetVehicle(veh_dst); - if (!IsValidVehicle(dst) || !CheckOwnership(dst->owner)) return CMD_ERROR; + if (!CheckOwnership(dst->owner)) return CMD_ERROR; switch (p2) { case CO_SHARE: { Vehicle *src; - if (!IsVehicleIndex(veh_src)) return CMD_ERROR; + if (!IsValidVehicleID(veh_src)) return CMD_ERROR; src = GetVehicle(veh_src); /* Sanity checks */ - if (!IsValidVehicle(src) || !CheckOwnership(src->owner) || dst->type != src->type || dst == src) + if (!CheckOwnership(src->owner) || dst->type != src->type || dst == src) return CMD_ERROR; /* Trucks can't share orders with busses (and visa versa) */ @@ -685,12 +689,12 @@ int32 CmdCloneOrder(TileIndex tile, uint Vehicle *src; int delta; - if (!IsVehicleIndex(veh_src)) return CMD_ERROR; + if (!IsValidVehicleID(veh_src)) return CMD_ERROR; src = GetVehicle(veh_src); /* Sanity checks */ - if (!IsValidVehicle(src) || !CheckOwnership(src->owner) || dst->type != src->type || dst == src) + if (!CheckOwnership(src->owner) || dst->type != src->type || dst == src) return CMD_ERROR; /* Trucks can't copy all the orders from busses (and visa versa) */ @@ -844,11 +848,12 @@ int32 CmdRestoreOrderIndex(TileIndex til OrderID cur_ord = GB(p2, 0, 16); uint16 serv_int = GB(p2, 16, 16); - if (!IsVehicleIndex(p1)) return CMD_ERROR; + if (!IsValidVehicleID(p1)) return CMD_ERROR; v = GetVehicle(p1); + /* Check the vehicle type and ownership, and if the service interval and order are in range */ - if (!IsValidVehicle(v) || !CheckOwnership(v->owner)) return CMD_ERROR; + if (!CheckOwnership(v->owner)) return CMD_ERROR; if (serv_int != GetServiceIntervalClamped(serv_int) || cur_ord >= v->num_orders) return CMD_ERROR; if (flags & DC_EXEC) { diff --git a/road_cmd.c b/road_cmd.c --- a/road_cmd.c +++ b/road_cmd.c @@ -293,7 +293,7 @@ int32 CmdBuildRoad(TileIndex tile, uint3 /* Road pieces are max 4 bitset values (NE, NW, SE, SW) and town can only be non-zero * if a non-player is building the road */ - if ((p1 >> 4) || (_current_player < MAX_PLAYERS && p2 != 0) || !IsTownIndex(p2)) return CMD_ERROR; + if ((p1 >> 4) || (_current_player < MAX_PLAYERS && p2 != 0) || !IsValidTownID(p2)) return CMD_ERROR; pieces = p1; tileh = GetTileSlope(tile, NULL); diff --git a/roadveh_cmd.c b/roadveh_cmd.c --- a/roadveh_cmd.c +++ b/roadveh_cmd.c @@ -212,7 +212,7 @@ int32 CmdStartStopRoadVeh(TileIndex tile Vehicle *v; uint16 callback; - if (!IsVehicleIndex(p1)) return CMD_ERROR; + if (!IsValidVehicleID(p1)) return CMD_ERROR; v = GetVehicle(p1); @@ -262,7 +262,7 @@ int32 CmdSellRoadVeh(TileIndex tile, uin { Vehicle *v; - if (!IsVehicleIndex(p1)) return CMD_ERROR; + if (!IsValidVehicleID(p1)) return CMD_ERROR; v = GetVehicle(p1); @@ -364,7 +364,7 @@ int32 CmdSendRoadVehToDepot(TileIndex ti Vehicle *v; const Depot *dep; - if (!IsVehicleIndex(p1)) return CMD_ERROR; + if (!IsValidVehicleID(p1)) return CMD_ERROR; v = GetVehicle(p1); @@ -411,7 +411,7 @@ int32 CmdTurnRoadVeh(TileIndex tile, uin { Vehicle *v; - if (!IsVehicleIndex(p1)) return CMD_ERROR; + if (!IsValidVehicleID(p1)) return CMD_ERROR; v = GetVehicle(p1); @@ -1757,7 +1757,7 @@ int32 CmdRefitRoadVeh(TileIndex tile, ui byte new_subtype = GB(p2, 8, 8); uint16 capacity = CALLBACK_FAILED; - if (!IsVehicleIndex(p1)) return CMD_ERROR; + if (!IsValidVehicleID(p1)) return CMD_ERROR; v = GetVehicle(p1); diff --git a/ship_cmd.c b/ship_cmd.c --- a/ship_cmd.c +++ b/ship_cmd.c @@ -929,7 +929,7 @@ int32 CmdSellShip(TileIndex tile, uint32 { Vehicle *v; - if (!IsVehicleIndex(p1)) return CMD_ERROR; + if (!IsValidVehicleID(p1)) return CMD_ERROR; v = GetVehicle(p1); @@ -964,7 +964,7 @@ int32 CmdStartStopShip(TileIndex tile, u Vehicle *v; uint16 callback; - if (!IsVehicleIndex(p1)) return CMD_ERROR; + if (!IsValidVehicleID(p1)) return CMD_ERROR; v = GetVehicle(p1); @@ -1002,7 +1002,7 @@ int32 CmdSendShipToDepot(TileIndex tile, Vehicle *v; const Depot *dep; - if (!IsVehicleIndex(p1)) return CMD_ERROR; + if (!IsValidVehicleID(p1)) return CMD_ERROR; v = GetVehicle(p1); @@ -1056,7 +1056,7 @@ int32 CmdRefitShip(TileIndex tile, uint3 byte new_subtype = GB(p2, 8, 8); uint16 capacity = CALLBACK_FAILED; - if (!IsVehicleIndex(p1)) return CMD_ERROR; + if (!IsValidVehicleID(p1)) return CMD_ERROR; v = GetVehicle(p1); diff --git a/signs.c b/signs.c --- a/signs.c +++ b/signs.c @@ -147,7 +147,7 @@ int32 CmdPlaceSign(TileIndex tile, uint3 */ int32 CmdRenameSign(TileIndex tile, uint32 flags, uint32 p1, uint32 p2) { - if (!IsSignIndex(p1)) return CMD_ERROR; + if (!IsValidSignID(p1)) return CMD_ERROR; /* If _cmd_text 0 means the new text for the sign is non-empty. * So rename the sign. If it is empty, it has no name, so delete it */ diff --git a/signs.h b/signs.h --- a/signs.h +++ b/signs.h @@ -34,11 +34,6 @@ static inline uint16 GetSignPoolSize(voi return _sign_pool.total_items; } -static inline bool IsSignIndex(uint index) -{ - return index < GetSignPoolSize(); -} - /** * Check if a Sign really exists. */ @@ -47,6 +42,11 @@ static inline bool IsValidSign(const Sig return si->str != STR_NULL; } +static inline bool IsValidSignID(uint index) +{ + return index < GetSignPoolSize() && IsValidSign(GetSign(index)); +} + #define FOR_ALL_SIGNS_FROM(ss, start) for (ss = GetSign(start); ss != NULL; ss = (ss->index + 1 < GetSignPoolSize()) ? GetSign(ss->index + 1) : NULL) if (IsValidSign(ss)) #define FOR_ALL_SIGNS(ss) FOR_ALL_SIGNS_FROM(ss, 0) diff --git a/station.h b/station.h --- a/station.h +++ b/station.h @@ -161,11 +161,6 @@ static inline uint16 GetStationPoolSize( return _station_pool.total_items; } -static inline bool IsStationIndex(StationID index) -{ - return index < GetStationPoolSize(); -} - /** * Check if a station really exists. */ @@ -174,6 +169,11 @@ static inline bool IsValidStation(const return st->xy != 0; } +static inline bool IsValidStationID(StationID index) +{ + return index < GetStationPoolSize() && IsValidStation(GetStation(index)); +} + #define FOR_ALL_STATIONS_FROM(st, start) for (st = GetStation(start); st != NULL; st = (st->index + 1 < GetStationPoolSize()) ? GetStation(st->index + 1) : NULL) if (IsValidStation(st)) #define FOR_ALL_STATIONS(st) FOR_ALL_STATIONS_FROM(st, 0) diff --git a/station_cmd.c b/station_cmd.c --- a/station_cmd.c +++ b/station_cmd.c @@ -2573,8 +2573,7 @@ void OnTick_Station(void) i = _station_tick_ctr; if (++_station_tick_ctr == GetStationPoolSize()) _station_tick_ctr = 0; - st = GetStation(i); - if (IsValidStation(st)) StationHandleBigTick(st); + if (IsValidStationID(i)) StationHandleBigTick(GetStation(i)); FOR_ALL_STATIONS(st) { StationHandleSmallTick(st); @@ -2627,10 +2626,10 @@ int32 CmdRenameStation(TileIndex tile, u StringID str; Station *st; - if (!IsStationIndex(p1) || _cmd_text[0] == '\0') return CMD_ERROR; + if (!IsValidStationID(p1) || _cmd_text[0] == '\0') return CMD_ERROR; st = GetStation(p1); - if (!IsValidStation(st) || !CheckOwnership(st->owner)) return CMD_ERROR; + if (!CheckOwnership(st->owner)) return CMD_ERROR; str = AllocateNameUnique(_cmd_text, 6); if (str == 0) return CMD_ERROR; diff --git a/town.h b/town.h --- a/town.h +++ b/town.h @@ -179,9 +179,9 @@ static inline uint16 GetTownPoolSize(voi return _town_pool.total_items; } -static inline bool IsTownIndex(uint index) +static inline bool IsValidTownID(uint index) { - return index < GetTownPoolSize(); + return index < GetTownPoolSize() && IsValidTown(GetTown(index)); } #define FOR_ALL_TOWNS_FROM(t, start) for (t = GetTown(start); t != NULL; t = (t->index + 1 < GetTownPoolSize()) ? GetTown(t->index + 1) : NULL) if (IsValidTown(t)) diff --git a/town_cmd.c b/town_cmd.c --- a/town_cmd.c +++ b/town_cmd.c @@ -409,14 +409,11 @@ void OnTick_Town(void) _cur_town_iter >= TOWN_GROWTH_FREQUENCY; _cur_town_iter -= TOWN_GROWTH_FREQUENCY) { uint32 i = _cur_town_ctr; - Town *t; if (++_cur_town_ctr >= GetTownPoolSize()) _cur_town_ctr = 0; - t = GetTown(i); - - if (IsValidTown(t)) TownTickHandler(t); + if (IsValidTownID(i)) TownTickHandler(GetTown(i)); } } @@ -1348,7 +1345,7 @@ int32 CmdRenameTown(TileIndex tile, uint StringID str; Town *t; - if (!IsTownIndex(p1) || _cmd_text[0] == '\0') return CMD_ERROR; + if (!IsValidTownID(p1) || _cmd_text[0] == '\0') return CMD_ERROR; t = GetTown(p1); @@ -1605,7 +1602,7 @@ int32 CmdDoTownAction(TileIndex tile, ui int32 cost; Town *t; - if (!IsTownIndex(p1) || p2 > lengthof(_town_action_proc)) return CMD_ERROR; + if (!IsValidTownID(p1) || p2 > lengthof(_town_action_proc)) return CMD_ERROR; t = GetTown(p1); diff --git a/train_cmd.c b/train_cmd.c --- a/train_cmd.c +++ b/train_cmd.c @@ -951,7 +951,7 @@ int32 CmdMoveRailVehicle(TileIndex tile, VehicleID d = GB(p1, 16, 16); Vehicle *src, *dst, *src_head, *dst_head; - if (!IsVehicleIndex(s)) return CMD_ERROR; + if (!IsValidVehicleID(s)) return CMD_ERROR; src = GetVehicle(s); @@ -1230,7 +1230,7 @@ int32 CmdStartStopTrain(TileIndex tile, Vehicle *v; uint16 callback; - if (!IsVehicleIndex(p1)) return CMD_ERROR; + if (!IsValidVehicleID(p1)) return CMD_ERROR; v = GetVehicle(p1); @@ -1275,7 +1275,7 @@ int32 CmdSellRailWagon(TileIndex tile, u Vehicle *new_f = NULL; int32 cost = 0; - if (!IsVehicleIndex(p1) || p2 > 2) return CMD_ERROR; + if (!IsValidVehicleID(p1) || p2 > 2) return CMD_ERROR; v = GetVehicle(p1); @@ -1671,7 +1671,7 @@ int32 CmdReverseTrainDirection(TileIndex { Vehicle *v; - if (!IsVehicleIndex(p1)) return CMD_ERROR; + if (!IsValidVehicleID(p1)) return CMD_ERROR; v = GetVehicle(p1); @@ -1720,7 +1720,7 @@ int32 CmdForceTrainProceed(TileIndex til { Vehicle *v; - if (!IsVehicleIndex(p1)) return CMD_ERROR; + if (!IsValidVehicleID(p1)) return CMD_ERROR; v = GetVehicle(p1); @@ -1746,7 +1746,7 @@ int32 CmdRefitRailVehicle(TileIndex tile int32 cost; uint num; - if (!IsVehicleIndex(p1)) return CMD_ERROR; + if (!IsValidVehicleID(p1)) return CMD_ERROR; v = GetVehicle(p1); @@ -1930,7 +1930,7 @@ int32 CmdSendTrainToDepot(TileIndex tile Vehicle *v; TrainFindDepotData tfdd; - if (!IsVehicleIndex(p1)) return CMD_ERROR; + if (!IsValidVehicleID(p1)) return CMD_ERROR; v = GetVehicle(p1); diff --git a/vehicle.c b/vehicle.c --- a/vehicle.c +++ b/vehicle.c @@ -1526,7 +1526,7 @@ int32 CmdCloneVehicle(TileIndex tile, ui int cost, total_cost = 0; uint32 build_argument = 2; - if (!IsVehicleIndex(p1)) return CMD_ERROR; + if (!IsValidVehicleID(p1)) return CMD_ERROR; v = GetVehicle(p1); v_front = v; w = NULL; @@ -1907,7 +1907,7 @@ int32 CmdNameVehicle(TileIndex tile, uin Vehicle *v; StringID str; - if (!IsVehicleIndex(p1) || _cmd_text[0] == '\0') return CMD_ERROR; + if (!IsValidVehicleID(p1) || _cmd_text[0] == '\0') return CMD_ERROR; v = GetVehicle(p1); @@ -1940,11 +1940,11 @@ int32 CmdChangeServiceInt(TileIndex tile Vehicle* v; uint16 serv_int = GetServiceIntervalClamped(p2); /* Double check the service interval from the user-input */ - if (serv_int != p2 || !IsVehicleIndex(p1)) return CMD_ERROR; + if (serv_int != p2 || !IsValidVehicleID(p1)) return CMD_ERROR; v = GetVehicle(p1); - if (!IsValidVehicle(v) || !CheckOwnership(v->owner)) return CMD_ERROR; + if (!CheckOwnership(v->owner)) return CMD_ERROR; if (flags & DC_EXEC) { v->service_interval = serv_int; diff --git a/vehicle.h b/vehicle.h --- a/vehicle.h +++ b/vehicle.h @@ -375,9 +375,9 @@ static inline bool IsValidVehicle(const * * @return Returns true if the vehicle-id is in range */ -static inline bool IsVehicleIndex(uint index) +static inline bool IsValidVehicleID(uint index) { - return index < GetVehiclePoolSize(); + return index < GetVehiclePoolSize() && IsValidVehicle(GetVehicle(index)); } /* Returns order 'index' of a vehicle or NULL when it doesn't exists */ diff --git a/waypoint.c b/waypoint.c --- a/waypoint.c +++ b/waypoint.c @@ -319,7 +319,7 @@ int32 CmdRenameWaypoint(TileIndex tile, { Waypoint *wp; - if (!IsWaypointIndex(p1)) return CMD_ERROR; + if (!IsValidWaypointID(p1)) return CMD_ERROR; if (_cmd_text[0] != '\0') { StringID str = AllocateNameUnique(_cmd_text, 0); diff --git a/waypoint.h b/waypoint.h --- a/waypoint.h +++ b/waypoint.h @@ -42,11 +42,6 @@ static inline uint16 GetWaypointPoolSize return _waypoint_pool.total_items; } -static inline bool IsWaypointIndex(uint index) -{ - return index < GetWaypointPoolSize(); -} - /** * Check if a Waypoint really exists. */ @@ -55,6 +50,11 @@ static inline bool IsValidWaypoint(const return wp->xy != 0; } +static inline bool IsValidWaypointID(uint index) +{ + return index < GetWaypointPoolSize() && IsValidWaypoint(GetWaypoint(index)); +} + #define FOR_ALL_WAYPOINTS_FROM(wp, start) for (wp = GetWaypoint(start); wp != NULL; wp = (wp->index + 1 < GetWaypointPoolSize()) ? GetWaypoint(wp->index + 1) : NULL) if (IsValidWaypoint(wp)) #define FOR_ALL_WAYPOINTS(wp) FOR_ALL_WAYPOINTS_FROM(wp, 0)