A single missing validation check in the setTokenPrice() function can permanently brick an entire TokenSwap contract. Once exploited, every single user fund becomes trapped forever. No recovery. No fix.
The Vulnerability
The setTokenPrice() function contains only one requirement:
solidity
require(_tokenPrice != 0, "Price cannot be zero");That's it. No upper bound. No sanity check. Nothing preventing an owner (or anyone who compromises the owner) from setting the price to an astronomical value.
Consider what happens when someone sets:
tokenPrice = type(uint256).max / 2 + 1Now look at the buy() function:
uint256 currencyAmount = Math.ceilDiv(_tokenAmount * tokenPrice, 10 ** token.decimals());In Solidity 0.8.x, multiplication includes built-in overflow checks. When _tokenAmount * tokenPrice exceeds type(uint256).max, the transaction reverts. But here's the nightmare: this isn't a temporary problem.
Why It's Permanent
Once a dangerously high price is set, the contract enters a death spiral
Owner sets poison price | Succeeds |
User tries to buy any amount | Reverts (overflow) |
User tries to sell any amount | Reverts (overflow) |
Owner tries to fix price | Succeeds |
User tries to buy again | Still reverts |
even after the owner resets the price to a normal value, transactions still revert? Yes. Because the contract's internal accounting becomes corrupted. The overflow doesn't just block one transaction, it breaks the mathematical assumptions the contract relies on. Once the price goes past a certain threshold, the damage is irreversible.
Owner sets an extremely high price (no upper bound check). Buy/sell functions multiply tokenAmount * tokenPrice , Overflow causes revert, every transaction fails.









Comments (2)