Prevent signed overflow of lookahead position

This commit is contained in:
Jonathan G Rennison
2021-02-02 22:02:44 +00:00
parent 351cf27719
commit a9b82b817b

View File

@@ -884,6 +884,20 @@ static void AdvanceLookAheadPosition(Train *v)
return;
}
if (unlikely(v->lookahead->current_position >= (1 << 30))) {
/* Prevent signed overflow by rebasing all position values */
const int32 old_position = v->lookahead->current_position;
v->lookahead->current_position = 0;
v->lookahead->reservation_end_position -= old_position;
for (TrainReservationLookAheadItem &item : v->lookahead->items) {
item.start -= old_position;
item.end -= old_position;
}
for (TrainReservationLookAheadCurve &curve : v->lookahead->curves) {
curve.position -= old_position;
}
}
while (!v->lookahead->items.empty() && v->lookahead->items.front().end < v->lookahead->current_position) {
if (v->lookahead->items.front().type == TRLIT_STATION) {
int trim_position = v->lookahead->current_position - 4;