Beginner FriendlyFoundryNFT
100 EXP
View results
Submission Details
Severity: low
Valid

Flaw in `getActivePlayerIndex` Function

Vulnerability Details

The getActivePlayerIndex function has a flaw in its implementation, leading to potential misinformation and mismanagement of player data. The function is intended to return the index of a player in the players array. However, if a player is not active or not found in the array, it returns 0.

function getActivePlayerIndex(address player) external view returns (uint256) {
for (uint256 i = 0; i < players.length; i++) {
if (players[i] == player) {
return i;
}
}
return 0;
}

Impact

Since arrays in Solidity are zero-indexed, the function returns 0 both when the player is the first in the array and when the player is not found. This makes it impossible to distinguish between a player located at index 0 and a non-existent player. This ambiguity can lead to confusion and errors in interactions with the contract, especially in contexts where determining the exact position of a player is crucial.

Recommendations

  • Distinct Return Value for Non-Existent Players: Modify the function to return a distinct value (such as a special constant or the length of the array) when a player is not found. This value should be clearly documented and must be different from any valid index.

  • Documentation and Error Handling: Update the documentation to clarify the return values and their meanings. Additionally, consider implementing error handling or revert messages to inform users when a player is not found.

  • Use of Mappings for Efficiency: Consider using a mapping to store player data, which can provide a more efficient and reliable way to track player states and indices, especially if the players array becomes large.

Updates

Lead Judging Commences

Hamiltonite Lead Judge about 2 years ago
Submission Judgement Published
Validated
Assigned finding tags:

getActivePlayerIndex can say a player is both entered at slot 0 and inactive

Support

FAQs

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

Give us feedback!