From ae7437750b081fdd8413420f89c140c76579c1e0 Mon Sep 17 00:00:00 2001 From: ebbit1q Date: Mon, 10 May 2021 19:21:12 +0200 Subject: [PATCH] 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 --- common/server_game.cpp | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/common/server_game.cpp b/common/server_game.cpp index db756264..46c67f8e 100644 --- a/common/server_game.cpp +++ b/common/server_game.cpp @@ -577,19 +577,17 @@ void Server_Game::unattachCards(GameEventStorage &ges, Server_Player *player) for (auto zone : player->getZones()) { 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 - QList attachedCards = {attachedCardsBase}; + QList attachedCards = card->getAttachedCards(); 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); + } } } }