diff --git a/slither/detectors/variables/uninitialized_local_variables.py b/slither/detectors/variables/uninitialized_local_variables.py index 759691d504..3f7eee4d6d 100644 --- a/slither/detectors/variables/uninitialized_local_variables.py +++ b/slither/detectors/variables/uninitialized_local_variables.py @@ -6,7 +6,7 @@ """ from typing import List -from slither.core.cfg.node import Node +from slither.core.cfg.node import Node, NodeType from slither.core.declarations.function_contract import FunctionContract from slither.detectors.abstract_detector import AbstractDetector, DetectorClassification from slither.utils.output import Output @@ -64,6 +64,14 @@ def _detect_uninitialized( self.visited_all_paths[node] = list(set(self.visited_all_paths[node] + fathers_context)) + # Remove a local variable declared in a for loop header + if ( + node.type == NodeType.VARIABLE + and len(node.sons) == 1 # Should always be true for a node that has a STARTLOOP son + and node.sons[0].type == NodeType.STARTLOOP + ): + fathers_context.remove(node.variable_declaration) + if self.key in node.context: fathers_context += node.context[self.key] diff --git a/tests/e2e/detectors/test_data/uninitialized-local/0.4.25/uninitialized_local_variable.sol b/tests/e2e/detectors/test_data/uninitialized-local/0.4.25/uninitialized_local_variable.sol index d28eef9578..82a3ab1246 100644 --- a/tests/e2e/detectors/test_data/uninitialized-local/0.4.25/uninitialized_local_variable.sol +++ b/tests/e2e/detectors/test_data/uninitialized-local/0.4.25/uninitialized_local_variable.sol @@ -6,4 +6,10 @@ contract Uninitialized{ return uint_not_init + uint_init; } + function noreportfor() public { + for(uint i; i < 6; i++) { + uint a = i; + } + } + } diff --git a/tests/e2e/detectors/test_data/uninitialized-local/0.4.25/uninitialized_local_variable.sol-0.4.25.zip b/tests/e2e/detectors/test_data/uninitialized-local/0.4.25/uninitialized_local_variable.sol-0.4.25.zip index 60ae4138c0..2c4ccdf173 100644 Binary files a/tests/e2e/detectors/test_data/uninitialized-local/0.4.25/uninitialized_local_variable.sol-0.4.25.zip and b/tests/e2e/detectors/test_data/uninitialized-local/0.4.25/uninitialized_local_variable.sol-0.4.25.zip differ diff --git a/tests/e2e/detectors/test_data/uninitialized-local/0.5.16/uninitialized_local_variable.sol b/tests/e2e/detectors/test_data/uninitialized-local/0.5.16/uninitialized_local_variable.sol index d28eef9578..82a3ab1246 100644 --- a/tests/e2e/detectors/test_data/uninitialized-local/0.5.16/uninitialized_local_variable.sol +++ b/tests/e2e/detectors/test_data/uninitialized-local/0.5.16/uninitialized_local_variable.sol @@ -6,4 +6,10 @@ contract Uninitialized{ return uint_not_init + uint_init; } + function noreportfor() public { + for(uint i; i < 6; i++) { + uint a = i; + } + } + } diff --git a/tests/e2e/detectors/test_data/uninitialized-local/0.5.16/uninitialized_local_variable.sol-0.5.16.zip b/tests/e2e/detectors/test_data/uninitialized-local/0.5.16/uninitialized_local_variable.sol-0.5.16.zip index c365c989db..b29fe8a42a 100644 Binary files a/tests/e2e/detectors/test_data/uninitialized-local/0.5.16/uninitialized_local_variable.sol-0.5.16.zip and b/tests/e2e/detectors/test_data/uninitialized-local/0.5.16/uninitialized_local_variable.sol-0.5.16.zip differ diff --git a/tests/e2e/detectors/test_data/uninitialized-local/0.6.11/uninitialized_local_variable.sol b/tests/e2e/detectors/test_data/uninitialized-local/0.6.11/uninitialized_local_variable.sol index d28eef9578..82a3ab1246 100644 --- a/tests/e2e/detectors/test_data/uninitialized-local/0.6.11/uninitialized_local_variable.sol +++ b/tests/e2e/detectors/test_data/uninitialized-local/0.6.11/uninitialized_local_variable.sol @@ -6,4 +6,10 @@ contract Uninitialized{ return uint_not_init + uint_init; } + function noreportfor() public { + for(uint i; i < 6; i++) { + uint a = i; + } + } + } diff --git a/tests/e2e/detectors/test_data/uninitialized-local/0.6.11/uninitialized_local_variable.sol-0.6.11.zip b/tests/e2e/detectors/test_data/uninitialized-local/0.6.11/uninitialized_local_variable.sol-0.6.11.zip index ca7ea0eb30..793b30f4a1 100644 Binary files a/tests/e2e/detectors/test_data/uninitialized-local/0.6.11/uninitialized_local_variable.sol-0.6.11.zip and b/tests/e2e/detectors/test_data/uninitialized-local/0.6.11/uninitialized_local_variable.sol-0.6.11.zip differ diff --git a/tests/e2e/detectors/test_data/uninitialized-local/0.7.6/uninitialized_local_variable.sol b/tests/e2e/detectors/test_data/uninitialized-local/0.7.6/uninitialized_local_variable.sol index d28eef9578..82a3ab1246 100644 --- a/tests/e2e/detectors/test_data/uninitialized-local/0.7.6/uninitialized_local_variable.sol +++ b/tests/e2e/detectors/test_data/uninitialized-local/0.7.6/uninitialized_local_variable.sol @@ -6,4 +6,10 @@ contract Uninitialized{ return uint_not_init + uint_init; } + function noreportfor() public { + for(uint i; i < 6; i++) { + uint a = i; + } + } + } diff --git a/tests/e2e/detectors/test_data/uninitialized-local/0.7.6/uninitialized_local_variable.sol-0.7.6.zip b/tests/e2e/detectors/test_data/uninitialized-local/0.7.6/uninitialized_local_variable.sol-0.7.6.zip index 3049b95bf3..6aa5401d17 100644 Binary files a/tests/e2e/detectors/test_data/uninitialized-local/0.7.6/uninitialized_local_variable.sol-0.7.6.zip and b/tests/e2e/detectors/test_data/uninitialized-local/0.7.6/uninitialized_local_variable.sol-0.7.6.zip differ