Skip to content

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

Open
@c-kado

Description

@c-kado

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions