Fix incorrect-equality false positives for boundary constants#2952
Open
ep0chzer0 wants to merge 2 commits intocrytic:masterfrom
Open
Fix incorrect-equality false positives for boundary constants#2952ep0chzer0 wants to merge 2 commits intocrytic:masterfrom
ep0chzer0 wants to merge 2 commits intocrytic:masterfrom
Conversation
1024666 to
2421551
Compare
Comparisons against boundary values like 0 and type(T).max should not be flagged as dangerous strict equalities because these values cannot be manipulated by an attacker: - balance == 0: cannot reach 0 by adding value - balance == type(uint256).max: cannot reach max by normal operations - block.timestamp == 0: safe initialization check Added is_comparing_against_safe_constant() method that excludes: - Literal 0 - type(T).max values for all integer types Fixes crytic#2759
Remove TestSolidityKeyword findings that compare against 0, which are now correctly excluded by the boundary constant fix.
2421551 to
f85c0fd
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Root Cause
The detector flags all strict equality comparisons involving tainted values (balances, timestamps, etc.) without considering whether the comparison target is a safe boundary value.
Fix
Added
is_comparing_against_safe_constant()method that excludes:0- cannot reach zero by adding valuetype(T).maxvalues - cannot reach max through normal operationsTest Coverage
Updated test file with new cases:
good4/good5()- comparingblock.timestamp/number == 0(NOT flagged)TestSafeConstants.good0/1/2()- comparing balance against 0 and type(uint256).max (NOT flagged)bad0/bad1()- comparing against arbitrary values 1, 100 (STILL flagged)Impact
balance == 0andallowance == type(uint256).maxbalance == 100 etherFixes #2759