diff --git a/slither/detectors/statements/boolean_constant_equality.py b/slither/detectors/statements/boolean_constant_equality.py index a8d5e6e0a..17df74e98 100644 --- a/slither/detectors/statements/boolean_constant_equality.py +++ b/slither/detectors/statements/boolean_constant_equality.py @@ -12,7 +12,7 @@ from slither.slithir.variables import Constant class BooleanEquality(AbstractDetector): """ - Boolean constant misuse + Boolean constant equality """ ARGUMENT = "boolean-equal" @@ -72,17 +72,16 @@ Boolean constants can be used directly and do not need to be compare to `true` o results = [] for contract in self.contracts: boolean_constant_misuses = self._detect_boolean_equality(contract) - if boolean_constant_misuses: - for (func, nodes) in boolean_constant_misuses: - for node in nodes: - info = [ - func, - " compares to a boolean constant:\n\t-", - node, - "\n", - ] + for (func, nodes) in boolean_constant_misuses: + for node in nodes: + info = [ + func, + " compares to a boolean constant:\n\t-", + node, + "\n", + ] - res = self.generate_result(info) - results.append(res) + res = self.generate_result(info) + results.append(res) return results diff --git a/tests/detectors/boolean-constant-equality/boolean-constant-equality.sol b/tests/detectors/boolean-constant-equality/boolean-constant-equality.sol new file mode 100644 index 000000000..799f43335 --- /dev/null +++ b/tests/detectors/boolean-constant-equality/boolean-constant-equality.sol @@ -0,0 +1,27 @@ +contract MyConc { + function bad0(bool foo) public pure returns (bool) { + if (foo) { + return true; + } + } + function bad1(bool b) public pure returns (bool) { + return (b == true); + } + + function bad2(bool x, uint8 y) public pure returns (bool) { + if (x == (y > 0)) { + return true; + } + } + + function bad3() public pure returns (bool) { + uint256 a; + if (a == 10) { + return true; + } + } + + function good(uint8 a) public pure returns (bool) { + return a >= 1; + } +} \ No newline at end of file diff --git a/tests/detectors/boolean-constant-equality/boolean-constant-equality.sol.0.4.25.BooleanEquality.json b/tests/detectors/boolean-constant-equality/boolean-constant-equality.sol.0.4.25.BooleanEquality.json new file mode 100644 index 000000000..72c1bcbbe --- /dev/null +++ b/tests/detectors/boolean-constant-equality/boolean-constant-equality.sol.0.4.25.BooleanEquality.json @@ -0,0 +1,170 @@ +[ + [ + { + "elements": [ + { + "type": "function", + "name": "bad1", + "source_mapping": { + "start": 139, + "length": 84, + "filename_used": "/GENERIC_PATH", + "filename_relative": "tests/detectors/boolean-constant-equality/boolean-constant-equality.sol", + "filename_absolute": "/GENERIC_PATH", + "filename_short": "tests/detectors/boolean-constant-equality/boolean-constant-equality.sol", + "is_dependency": false, + "lines": [ + 7, + 8, + 9 + ], + "starting_column": 5, + "ending_column": 6 + }, + "type_specific_fields": { + "parent": { + "type": "contract", + "name": "MyConc", + "source_mapping": { + "start": 0, + "length": 578, + "filename_used": "/GENERIC_PATH", + "filename_relative": "tests/detectors/boolean-constant-equality/boolean-constant-equality.sol", + "filename_absolute": "/GENERIC_PATH", + "filename_short": "tests/detectors/boolean-constant-equality/boolean-constant-equality.sol", + "is_dependency": false, + "lines": [ + 1, + 2, + 3, + 4, + 5, + 6, + 7, + 8, + 9, + 10, + 11, + 12, + 13, + 14, + 15, + 16, + 17, + 18, + 19, + 20, + 21, + 22, + 23, + 24, + 25, + 26, + 27, + 28 + ], + "starting_column": 1, + "ending_column": 0 + } + }, + "signature": "bad1(bool)" + } + }, + { + "type": "node", + "name": "(b == true)", + "source_mapping": { + "start": 198, + "length": 18, + "filename_used": "/GENERIC_PATH", + "filename_relative": "tests/detectors/boolean-constant-equality/boolean-constant-equality.sol", + "filename_absolute": "/GENERIC_PATH", + "filename_short": "tests/detectors/boolean-constant-equality/boolean-constant-equality.sol", + "is_dependency": false, + "lines": [ + 8 + ], + "starting_column": 9, + "ending_column": 27 + }, + "type_specific_fields": { + "parent": { + "type": "function", + "name": "bad1", + "source_mapping": { + "start": 139, + "length": 84, + "filename_used": "/GENERIC_PATH", + "filename_relative": "tests/detectors/boolean-constant-equality/boolean-constant-equality.sol", + "filename_absolute": "/GENERIC_PATH", + "filename_short": "tests/detectors/boolean-constant-equality/boolean-constant-equality.sol", + "is_dependency": false, + "lines": [ + 7, + 8, + 9 + ], + "starting_column": 5, + "ending_column": 6 + }, + "type_specific_fields": { + "parent": { + "type": "contract", + "name": "MyConc", + "source_mapping": { + "start": 0, + "length": 578, + "filename_used": "/GENERIC_PATH", + "filename_relative": "tests/detectors/boolean-constant-equality/boolean-constant-equality.sol", + "filename_absolute": "/GENERIC_PATH", + "filename_short": "tests/detectors/boolean-constant-equality/boolean-constant-equality.sol", + "is_dependency": false, + "lines": [ + 1, + 2, + 3, + 4, + 5, + 6, + 7, + 8, + 9, + 10, + 11, + 12, + 13, + 14, + 15, + 16, + 17, + 18, + 19, + 20, + 21, + 22, + 23, + 24, + 25, + 26, + 27, + 28 + ], + "starting_column": 1, + "ending_column": 0 + } + }, + "signature": "bad1(bool)" + } + } + } + } + ], + "description": "MyConc.bad1(bool) (tests/detectors/boolean-constant-equality/boolean-constant-equality.sol#7-9) compares to a boolean constant:\n\t-(b == true) (tests/detectors/boolean-constant-equality/boolean-constant-equality.sol#8)\n", + "markdown": "[MyConc.bad1(bool)](tests/detectors/boolean-constant-equality/boolean-constant-equality.sol#L7-L9) compares to a boolean constant:\n\t-[(b == true)](tests/detectors/boolean-constant-equality/boolean-constant-equality.sol#L8)\n", + "id": "4a53e773c88b730f07c2e4106545df03b44679c56ee0d9dbd75dca010320e69c", + "check": "boolean-equal", + "impact": "Informational", + "confidence": "High" + } + ] +] \ No newline at end of file diff --git a/tests/test_detectors.py b/tests/test_detectors.py index bc1780c3f..517394791 100644 --- a/tests/test_detectors.py +++ b/tests/test_detectors.py @@ -37,6 +37,7 @@ from slither.detectors.shadowing.local import LocalShadowing from slither.detectors.shadowing.state import StateShadowing from slither.detectors.source.rtlo import RightToLeftOverride from slither.detectors.statements.assembly import Assembly +from slither.detectors.statements.boolean_constant_equality import BooleanEquality from slither.detectors.statements.calls_in_loop import MultipleCallsInLoop from slither.detectors.statements.controlled_delegatecall import ControlledDelegateCall from slither.detectors.statements.incorrect_strict_equality import IncorrectStrictEquality @@ -91,6 +92,11 @@ def id_test(test_item: Test): ALL_TESTS = [ + Test( + BooleanEquality, + "tests/detectors/boolean-constant-equality/boolean-constant-equality.sol", + "0.4.25", + ), Test(UncheckedLowLevel, "tests/detectors/unchecked-lowlevel/unchecked_lowlevel.sol", "0.4.25"), Test( UncheckedLowLevel,