crytic / crytic/slither

Slither detects reentrancy vulnerability against the code using a mutex to guard reentrancy.

Open
#2,618 0 comments 0 reactions 0 assignees View on GitHub
enhancement
Dominant language
Python
Stars
6.4k
Forks
1.1k
PR merge metrics
No merged PRs in 30d

Description

### Describe the desired feature

Slither detects reentrancy in the code below.
I think the code is safe for reentrancy.
`bool locked` is a variable for mutex.
Slither detects vulnerabilities by recognizing the variable as a state variable written after an external call.

```
pragma solidity ^0.7.0;

contract MutexPattern {
bool locked = false;
mapping(address => uint256) public balances;

function withdraw(uint _amount) public payable returns(bool) {
require(!locked, "Blocked from reentrancy.");
locked = true;

require(balances[msg.sender] >= _amount, "No balance to withdraw.");

balances[msg.sender] -= _amount;
(bool success, ) = msg.sender.call{value: _amount}("");
require(success);

locked = false;
return true;
}
}
```

Slither detects the code below using a modifier as safe, although the functionality is the same as the above code.

```
pragma solidity ^0.7.0;

contract MutexPattern {
bool locked = false;
mapping(address => uint256) public balances;

modifier noReentrancy() {
require(!locked, "Blocked from reentrancy.");
locked = true;
_;
locked = false;
}

function withdraw(uint _amount) public payable noReentrancy returns(bool) {
require(balances[msg.sender] >= _amount, "No balance to withdraw.");

balances[msg.sender] -= _amount;
(bool success, ) = msg.sender.call{value: _amount}("");
require(success);

return true;
}
}
```

What differences are in them?

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.