TwentyOne

First Flight #29
Beginner FriendlyGameFiFoundrySolidity
100 EXP
View results
Submission Details
Severity: medium
Valid

Lack of handling for draw scenarios.

Description:

https://github.com/Cyfrin/2024-11-TwentyOne/blob/a4429168302722d14a5e5996d25d6fc5be22a899/src/TwentyOne.sol#L120

In the call function, there is no explicit handling for draw(push) scenarios where the player's hand total equals the dealer's hand total. This leads to ambiguity in the game's outcome and may result in the contract failing to return the player's bet or incorrectly determining a winner. Draws are a standard outcome in blackjack games and should be handled to ensure fairness and adherence to game rules.

Impact:

Without proper handling, players may lose their bets unfairly or fail to recover their wager when a draw occurs.

Proof of Concept:

  1. Player 1 starts the game by betting 1 ether and receives a hand total of 18.

  2. The dealer also ends up with a hand total of 18.

  3. The endgame function currently lacks logic to detect and handle this draw scenario.

  4. The contract may either revert or proceed incorrectly without returning the player's bet.

Tools Used:

Manual Review.

Recommended Mitigation:

Introduce a conditional check in the endgame function to handle draw scenarios explicitly. For example:

function call() public {
// REST OF CODE....
if (dealerHand > 21) {
emit PlayerWonTheGame("Dealer went bust, players winning hand: ", playerHand);
endGame(msg.sender, true);
} else if (playerHand > dealerHand) {
emit PlayerWonTheGame("Dealer's hand is lower, players winning hand: ", playerHand);
endGame(msg.sender, true);
}
+ else if (playerHand == dealerHand) {
// Handle push: refund the player's bet
+ payable(player).transfer(playerBet);
+ emit GameDraw(player, playerHandTotal);
+ endGame(msg.sender, false);
}
else {
emit PlayerLostTheGame("Dealer's hand is higher, dealers winning hand: ", dealerHand);
endGame(msg.sender, false);
}
}
Updates

Lead Judging Commences

inallhonesty Lead Judge 11 months ago
Submission Judgement Published
Validated
Assigned finding tags:

Tie case

Support

FAQs

Can't find an answer? Chat with us on Discord, Twitter or Linkedin.