From 7cc52f272d78203eb03170ee82a84b8c0ef6b449 Mon Sep 17 00:00:00 2001 From: ItsDrike Date: Sat, 7 Dec 2024 20:50:52 +0100 Subject: [PATCH] Add safe-guard against value underflow in column move check --- src/gamestate.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/gamestate.cpp b/src/gamestate.cpp index 9fe3e6e..999d758 100644 --- a/src/gamestate.cpp +++ b/src/gamestate.cpp @@ -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(static_cast(columnCard.value() - 1))) + const int reqValue = static_cast(columnCard.value() - 1); + if (reqValue < 0 || cardToMove.value() != static_cast(reqValue)) return false; // The card must be of opposite color