Fix #9256, 12e43c697d: invalid read after free. (#9258)

This also changes ScriptEventVehicleAutoReplaced when replacing wagons:
The event is now only spawned, if the head engine changes, so only if the VehicleID of the consist changes.
Previously replacing wagons spawned an event with OldVehicleID==NewVehicleID.

(cherry picked from commit 5bd8144853)
This commit is contained in:
frosch
2021-05-12 23:40:03 +02:00
committed by Jonathan G Rennison
parent dcb0cc2c52
commit c2fce47340

View File

@@ -631,6 +631,7 @@ static CommandCost ReplaceChain(Vehicle **chain, DoCommandFlag flags, bool wagon
/* Success ! */ /* Success ! */
if ((flags & DC_EXEC) != 0 && new_head != old_head) { if ((flags & DC_EXEC) != 0 && new_head != old_head) {
*chain = new_head; *chain = new_head;
AI::NewEvent(old_head->owner, new ScriptEventVehicleAutoReplaced(old_head->index, new_head->index));
} }
/* Transfer cargo of old vehicles and sell them */ /* Transfer cargo of old vehicles and sell them */
@@ -644,10 +645,7 @@ static CommandCost ReplaceChain(Vehicle **chain, DoCommandFlag flags, bool wagon
if ((flags & DC_EXEC) != 0) { if ((flags & DC_EXEC) != 0) {
old_vehs[i] = nullptr; old_vehs[i] = nullptr;
if (i == 0) { if (i == 0) old_head = nullptr;
AI::NewEvent(old_head->owner, new ScriptEventVehicleAutoReplaced(old_head->index, new_head->index));
old_head = nullptr;
}
} }
/* Sell the vehicle. /* Sell the vehicle.
* Note: This might temporarily construct new trains, so use DC_AUTOREPLACE to prevent * Note: This might temporarily construct new trains, so use DC_AUTOREPLACE to prevent