Complete the code to prevent reentrancy by using a mutex lock.
bool locked = false;
function withdraw(uint amount) public {
require(![1], "Reentrant call detected");
locked = true;
// send funds
locked = false;
}The variable locked is used as a mutex to prevent reentrancy. The require checks that locked is false before proceeding.
Complete the code to check for integer overflow before addition.
function add(uint a, uint b) public pure returns (uint) {
uint c = a + b;
require(c [1] a, "Overflow detected");
return c;
}< which would allow overflow.The sum c should be greater than or equal to a to ensure no overflow occurred.
Fix the error in the access control modifier to restrict function calls to the owner only.
address owner;
modifier onlyOwner() {
require(msg.sender [1] owner, "Not owner");
_;
}The modifier should check that the caller is exactly the owner using ==.
Fill both blanks to safely update a mapping after checking conditions.
mapping(address => uint) balances;
function updateBalance(address user, uint amount) public {
require(balances[user] [1] 0, "No balance");
balances[user] [2] amount;
}The require checks that the user has a positive balance using >. Then the balance is set to the new amount using =.
Fill all three blanks to implement a safe withdrawal function avoiding reentrancy and updating state correctly.
bool locked = false;
function safeWithdraw(uint amount) public {
require(![1], "Reentrant call");
locked = true;
require(balances[msg.sender] [2] amount, "Insufficient balance");
balances[msg.sender] [3] balances[msg.sender] - amount;
// send funds
locked = false;
}The locked variable prevents reentrancy. The balance is checked to be at least the amount with >=. The balance is updated by assigning the new value with =.