do not edit the zone currently iterated on (#4345)

this can cause the iterator to become invalidated which will crash but
because of the data not always being moved it will often still work as
intended, giving the idea that it is random
This commit is contained in:
ebbit1q 2021-05-10 19:21:12 +02:00 committed by GitHub
parent 046a3649ed
commit ae7437750b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -577,19 +577,17 @@ void Server_Game::unattachCards(GameEventStorage &ges, Server_Player *player)
for (auto zone : player->getZones()) { for (auto zone : player->getZones()) {
for (auto card : zone->getCards()) { for (auto card : zone->getCards()) {
if (card == nullptr) {
continue;
}
const auto &attachedCardsBase = card->getAttachedCards();
if (attachedCardsBase.isEmpty()) {
continue;
}
// Make a copy of the list because the original one gets modified during the loop // Make a copy of the list because the original one gets modified during the loop
QList<Server_Card *> attachedCards = {attachedCardsBase}; QList<Server_Card *> attachedCards = card->getAttachedCards();
for (Server_Card *attachedCard : attachedCards) { for (Server_Card *attachedCard : attachedCards) {
attachedCard->getZone()->getPlayer()->unattachCard(ges, attachedCard); auto otherPlayer = attachedCard->getZone()->getPlayer();
// do not modify the current player's zone!
// this would cause the current card iterator to be invalidated!
// we only have to return cards owned by other players
// because the current player is leaving the game anyway
if (otherPlayer != player) {
otherPlayer->unattachCard(ges, attachedCard);
}
} }
} }
} }