MyCut

AI First Flight #8
Beginner FriendlyFoundry
EXP
View results
Submission Details
Severity: high
Valid

`Pot::constructor` Overwrites Rewards for Duplicate Players, Leading to Incorrect Distribution

Description

The for loop in Pot::constructor overwrites playersToRewards[i_players[i]] using = assignment. If a player's address appears multiple times in i_players[], the reward is overwritten rather than accumulated.

// src/Pot.sol:32-34
for (uint256 i = 0; i < i_players.length; i++) {
playersToRewards[i_players[i]] = i_rewards[i]; //@> = overwrites!
}

Concrete Example:

  • i_players = [0x123, 0x456, 0x123]

  • i_rewards = [100, 200, 300]

  • Expected reward for 0x123: 100 + 300 = 400

  • Actual reward for 0x123: 300 (100 is overwritten)

  • Player loses 100 tokens

Risk

Likelihood:

  • Whenever the same address appears in i_players[] more than once

  • This occurs when a contest has duplicate player entries (intentional bonus or admin error)

Impact:

  • Players with duplicate entries receive only the last occurrence's reward

  • Earlier rewards are silently discarded with no event or error

  • Compromises the integrity of reward distribution

Proof of Concept

function test_ConstructorOverwritesDuplicatePlayerReward() public mintAndApproveTokens {
address[] memory sixPlayers = [player1, player2, player3, player4, player1, player5];
uint256[] memory rewardForSix = [2, 3, 4, 5, 6, 7];
uint256 total = 27; // 2+3+4+5+6+7
vm.startPrank(user);
contest = ContestManager(conMan).createContest(sixPlayers, rewardForSix, IERC20(ERC20Mock(weth)), total);
ContestManager(conMan).fundContest(0);
vm.stopPrank();
// player1 appears at index 0 (reward=2) and index 4 (reward=6) → should get 8
uint256 expectedPlayer1 = rewardForSix[0] + rewardForSix[4]; // 8
uint256 assignedPlayer1 = Pot(contest).checkCut(player1);
console.log("Expected:", expectedPlayer1); // 8
console.log("Assigned:", assignedPlayer1); // 6 (last occurrence only)
assert(assignedPlayer1 < expectedPlayer1); // 6 < 8
}

Run with:

forge test --match-test test_ConstructorOverwritesDuplicatePlayerReward -vv

Result: [PASS] — player1 receives 6 instead of expected 8.

Recommended Mitigation

for (uint256 i = 0; i < i_players.length; i++) {
- playersToRewards[i_players[i]] = i_rewards[i];
+ playersToRewards[i_players[i]] += i_rewards[i];
}
Updates

Lead Judging Commences

ai-first-flight-judge Lead Judge about 5 hours ago
Submission Judgement Published
Validated
Assigned finding tags:

[H-03] [M1] `Pot::constructor` Overwrites Rewards for Duplicate Players, Leading to Incorrect Distribution

## Description The `for` loop inside the `Pot::constructor` override the `playersToRewards[i_players[i]]` with new reward `i_rewards[i]`.So if a player's address appears multiple times, the reward is overwritten rather than accumulated. This results in the player receiving only the reward from the last occurrence of their address in the array, ignoring prior rewards. ## Vulnerability Details **Proof of Concept:** 1. Suppose i_players contains \[0x123, 0x456, 0x123] and i_rewards contains \[100, 200, 300]. 2. The playersToRewards mapping will be updated as follows during construction: - For address 0x123 at index 0, reward is set to 300. - For address 0x456 at index 1, reward is set to 200. - For address 0x123 at index 2, reward is updated to 100. 3. As a result, the final reward for address 0x123 in playersToRewards will be 100, not 400 (300+100).This leads to incorrect and lower reward distributions. **Proof of Code (PoC):** place the following in the `TestMyCut.t.sol::TestMyCut` ```Solidity address player3 = makeAddr("player3"); address player4 = makeAddr("player4"); address player5 = makeAddr("player5"); address[] sixPlayersWithDuplicateOneAddress = [player1, player2, player3, player4, player1, player5]; uint256[] rewardForSixPlayers = [2, 3, 4, 5, 6, 7]; uint256 totalRewardForSixPlayers = 27; // 2+3+4+5+6+7 function test_ConstructorFailsInCorrectlyAssigningReward() public mintAndApproveTokens { for (uint256 i = 0; i < sixPlayersWithDuplicateOneAddress.length; i++) { console.log("Player: %s reward: %d", sixPlayersWithDuplicateOneAddress[i], rewardForSixPlayers[i]); } /** * player1 has two occurance in sixPlayersWithDuplicateOneAddress ( at index 0 and 4) * So it's expected reward should be 2+6 = 8 */ vm.startPrank(user); contest = ContestManager(conMan).createContest(sixPlayersWithDuplicateOneAddress, rewardForSixPlayers, IERC20(ERC20Mock(weth)), totalRewardForSixPlayers); ContestManager(conMan).fundContest(0); vm.stopPrank(); uint256 expectedRewardForPlayer1 = rewardForSixPlayers[0] + rewardForSixPlayers[4]; uint256 assignedRewardForPlaye1 = Pot(contest).checkCut(player1); console.log("Expected Reward For Player1: %d", expectedRewardForPlayer1); console.log("Assigned Reward For Player1: %d", assignedRewardForPlaye1); assert(assignedRewardForPlaye1 < expectedRewardForPlayer1); } ``` ## Impact The overall integrity of the reward distribution process is compromised. Players with multiple entries in the i_players\[] array will only receive the reward from their last occurrence in the array, leading to incorrect and lower reward distributions. ## Recommendations **Recommended Mitigation:** Aggregate the rewards for each player inside the constructor to ensure duplicate addresses accumulate rewards instead of overwriting them.This can be achieved by using the += operator in the loop that assigns rewards to players. ```diff for (uint256 i = 0; i < i_players.length; i++) { - playersToRewards[i_players[i]] = i_rewards[i]; + playersToRewards[i_players[i]] += i_rewards[i]; } ```

Support

FAQs

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

Give us feedback!