Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix issue-887, FP reentrancy in constructor #1048

Merged
merged 1 commit into from
Feb 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions slither/detectors/reentrancy/reentrancy.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,11 +283,12 @@ def _explore(self, node, visited, skip_father=None):

def detect_reentrancy(self, contract):
for function in contract.functions_and_modifiers_declared:
if function.is_implemented:
if self.KEY in function.context:
continue
self._explore(function.entry_point, [])
function.context[self.KEY] = True
if not function.is_constructor:
if function.is_implemented:
if self.KEY in function.context:
continue
self._explore(function.entry_point, [])
function.context[self.KEY] = True

def _detect(self):
""""""
Expand Down

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@ contract ReentrancyBenign {
uint8 anotherVariableToChange;
uint8 counter = 0;

// Should not detect reentrancy in constructor
constructor(address addr) {
(bool success) = addr.call();
if (!success) {
revert();
}
counter += 1;
}

function bad0() public {
if (!(msg.sender.call())) {
revert();
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@ contract ReentrancyBenign {
uint8 anotherVariableToChange;
uint8 counter = 0;

// Should not detect reentrancy in constructor
constructor(address addr) public {
(bool success,) = addr.call("");
if (!success) {
revert();
}
counter += 1;
}

function bad0() public {
(bool success,) = msg.sender.call("");
if (!success) {
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@ contract ReentrancyBenign {
uint8 anotherVariableToChange;
uint8 counter = 0;

// Should not detect reentrancy in constructor
constructor(address addr) public {
(bool success,) = addr.call("");
if (!success) {
revert();
}
counter += 1;
}

function bad0() public {
(bool success,) = msg.sender.call("");
if (!success) {
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@ contract ReentrancyBenign {
uint8 anotherVariableToChange;
uint8 counter = 0;

// Should not detect reentrancy in constructor
constructor(address addr) {
(bool success,) = addr.call("");
if (!success) {
revert();
}
counter += 1;
}

function bad0() public {
(bool success,) = msg.sender.call("");
if (!success) {
Expand Down

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions tests/detectors/reentrancy-eth/0.4.25/reentrancy.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@ contract Reentrancy {
userBalance[msg.sender] += msg.value;
}

// Should not detect reentrancy in constructor
constructor() public {
// send userBalance[msg.sender] ethers to msg.sender
// if mgs.sender is a contract, it will call its fallback function
if (!(msg.sender.call.value(userBalance[msg.sender])())) {
revert();
}
userBalance[msg.sender] = 0;
}

function withdrawBalance() public{
// send userBalance[msg.sender] ethers to msg.sender
// if mgs.sender is a contract, it will call its fallback function
Expand Down
Loading