crytic / crytic/slither

False positive: costly-loop detector when evaluating an if statement with assignment and return

Open
#1,338 6 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 issue:

A costly-loop warning triggers when evaluating a loop that contains an if statement that includes both an assignment to a state variable followed by a return. This shouldn't be flagged as costly because it is the most efficient way of assigning this value since it only triggers once before exiting the loop.

### Code example to reproduce the issue:

```solidity
uint 256 storedIndex = 0;
arrayOfValues = [1640995200, 1672531200, 1704067200];

function currYear() public {
for (uint256 i = 0; i < arrayOfValues.length; i++) {
if (arrayOfValues[i] > block.timestamp) {
storedIndex = i;
return;
}
}
}
```

### Version:

0.8.3

### Relevant log output:

```shell
currYear() has costly operations inside a loop:
- storedIndex = i
Reference: https://github.com/crytic/slither/wiki/Detector-Documentation#costly-operations-inside-a-loop
```

Contributor guide

Open the contributing guide

Research direction

Start by locating the costly-loop detector and reproduce the warning with the Solidity example in the issue. Trace how an assignment followed by return inside the loop is classified; done means this one-time assignment is no longer reported as a costly loop operation while other costly cases remain covered.

Written by the indexing model from the issue text.

Assessment

Tech stack
python, solidity
Domain
devtools, security
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.