Skip to content

Commit

Permalink
Avoid dereferencing map_get_first_element_at nullptr on libopenrct2 (#…
Browse files Browse the repository at this point in the history
…10013)

* Avoid dereferencing map_get_first_element_at nullptr on Map.cpp

* Avoid dereferencing map_get_first_element_at nullptr on MapAnimation.cpp

Returning true or internal control variable, based on what was seen on `map_animation_invalidate_track_onridephoto`

* Avoid dereferencing map_get_first_element_at nullptr on Park.cpp

* Avoid dereferencing map_get_first_element_at nullptr on Scenery.cpp

* Avoid dereferencing map_get_first_element_at nullptr on Sprite.cpp

* Avoid dereferencing map_get_first_element_at nullptr on TileInspector.cpp

* Avoid dereferencing map_get_first_element_at nullptr on Wall.cpp

* Avoid dereferencing map_get_first_element_at nullptr on Fountain.cpp

* Avoid dereferencing map_get_first_element_at nullptr on Footpath.cpp

* Avoid dereferencing map_get_first_element_at nullptr on Entrance.cpp

* Avoid dereferencing map_get_first_element_at nullptr on Banner.cpp

* Avoid dereferencing map_get_first_element_at nullptr on Vehicle.cpp

* Avoid dereferencing map_get_first_element_at nullptr on TrackDesignSave.cpp

* Avoid dereferencing map_get_first_element_at nullptr on TrackDesign.cpp

* Avoid dereferencing map_get_first_element_at nullptr on Track.cpp

* Avoid dereferencing map_get_first_element_at nullptr on Station.cpp

* Avoid dereferencing map_get_first_element_at nullptr on RideRatings.cpp

* Avoid dereferencing map_get_first_element_at nullptr on Ride.cpp

* Avoid dereferencing map_get_first_element_at nullptr on S4Importer.cpp

* Avoid dereferencing map_get_first_element_at nullptr on Staff.cpp

* Avoid dereferencing map_get_first_element_at nullptr on Peep.cpp

* Avoid dereferencing map_get_first_element_at nullptr on GuestPathfinding.cpp

* Avoid dereferencing map_get_first_element_at nullptr on Guest.cpp

* Avoid dereferencing map_get_first_element_at nullptr on VirtualFloor.cpp

* Avoid dereferencing map_get_first_element_at nullptr on Paint.TileElement.cpp

* Fix issues raised on review

* Fix remaining review issues.

* Early exit on loops if tileElement is nullptr

* Fix clang-format issues
  • Loading branch information
tupaschoal authored and duncanspumpkin committed Oct 9, 2019
1 parent 0a00f62 commit b793d7e
Show file tree
Hide file tree
Showing 25 changed files with 198 additions and 4 deletions.
2 changes: 2 additions & 0 deletions src/openrct2/paint/VirtualFloor.cpp
Expand Up @@ -243,6 +243,8 @@ static void virtual_floor_get_tile_properties(
// * Walls / banners, which are displayed as occupied edges
// * Ghost objects, which are displayed as lit squares
TileElement* tileElement = map_get_first_element_at(x >> 5, y >> 5);
if (tileElement == nullptr)
return;
do
{
int32_t elementType = tileElement->GetType();
Expand Down
2 changes: 2 additions & 0 deletions src/openrct2/paint/tile_element/Paint.TileElement.cpp
Expand Up @@ -155,6 +155,8 @@ static void sub_68B3FB(paint_session* session, int32_t x, int32_t y)
session->MapPosition.y = y;

TileElement* tile_element = map_get_first_element_at(x >> 5, y >> 5);
if (tile_element == nullptr)
return;
uint8_t rotation = session->CurrentRotation;

bool partOfVirtualFloor = false;
Expand Down
23 changes: 22 additions & 1 deletion src/openrct2/peep/Guest.cpp
Expand Up @@ -959,6 +959,8 @@ void Guest::Tick128UpdateGuest(int32_t index)
bool found = false;
do
{
if (tileElement == nullptr)
break;
if (tileElement->GetType() != TILE_ELEMENT_TYPE_PATH)
continue;
if (tileElement->base_height != next_z)
Expand Down Expand Up @@ -2948,7 +2950,8 @@ static PeepThoughtType peep_assess_surroundings(int16_t centre_x, int16_t centre
for (int16_t y = initial_y; y < final_y; y += 32)
{
TileElement* tileElement = map_get_first_element_at(x / 32, y / 32);

if (tileElement == nullptr)
continue;
do
{
Ride* ride;
Expand Down Expand Up @@ -4176,6 +4179,8 @@ void Guest::UpdateRideLeaveVehicle()
continue;

TileElement* inner_map = map_get_first_element_at(vehicle->track_x / 32, vehicle->track_y / 32);
if (inner_map == nullptr)
continue;
for (;; inner_map++)
{
if (inner_map->GetType() != TILE_ELEMENT_TYPE_TRACK)
Expand Down Expand Up @@ -4937,6 +4942,8 @@ void Guest::UpdateRideMazePathfinding()
maze_type mazeType = maze_type::invalid;

auto tileElement = map_get_first_element_at(targetLoc.x / 32, targetLoc.y / 32);
if (tileElement == nullptr)
return;
do
{
if (stationHeight != tileElement->base_height)
Expand Down Expand Up @@ -5033,6 +5040,8 @@ void Guest::UpdateRideLeaveExit()

// Find the station track element
TileElement* tileElement = map_get_first_element_at(targetLoc.x / 32, targetLoc.y / 32);
if (tileElement == nullptr)
return;
do
{
if (tileElement->GetType() != TILE_ELEMENT_TYPE_PATH)
Expand Down Expand Up @@ -5452,6 +5461,8 @@ void Guest::UpdateWalking()
return;

TileElement* tileElement = map_get_first_element_at(next_x / 32, next_y / 32);
if (tileElement == nullptr)
return;

for (;; tileElement++)
{
Expand Down Expand Up @@ -5868,6 +5879,8 @@ void Guest::UpdateUsingBin()
}

TileElement* tileElement = map_get_first_element_at(next_x / 32, next_y / 32);
if (tileElement == nullptr)
return;

for (;; tileElement++)
{
Expand Down Expand Up @@ -6036,6 +6049,8 @@ bool Guest::UpdateWalkingFindBench()
return false;

TileElement* tileElement = map_get_first_element_at(next_x / 32, next_y / 32);
if (tileElement == nullptr)
return false;

for (;; tileElement++)
{
Expand Down Expand Up @@ -6131,6 +6146,8 @@ bool Guest::UpdateWalkingFindBin()
return false;

TileElement* tileElement = map_get_first_element_at(peep->next_x / 32, peep->next_y / 32);
if (tileElement == nullptr)
return false;

for (;; tileElement++)
{
Expand Down Expand Up @@ -6232,6 +6249,8 @@ static void peep_update_walking_break_scenery(Peep* peep)
return;

TileElement* tileElement = map_get_first_element_at(peep->next_x / 32, peep->next_y / 32);
if (tileElement == nullptr)
return;

for (;; tileElement++)
{
Expand Down Expand Up @@ -6875,6 +6894,8 @@ void Guest::UpdateSpriteType()
TileElement* tileElement = map_get_first_element_at(x / 32, y / 32);
while (true)
{
if (tileElement == nullptr)
break;
if ((z / 8) < tileElement->base_height)
break;

Expand Down
6 changes: 6 additions & 0 deletions src/openrct2/peep/GuestPathfinding.cpp
Expand Up @@ -239,6 +239,8 @@ static uint8_t footpath_element_next_in_direction(TileCoordsXYZ loc, PathElement
nextTileElement = map_get_first_element_at(loc.x, loc.y);
do
{
if (nextTileElement == nullptr)
break;
if (nextTileElement->IsGhost())
continue;
if (nextTileElement->GetType() != TILE_ELEMENT_TYPE_PATH)
Expand Down Expand Up @@ -1209,6 +1211,8 @@ Direction peep_pathfind_choose_direction(TileCoordsXYZ loc, Peep* peep)
bool isThin = false;
do
{
if (dest_tile_element == nullptr)
break;
if (dest_tile_element->base_height != loc.z)
continue;
if (dest_tile_element->GetType() != TILE_ELEMENT_TYPE_PATH)
Expand Down Expand Up @@ -1768,6 +1772,8 @@ static void get_ride_queue_end(TileCoordsXYZ& loc)

tileElement = map_get_first_element_at(nextTile.x, nextTile.y);
found = false;
if (tileElement == nullptr)
break;
do
{
if (tileElement == firstPathElement)
Expand Down
6 changes: 6 additions & 0 deletions src/openrct2/peep/Peep.cpp
Expand Up @@ -484,6 +484,8 @@ bool Peep::CheckForPath()

do
{
if (tile_element == nullptr)
break;
if (tile_element->GetType() == map_type)
{
if (height == tile_element->base_height)
Expand Down Expand Up @@ -2541,6 +2543,8 @@ static void peep_interact_with_entrance(Peep* peep, int16_t x, int16_t y, TileEl
TileElement* nextTileElement = map_get_first_element_at(next_x / 32, next_y / 32);
do
{
if (nextTileElement == nullptr)
break;
if (nextTileElement->GetType() != TILE_ELEMENT_TYPE_PATH)
continue;

Expand Down Expand Up @@ -3092,6 +3096,8 @@ void Peep::PerformNextAction(uint8_t& pathing_result, TileElement*& tile_result)
}

TileElement* tileElement = map_get_first_element_at(newLoc.x / 32, newLoc.y / 32);
if (tileElement == nullptr)
return;
int16_t base_z = std::max(0, (z / 8) - 2);
int16_t top_z = (z / 8) + 1;

Expand Down
11 changes: 10 additions & 1 deletion src/openrct2/peep/Staff.cpp
Expand Up @@ -268,6 +268,8 @@ bool staff_can_ignore_wide_flag(Peep* staff, int32_t x, int32_t y, uint8_t z, Ti

/* Search through all adjacent map elements */
TileElement* test_element = map_get_first_element_at(adjac_x / 32, adjac_y / 32);
if (test_element == nullptr)
return false;
bool pathfound = false;
bool widefound = false;
do
Expand Down Expand Up @@ -474,7 +476,8 @@ static uint8_t staff_handyman_direction_to_nearest_litter(Peep* peep)
int16_t nextZ = ((peep->z + 8) & 0xFFF0) / 8;

TileElement* tileElement = map_get_first_element_at(nextTile.x / 32, nextTile.y / 32);

if (tileElement == nullptr)
return 0xFF;
do
{
if (tileElement->base_height != nextZ)
Expand All @@ -489,6 +492,8 @@ static uint8_t staff_handyman_direction_to_nearest_litter(Peep* peep)
nextTile.y = (peep->y & 0xFFE0) + CoordsDirectionDelta[nextDirection].y;

tileElement = map_get_first_element_at(nextTile.x / 32, nextTile.y / 32);
if (tileElement == nullptr)
return 0xFF;

do
{
Expand Down Expand Up @@ -1250,6 +1255,8 @@ void Staff::UpdateWatering()
int32_t actionY = next_y + CoordsDirectionDelta[var_37].y;

TileElement* tile_element = map_get_first_element_at(actionX / 32, actionY / 32);
if (tile_element == nullptr)
return;

do
{
Expand Down Expand Up @@ -1315,6 +1322,8 @@ void Staff::UpdateEmptyingBin()
return;

TileElement* tile_element = map_get_first_element_at(next_x / 32, next_y / 32);
if (tile_element == nullptr)
return;

for (;; tile_element++)
{
Expand Down
4 changes: 4 additions & 0 deletions src/openrct2/rct1/S4Importer.cpp
Expand Up @@ -2743,6 +2743,8 @@ class S4Importer final : public IParkImporter
for (int32_t y = 0; y < RCT1_MAX_MAP_SIZE; y++)
{
TileElement* tileElement = map_get_first_element_at(x, y);
if (tileElement == nullptr)
continue;
do
{
if (tileElement->GetType() == TILE_ELEMENT_TYPE_WALL)
Expand Down Expand Up @@ -2998,6 +3000,8 @@ class S4Importer final : public IParkImporter
for (int32_t y = 0; y < RCT1_MAX_MAP_SIZE; y++)
{
TileElement* tileElement = map_get_first_element_at(x, y);
if (tileElement == nullptr)
continue;
do
{
if (tileElement->GetType() == TILE_ELEMENT_TYPE_TRACK)
Expand Down
18 changes: 18 additions & 0 deletions src/openrct2/ride/Ride.cpp
Expand Up @@ -4944,6 +4944,8 @@ static bool ride_initialise_cable_lift_track(Ride* ride, bool isApplying)

bool success = false;
TileElement* tileElement = map_get_first_element_at(location.x, location.y);
if (tileElement == nullptr)
return success;
do
{
if (tileElement->GetType() != TILE_ELEMENT_TYPE_TRACK)
Expand Down Expand Up @@ -5182,6 +5184,8 @@ static TileElement* loc_6B4F6B(ride_id_t rideIndex, int32_t x, int32_t y)
return nullptr;

TileElement* tileElement = map_get_first_element_at(x / 32, y / 32);
if (tileElement == nullptr)
return nullptr;
do
{
if (tileElement->GetType() != TILE_ELEMENT_TYPE_TRACK)
Expand Down Expand Up @@ -6251,6 +6255,8 @@ void ride_get_entrance_or_exit_position_from_screen_position(
if (mapX >= 0 && mapY >= 0 && mapX < (256 * 32) && mapY < (256 * 32))
{
tileElement = map_get_first_element_at(mapX >> 5, mapY >> 5);
if (tileElement == nullptr)
continue;
do
{
if (tileElement->GetType() != TILE_ELEMENT_TYPE_TRACK)
Expand Down Expand Up @@ -6306,6 +6312,8 @@ void ride_get_entrance_or_exit_position_from_screen_position(
mapX -= CoordsDirectionDelta[direction].x;
mapY -= CoordsDirectionDelta[direction].y;
tileElement = map_get_first_element_at(mapX >> 5, mapY >> 5);
if (tileElement == nullptr)
break;
bool goToNextTile = false;

do
Expand Down Expand Up @@ -6648,6 +6656,8 @@ static int32_t ride_get_track_length(Ride* ride)
z = ride->stations[i].Height;

tileElement = map_get_first_element_at(x >> 5, y >> 5);
if (tileElement == nullptr)
continue;
do
{
if (tileElement->GetType() != TILE_ELEMENT_TYPE_TRACK)
Expand Down Expand Up @@ -6904,6 +6914,8 @@ void sub_6CB945(Ride* ride)
location.y -= CoordsDirectionDelta[direction].y;
}
tileElement = map_get_first_element_at(location.x >> 5, location.y >> 5);
if (tileElement == nullptr)
break;

bool trackFound = false;
do
Expand Down Expand Up @@ -6951,6 +6963,8 @@ void sub_6CB945(Ride* ride)

bool trackFound = false;
tileElement = map_get_first_element_at(blockLocation.x >> 5, blockLocation.y >> 5);
if (tileElement == nullptr)
break;
do
{
if (blockLocation.z != tileElement->base_height)
Expand Down Expand Up @@ -7016,6 +7030,8 @@ void sub_6CB945(Ride* ride)
CoordsXY location = { locationCoords.x * 32, locationCoords.y * 32 };

TileElement* tileElement = map_get_first_element_at(location.x >> 5, location.y >> 5);
if (tileElement == nullptr)
continue;
do
{
if (tileElement->GetType() != TILE_ELEMENT_TYPE_ENTRANCE)
Expand All @@ -7031,6 +7047,8 @@ void sub_6CB945(Ride* ride)

bool shouldRemove = true;
TileElement* trackElement = map_get_first_element_at(nextLocation.x >> 5, nextLocation.y >> 5);
if (trackElement == nullptr)
continue;
do
{
if (trackElement->GetType() != TILE_ELEMENT_TYPE_TRACK)
Expand Down
18 changes: 18 additions & 0 deletions src/openrct2/ride/RideRatings.cpp
Expand Up @@ -207,6 +207,11 @@ static void ride_ratings_update_state_2()
int32_t trackType = gRideRatingsCalcData.proximity_track_type;

TileElement* tileElement = map_get_first_element_at(x, y);
if (tileElement == nullptr)
{
gRideRatingsCalcData.state = RIDE_RATINGS_STATE_FIND_NEXT_RIDE;
return;
}
do
{
if (tileElement->IsGhost())
Expand Down Expand Up @@ -313,6 +318,11 @@ static void ride_ratings_update_state_5()
int32_t trackType = gRideRatingsCalcData.proximity_track_type;

TileElement* tileElement = map_get_first_element_at(x, y);
if (tileElement == nullptr)
{
gRideRatingsCalcData.state = RIDE_RATINGS_STATE_FIND_NEXT_RIDE;
return;
}
do
{
if (tileElement->IsGhost())
Expand Down Expand Up @@ -419,6 +429,8 @@ static void ride_ratings_score_close_proximity_in_direction(TileElement* inputTi
return;

TileElement* tileElement = map_get_first_element_at(x >> 5, y >> 5);
if (tileElement == nullptr)
return;
do
{
if (tileElement->IsGhost())
Expand Down Expand Up @@ -471,6 +483,8 @@ static void ride_ratings_score_close_proximity_in_direction(TileElement* inputTi
static void ride_ratings_score_close_proximity_loops_helper(TileElement* inputTileElement, int32_t x, int32_t y)
{
TileElement* tileElement = map_get_first_element_at(x >> 5, y >> 5);
if (tileElement == nullptr)
return;
do
{
if (tileElement->IsGhost())
Expand Down Expand Up @@ -545,6 +559,8 @@ static void ride_ratings_score_close_proximity(TileElement* inputTileElement)
int32_t x = gRideRatingsCalcData.proximity_x;
int32_t y = gRideRatingsCalcData.proximity_y;
TileElement* tileElement = map_get_first_element_at(x >> 5, y >> 5);
if (tileElement == nullptr)
return;
do
{
if (tileElement->IsGhost())
Expand Down Expand Up @@ -1438,6 +1454,8 @@ static int32_t ride_ratings_get_scenery_score(Ride* ride)
{
// Count scenery items on this tile
TileElement* tileElement = map_get_first_element_at(xx, yy);
if (tileElement == nullptr)
continue;
do
{
if (tileElement->IsGhost())
Expand Down
4 changes: 4 additions & 0 deletions src/openrct2/ride/Station.cpp
Expand Up @@ -340,6 +340,8 @@ TileElement* ride_get_station_start_track_element(Ride* ride, int32_t stationInd

// Find the station track element
TileElement* tileElement = map_get_first_element_at(x, y);
if (tileElement == nullptr)
return nullptr;
do
{
if (tileElement->GetType() == TILE_ELEMENT_TYPE_TRACK && z == tileElement->base_height)
Expand All @@ -354,6 +356,8 @@ TileElement* ride_get_station_exit_element(int32_t x, int32_t y, int32_t z)
{
// Find the station track element
TileElement* tileElement = map_get_first_element_at(x, y);
if (tileElement == nullptr)
return nullptr;
do
{
if (tileElement == nullptr)
Expand Down

0 comments on commit b793d7e

Please sign in to comment.