-
Notifications
You must be signed in to change notification settings - Fork 981
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ignore private
__gap
variables in shadowing detectors (#1117)
* Don't consider shadowing of private __gap variables in state and abstract shadowing detectors
- Loading branch information
1 parent
dc0cec2
commit c57e272
Showing
13 changed files
with
294 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
from slither.core.declarations import Contract | ||
from slither.core.variables.state_variable import StateVariable | ||
from slither.core.solidity_types import ArrayType, ElementaryType | ||
|
||
|
||
def is_upgradable_gap_variable(contract: Contract, variable: StateVariable) -> bool: | ||
"""Helper function that returns true if 'variable' is a gap variable used | ||
for upgradable contracts. More specifically, the function returns true if: | ||
- variable is named "__gap" | ||
- it is a uint256 array declared at the end of the contract | ||
- it has private visibility""" | ||
|
||
# Return early on if the variable name is != gap to avoid iterating over all the state variables | ||
if variable.name != "__gap": | ||
return False | ||
|
||
declared_variable_ordered = [ | ||
v for v in contract.state_variables_ordered if v in contract.state_variables_declared | ||
] | ||
|
||
if not declared_variable_ordered: | ||
return False | ||
|
||
variable_type = variable.type | ||
return ( | ||
declared_variable_ordered[-1] is variable | ||
and isinstance(variable_type, ArrayType) | ||
and variable_type.type == ElementaryType("uint256") | ||
and variable.visibility == "private" | ||
) |
This file contains 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
8 changes: 8 additions & 0 deletions
8
tests/detectors/shadowing-abstract/0.7.5/public_gap_variable.sol
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
pragma solidity ^0.7.5; | ||
contract BaseContract{ | ||
uint256[50] private __gap; | ||
} | ||
|
||
contract DerivedContract is BaseContract{ | ||
uint256[50] public __gap; | ||
} |
95 changes: 95 additions & 0 deletions
95
...rs/shadowing-abstract/0.7.5/public_gap_variable.sol.0.7.5.ShadowingAbstractDetection.json
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
[ | ||
[ | ||
{ | ||
"elements": [ | ||
{ | ||
"type": "variable", | ||
"name": "__gap", | ||
"source_mapping": { | ||
"start": 127, | ||
"length": 24, | ||
"filename_used": "/GENERIC_PATH", | ||
"filename_relative": "tests/detectors/shadowing-abstract/0.7.5/public_gap_variable.sol", | ||
"filename_absolute": "/GENERIC_PATH", | ||
"filename_short": "tests/detectors/shadowing-abstract/0.7.5/public_gap_variable.sol", | ||
"is_dependency": false, | ||
"lines": [ | ||
7 | ||
], | ||
"starting_column": 5, | ||
"ending_column": 29 | ||
}, | ||
"type_specific_fields": { | ||
"parent": { | ||
"type": "contract", | ||
"name": "DerivedContract", | ||
"source_mapping": { | ||
"start": 81, | ||
"length": 73, | ||
"filename_used": "/GENERIC_PATH", | ||
"filename_relative": "tests/detectors/shadowing-abstract/0.7.5/public_gap_variable.sol", | ||
"filename_absolute": "/GENERIC_PATH", | ||
"filename_short": "tests/detectors/shadowing-abstract/0.7.5/public_gap_variable.sol", | ||
"is_dependency": false, | ||
"lines": [ | ||
6, | ||
7, | ||
8 | ||
], | ||
"starting_column": 1, | ||
"ending_column": 2 | ||
} | ||
} | ||
} | ||
}, | ||
{ | ||
"type": "variable", | ||
"name": "__gap", | ||
"source_mapping": { | ||
"start": 51, | ||
"length": 25, | ||
"filename_used": "/GENERIC_PATH", | ||
"filename_relative": "tests/detectors/shadowing-abstract/0.7.5/public_gap_variable.sol", | ||
"filename_absolute": "/GENERIC_PATH", | ||
"filename_short": "tests/detectors/shadowing-abstract/0.7.5/public_gap_variable.sol", | ||
"is_dependency": false, | ||
"lines": [ | ||
3 | ||
], | ||
"starting_column": 5, | ||
"ending_column": 30 | ||
}, | ||
"type_specific_fields": { | ||
"parent": { | ||
"type": "contract", | ||
"name": "BaseContract", | ||
"source_mapping": { | ||
"start": 24, | ||
"length": 55, | ||
"filename_used": "/GENERIC_PATH", | ||
"filename_relative": "tests/detectors/shadowing-abstract/0.7.5/public_gap_variable.sol", | ||
"filename_absolute": "/GENERIC_PATH", | ||
"filename_short": "tests/detectors/shadowing-abstract/0.7.5/public_gap_variable.sol", | ||
"is_dependency": false, | ||
"lines": [ | ||
2, | ||
3, | ||
4 | ||
], | ||
"starting_column": 1, | ||
"ending_column": 2 | ||
} | ||
} | ||
} | ||
} | ||
], | ||
"description": "DerivedContract.__gap (tests/detectors/shadowing-abstract/0.7.5/public_gap_variable.sol#7) shadows:\n\t- BaseContract.__gap (tests/detectors/shadowing-abstract/0.7.5/public_gap_variable.sol#3)\n", | ||
"markdown": "[DerivedContract.__gap](tests/detectors/shadowing-abstract/0.7.5/public_gap_variable.sol#L7) shadows:\n\t- [BaseContract.__gap](tests/detectors/shadowing-abstract/0.7.5/public_gap_variable.sol#L3)\n", | ||
"first_markdown_element": "tests/detectors/shadowing-abstract/0.7.5/public_gap_variable.sol#L7", | ||
"id": "8f81b2b4b3285fe96f0b580cdd2144cc6cf6808d970ba68878b9901744069c4c", | ||
"check": "shadowing-abstract", | ||
"impact": "Medium", | ||
"confidence": "High" | ||
} | ||
] | ||
] |
8 changes: 8 additions & 0 deletions
8
tests/detectors/shadowing-abstract/0.7.5/shadowing_state_variable.sol
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
pragma solidity ^0.7.5; | ||
contract BaseContract{ | ||
uint256[50] private __gap; | ||
} | ||
|
||
contract DerivedContract is BaseContract{ | ||
uint256[50] private __gap; | ||
} |
3 changes: 3 additions & 0 deletions
3
...adowing-abstract/0.7.5/shadowing_state_variable.sol.0.7.5.ShadowingAbstractDetection.json
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
[ | ||
[] | ||
] |
10 changes: 10 additions & 0 deletions
10
tests/detectors/shadowing-state/0.7.5/public_gap_variable.sol
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
pragma solidity ^0.7.5; | ||
contract BaseContract{ | ||
uint256[50] private __gap; | ||
function f() external {} | ||
} | ||
|
||
contract DerivedContract is BaseContract{ | ||
uint256[50] public __gap; | ||
function g() external {} | ||
} |
97 changes: 97 additions & 0 deletions
97
tests/detectors/shadowing-state/0.7.5/public_gap_variable.sol.0.7.5.StateShadowing.json
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
[ | ||
[ | ||
{ | ||
"elements": [ | ||
{ | ||
"type": "variable", | ||
"name": "__gap", | ||
"source_mapping": { | ||
"start": 156, | ||
"length": 24, | ||
"filename_used": "/GENERIC_PATH", | ||
"filename_relative": "tests/detectors/shadowing-state/0.7.5/public_gap_variable.sol", | ||
"filename_absolute": "/GENERIC_PATH", | ||
"filename_short": "tests/detectors/shadowing-state/0.7.5/public_gap_variable.sol", | ||
"is_dependency": false, | ||
"lines": [ | ||
8 | ||
], | ||
"starting_column": 5, | ||
"ending_column": 29 | ||
}, | ||
"type_specific_fields": { | ||
"parent": { | ||
"type": "contract", | ||
"name": "DerivedContract", | ||
"source_mapping": { | ||
"start": 110, | ||
"length": 102, | ||
"filename_used": "/GENERIC_PATH", | ||
"filename_relative": "tests/detectors/shadowing-state/0.7.5/public_gap_variable.sol", | ||
"filename_absolute": "/GENERIC_PATH", | ||
"filename_short": "tests/detectors/shadowing-state/0.7.5/public_gap_variable.sol", | ||
"is_dependency": false, | ||
"lines": [ | ||
7, | ||
8, | ||
9, | ||
10 | ||
], | ||
"starting_column": 1, | ||
"ending_column": 2 | ||
} | ||
} | ||
} | ||
}, | ||
{ | ||
"type": "variable", | ||
"name": "__gap", | ||
"source_mapping": { | ||
"start": 51, | ||
"length": 25, | ||
"filename_used": "/GENERIC_PATH", | ||
"filename_relative": "tests/detectors/shadowing-state/0.7.5/public_gap_variable.sol", | ||
"filename_absolute": "/GENERIC_PATH", | ||
"filename_short": "tests/detectors/shadowing-state/0.7.5/public_gap_variable.sol", | ||
"is_dependency": false, | ||
"lines": [ | ||
3 | ||
], | ||
"starting_column": 5, | ||
"ending_column": 30 | ||
}, | ||
"type_specific_fields": { | ||
"parent": { | ||
"type": "contract", | ||
"name": "BaseContract", | ||
"source_mapping": { | ||
"start": 24, | ||
"length": 84, | ||
"filename_used": "/GENERIC_PATH", | ||
"filename_relative": "tests/detectors/shadowing-state/0.7.5/public_gap_variable.sol", | ||
"filename_absolute": "/GENERIC_PATH", | ||
"filename_short": "tests/detectors/shadowing-state/0.7.5/public_gap_variable.sol", | ||
"is_dependency": false, | ||
"lines": [ | ||
2, | ||
3, | ||
4, | ||
5 | ||
], | ||
"starting_column": 1, | ||
"ending_column": 2 | ||
} | ||
} | ||
} | ||
} | ||
], | ||
"description": "DerivedContract.__gap (tests/detectors/shadowing-state/0.7.5/public_gap_variable.sol#8) shadows:\n\t- BaseContract.__gap (tests/detectors/shadowing-state/0.7.5/public_gap_variable.sol#3)\n", | ||
"markdown": "[DerivedContract.__gap](tests/detectors/shadowing-state/0.7.5/public_gap_variable.sol#L8) shadows:\n\t- [BaseContract.__gap](tests/detectors/shadowing-state/0.7.5/public_gap_variable.sol#L3)\n", | ||
"first_markdown_element": "tests/detectors/shadowing-state/0.7.5/public_gap_variable.sol#L8", | ||
"id": "8f81b2b4b3285fe96f0b580cdd2144cc6cf6808d970ba68878b9901744069c4c", | ||
"check": "shadowing-state", | ||
"impact": "High", | ||
"confidence": "High" | ||
} | ||
] | ||
] |
8 changes: 8 additions & 0 deletions
8
tests/detectors/shadowing-state/0.7.5/shadowing_state_variable.sol
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
pragma solidity ^0.7.5; | ||
contract BaseContract{ | ||
uint256[50] private __gap; | ||
} | ||
|
||
contract DerivedContract is BaseContract{ | ||
uint256[50] private __gap; | ||
} |
3 changes: 3 additions & 0 deletions
3
tests/detectors/shadowing-state/0.7.5/shadowing_state_variable.sol.0.7.5.StateShadowing.json
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
[ | ||
[] | ||
] |
This file contains 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