Add safe-guard against value underflow in column move check

This commit is contained in:
ItsDrike 2024-12-07 20:50:52 +01:00
parent bea2be5f63
commit 7cc52f272d
Signed by: ItsDrike
GPG key ID: FA2745890B7048C0

View file

@ -452,7 +452,7 @@ QString GameState::generateStateHash() const {
stateHash += ",";
}
if (stateHash.last(1) != "|")
if (stateHash.length() > 0 && stateHash.last(1) != "|")
stateHash.removeLast();
stateHash += "|";
}
@ -464,7 +464,7 @@ QString GameState::generateStateHash() const {
for (const PlayingCard* card : foundationPile) {
stateHash += QString::number(card->value()) + "." + QString::number(card->suit()) + ",";
}
if (stateHash.last(1) != "|")
if (stateHash.length() > 0 && stateHash.last(1) != "|")
stateHash.removeLast();
stateHash += "|";
}
@ -588,7 +588,8 @@ bool GameState::isColumnMoveValid(const PlayingCard& cardToMove, int columnId) c
const PlayingCard& columnCard = *columnStack.last()->card();
// The card's value must be one less than the card in the column
if (cardToMove.value() != static_cast<PlayingCard::Value>(static_cast<int>(columnCard.value() - 1)))
const int reqValue = static_cast<int>(columnCard.value() - 1);
if (reqValue < 0 || cardToMove.value() != static_cast<PlayingCard::Value>(reqValue))
return false;
// The card must be of opposite color