Merge pull request #17801 from Gymnasiast/fix/17797

Fix #17797: NPE in ride_check_block_brakes()
This commit is contained in:
Michael Steenbeek 2022-08-13 17:55:06 +02:00 committed by GitHub
commit b3d65c9ee6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 31 additions and 25 deletions

View File

@ -565,7 +565,7 @@ bool ViewportInteractionRightClick(const ScreenCoordsXY& screenCoords)
break;
case ViewportInteractionItem::Ride:
tileElement = { info.Loc, info.Element };
ride_modify(&tileElement);
ride_modify(tileElement);
break;
case ViewportInteractionItem::Scenery:
ViewportInteractionRemoveScenery(info.Element, info.Loc);

View File

@ -2666,15 +2666,18 @@ void Ride::ChainQueues() const
*
* rct2: 0x006D3319
*/
static ResultWithMessage ride_check_block_brakes(CoordsXYE* input, CoordsXYE* output)
static ResultWithMessage RideCheckBlockBrakes(const CoordsXYE& input, CoordsXYE* output)
{
RideId rideIndex = input->element->AsTrack()->GetRideIndex();
if (input.element == nullptr || input.element->GetType() != TileElementType::Track)
return { false };
RideId rideIndex = input.element->AsTrack()->GetRideIndex();
rct_window* w = window_find_by_class(WC_RIDE_CONSTRUCTION);
if (w != nullptr && _rideConstructionState != RideConstructionState::State0 && _currentRideIndex == rideIndex)
ride_construction_invalidate_current_track();
track_circuit_iterator it;
track_circuit_iterator_begin(&it, *input);
track_circuit_iterator_begin(&it, input);
while (track_circuit_iterator_next(&it))
{
if (it.current.element->AsTrack()->GetTrackType() == TrackElemType::BlockBrakes)
@ -3817,7 +3820,7 @@ void Ride::ConstructMissingEntranceOrExit() const
CoordsXYE trackElement;
ride_try_get_origin_element(this, &trackElement);
ride_find_track_gap(this, &trackElement, &trackElement);
int32_t ok = ride_modify(&trackElement);
int32_t ok = ride_modify(trackElement);
if (ok == 0)
{
return;
@ -3833,12 +3836,15 @@ void Ride::ConstructMissingEntranceOrExit() const
*
* rct2: 0x006B528A
*/
static void ride_scroll_to_track_error(CoordsXYE* trackElement)
static void ride_scroll_to_track_error(const CoordsXYE& trackElement)
{
if (trackElement.element == nullptr)
return;
auto* w = window_get_main();
if (w != nullptr)
{
window_scroll_to_location(*w, { *trackElement, trackElement->element->GetBaseZ() });
window_scroll_to_location(*w, { trackElement, trackElement.element->GetBaseZ() });
ride_modify(trackElement);
}
}
@ -3921,17 +3927,17 @@ ResultWithMessage Ride::Test(RideStatus newStatus, bool isApplying)
if (ride_find_track_gap(this, &trackElement, &problematicTrackElement)
&& (newStatus != RideStatus::Simulating || IsBlockSectioned()))
{
ride_scroll_to_track_error(&problematicTrackElement);
ride_scroll_to_track_error(problematicTrackElement);
return { false, STR_TRACK_IS_NOT_A_COMPLETE_CIRCUIT };
}
}
if (IsBlockSectioned())
{
auto blockBrakeCheck = ride_check_block_brakes(&trackElement, &problematicTrackElement);
auto blockBrakeCheck = RideCheckBlockBrakes(trackElement, &problematicTrackElement);
if (!blockBrakeCheck.Successful)
{
ride_scroll_to_track_error(&problematicTrackElement);
ride_scroll_to_track_error(problematicTrackElement);
return { false, blockBrakeCheck.Message };
}
}
@ -3943,7 +3949,7 @@ ResultWithMessage Ride::Test(RideStatus newStatus, bool isApplying)
{
if (ride_check_track_contains_inversions(&trackElement, &problematicTrackElement))
{
ride_scroll_to_track_error(&problematicTrackElement);
ride_scroll_to_track_error(problematicTrackElement);
return { false, STR_TRACK_UNSUITABLE_FOR_TYPE_OF_TRAIN };
}
}
@ -3951,7 +3957,7 @@ ResultWithMessage Ride::Test(RideStatus newStatus, bool isApplying)
{
if (ride_check_track_contains_banked(&trackElement, &problematicTrackElement))
{
ride_scroll_to_track_error(&problematicTrackElement);
ride_scroll_to_track_error(problematicTrackElement);
return { false, STR_TRACK_UNSUITABLE_FOR_TYPE_OF_TRAIN };
}
}
@ -3966,13 +3972,13 @@ ResultWithMessage Ride::Test(RideStatus newStatus, bool isApplying)
if (!ride_check_station_length(&trackElement, &problematicTrackElement))
{
ride_scroll_to_track_error(&problematicTrackElement);
ride_scroll_to_track_error(problematicTrackElement);
return { false, STR_STATION_NOT_LONG_ENOUGH };
}
if (!ride_check_start_and_end_is_station(&trackElement))
{
ride_scroll_to_track_error(&problematicTrackElement);
ride_scroll_to_track_error(problematicTrackElement);
return { false, STR_RIDE_MUST_START_AND_END_WITH_STATIONS };
}
}
@ -4056,17 +4062,17 @@ ResultWithMessage Ride::Open(bool isApplying)
{
if (ride_find_track_gap(this, &trackElement, &problematicTrackElement))
{
ride_scroll_to_track_error(&problematicTrackElement);
ride_scroll_to_track_error(problematicTrackElement);
return { false, STR_TRACK_IS_NOT_A_COMPLETE_CIRCUIT };
}
}
if (IsBlockSectioned())
{
auto blockBrakeCheck = ride_check_block_brakes(&trackElement, &problematicTrackElement);
auto blockBrakeCheck = RideCheckBlockBrakes(trackElement, &problematicTrackElement);
if (!blockBrakeCheck.Successful)
{
ride_scroll_to_track_error(&problematicTrackElement);
ride_scroll_to_track_error(problematicTrackElement);
return { false, blockBrakeCheck.Message };
}
}
@ -4078,7 +4084,7 @@ ResultWithMessage Ride::Open(bool isApplying)
{
if (ride_check_track_contains_inversions(&trackElement, &problematicTrackElement))
{
ride_scroll_to_track_error(&problematicTrackElement);
ride_scroll_to_track_error(problematicTrackElement);
return { false, STR_TRACK_UNSUITABLE_FOR_TYPE_OF_TRAIN };
}
}
@ -4086,7 +4092,7 @@ ResultWithMessage Ride::Open(bool isApplying)
{
if (ride_check_track_contains_banked(&trackElement, &problematicTrackElement))
{
ride_scroll_to_track_error(&problematicTrackElement);
ride_scroll_to_track_error(problematicTrackElement);
return { false, STR_TRACK_UNSUITABLE_FOR_TYPE_OF_TRAIN };
}
}
@ -4101,13 +4107,13 @@ ResultWithMessage Ride::Open(bool isApplying)
if (!ride_check_station_length(&trackElement, &problematicTrackElement))
{
ride_scroll_to_track_error(&problematicTrackElement);
ride_scroll_to_track_error(problematicTrackElement);
return { false, STR_STATION_NOT_LONG_ENOUGH };
}
if (!ride_check_start_and_end_is_station(&trackElement))
{
ride_scroll_to_track_error(&problematicTrackElement);
ride_scroll_to_track_error(problematicTrackElement);
return { false, STR_RIDE_MUST_START_AND_END_WITH_STATIONS };
}
}

View File

@ -141,7 +141,7 @@ void ride_construct(Ride* ride)
ride_find_track_gap(ride, &trackElement, &trackElement);
rct_window* w = window_get_main();
if (w != nullptr && ride_modify(&trackElement))
if (w != nullptr && ride_modify(trackElement))
window_scroll_to_location(*w, { trackElement, trackElement.element->GetBaseZ() });
}
else
@ -985,9 +985,9 @@ static bool ride_modify_maze(const CoordsXYE& tileElement)
*
* rct2: 0x006CC056
*/
bool ride_modify(CoordsXYE* input)
bool ride_modify(const CoordsXYE& input)
{
auto tileElement = *input;
auto tileElement = input;
if (tileElement.element == nullptr)
return false;

View File

@ -88,7 +88,7 @@ void ride_construction_set_default_next_piece();
void ride_select_next_section();
void ride_select_previous_section();
bool ride_modify(CoordsXYE* input);
bool ride_modify(const CoordsXYE& input);
CoordsXYZD ride_get_entrance_or_exit_position_from_screen_position(const ScreenCoordsXY& screenCoords);