FV-SOL-9-C2 Unrestricted Mapping
TLDR
Solidity mappings do not provide a way to iterate over keys natively. However, if developers use an array to track mapping keys for iteration, looping through the keys can lead to unbounded loops if the array grows indefinitely
Game
If this auxiliary array grows without restriction, what will looping over it do?
// SPDX-License-Identifier: MIT
// Open me in VSCode and really think before opening the hints!
// Add @audit tags wherever suspicious
// Go to the solidity docs to complete missing knowledge of what's happening here
// Solve by drafting a fix!
pragma solidity ^0.8.0;
contract UnrestrictedMappingGame {
mapping(address => uint256) public balances;
address[] public users;
// Add or update a user's balance
function setBalance(address user, uint256 amount) public {
if (balances[user] == 0) {
users.push(user);
}
balances[user] = amount;
}
// Sum up all balances
function totalBalances() public view returns (uint256) {
uint256 total = 0;
for (uint256 i = 0; i < users.length; i++) {
total += balances[users[i]];
}
return total;
}
}
Last updated
Was this helpful?