remove similar-names bc it's slow (#2469)

pull/2473/head
alpharush 6 months ago committed by GitHub
parent 23006f5405
commit b9a3ea6189
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 1
      slither/detectors/all_detectors.py
  2. 106
      slither/detectors/variables/similar_variables.py
  3. 2
      tests/e2e/detectors/snapshots/detectors__detector_SimilarVarsDetection_0_4_25_similar_variables_sol__0.txt
  4. 2
      tests/e2e/detectors/snapshots/detectors__detector_SimilarVarsDetection_0_5_16_similar_variables_sol__0.txt
  5. 2
      tests/e2e/detectors/snapshots/detectors__detector_SimilarVarsDetection_0_6_11_similar_variables_sol__0.txt
  6. 2
      tests/e2e/detectors/snapshots/detectors__detector_SimilarVarsDetection_0_7_6_similar_variables_sol__0.txt
  7. 7
      tests/e2e/detectors/test_data/similar-names/0.4.25/similar_variables.sol
  8. BIN
      tests/e2e/detectors/test_data/similar-names/0.4.25/similar_variables.sol-0.4.25.zip
  9. 7
      tests/e2e/detectors/test_data/similar-names/0.5.16/similar_variables.sol
  10. BIN
      tests/e2e/detectors/test_data/similar-names/0.5.16/similar_variables.sol-0.5.16.zip
  11. 7
      tests/e2e/detectors/test_data/similar-names/0.6.11/similar_variables.sol
  12. BIN
      tests/e2e/detectors/test_data/similar-names/0.6.11/similar_variables.sol-0.6.11.zip
  13. 7
      tests/e2e/detectors/test_data/similar-names/0.7.6/similar_variables.sol
  14. BIN
      tests/e2e/detectors/test_data/similar-names/0.7.6/similar_variables.sol-0.7.6.zip
  15. 20
      tests/e2e/detectors/test_detectors.py

@ -57,7 +57,6 @@ from .slither.name_reused import NameReused
from .functions.unimplemented import UnimplementedFunctionDetection
from .statements.mapping_deletion import MappingDeletionDetection
from .statements.array_length_assignment import ArrayLengthAssignment
from .variables.similar_variables import SimilarVarsDetection
from .variables.function_init_state_variables import FunctionInitializedState
from .statements.redundant_statements import RedundantStatements
from .operations.bad_prng import BadPRNG

@ -1,106 +0,0 @@
"""
Check for state variables too similar
Do not check contract inheritance
"""
import difflib
from typing import List, Set, Tuple
from slither.core.declarations.contract import Contract
from slither.core.variables.local_variable import LocalVariable
from slither.detectors.abstract_detector import (
AbstractDetector,
DetectorClassification,
DETECTOR_INFO,
)
from slither.utils.output import Output
class SimilarVarsDetection(AbstractDetector):
"""
Variable similar detector
"""
ARGUMENT = "similar-names"
HELP = "Variable names are too similar"
IMPACT = DetectorClassification.INFORMATIONAL
CONFIDENCE = DetectorClassification.MEDIUM
WIKI = (
"https://github.com/crytic/slither/wiki/Detector-Documentation#variable-names-too-similar"
)
WIKI_TITLE = "Variable names too similar"
WIKI_DESCRIPTION = "Detect variables with names that are too similar."
WIKI_EXPLOIT_SCENARIO = "Bob uses several variables with similar names. As a result, his code is difficult to review."
WIKI_RECOMMENDATION = "Prevent variables from having similar names."
@staticmethod
def similar(seq1: str, seq2: str) -> bool:
"""Test the name similarity
Two name are similar if difflib.SequenceMatcher on the lowercase
version of the name is greater than 0.90
See: https://docs.python.org/2/library/difflib.html
Args:
seq1 (str): first name
seq2 (str): second name
Returns:
bool: true if names are similar
"""
val = difflib.SequenceMatcher(a=seq1, b=seq2).ratio()
ret = val > 0.90
return ret
@staticmethod
def detect_sim(contract: Contract) -> Set[Tuple[LocalVariable, LocalVariable]]:
"""Detect variables with similar name
Returns:
bool: true if variables have similar name
"""
all_var = [x.variables for x in contract.functions]
all_var = [x for l in all_var for x in l]
contract_var = contract.variables
all_var = list(set(all_var + contract_var))
ret = set()
# pylint: disable=consider-using-enumerate
for i in range(len(all_var)):
v1 = all_var[i]
_v1_name_lower = v1.name.lower()
for j in range(i, len(all_var)):
v2 = all_var[j]
if len(v1.name) != len(v2.name):
continue
_v2_name_lower = v2.name.lower()
if _v1_name_lower != _v2_name_lower:
if SimilarVarsDetection.similar(_v1_name_lower, _v2_name_lower):
ret.add((v1, v2))
return ret
def _detect(self) -> List[Output]:
"""Detect similar variables name
Returns:
list: {'vuln', 'filename,'contract','vars'}
"""
results = []
for c in self.contracts:
allVars = self.detect_sim(c)
if allVars:
for (v1, v2) in sorted(allVars, key=lambda x: (x[0].name, x[1].name)):
v_left = v1 if v1.name < v2.name else v2
v_right = v2 if v_left == v1 else v1
info: DETECTOR_INFO = [
"Variable ",
v_left,
" is too similar to ",
v_right,
"\n",
]
json = self.generate_result(info)
results.append(json)
return results

@ -1,2 +0,0 @@
Variable Similar.f().testVariable (tests/e2e/detectors/test_data/similar-names/0.4.25/similar_variables.sol#3) is too similar to Similar.f().textVariable (tests/e2e/detectors/test_data/similar-names/0.4.25/similar_variables.sol#4)

@ -1,2 +0,0 @@
Variable Similar.f().testVariable (tests/e2e/detectors/test_data/similar-names/0.5.16/similar_variables.sol#3) is too similar to Similar.f().textVariable (tests/e2e/detectors/test_data/similar-names/0.5.16/similar_variables.sol#4)

@ -1,2 +0,0 @@
Variable Similar.f().testVariable (tests/e2e/detectors/test_data/similar-names/0.6.11/similar_variables.sol#3) is too similar to Similar.f().textVariable (tests/e2e/detectors/test_data/similar-names/0.6.11/similar_variables.sol#4)

@ -1,2 +0,0 @@
Variable Similar.f().testVariable (tests/e2e/detectors/test_data/similar-names/0.7.6/similar_variables.sol#3) is too similar to Similar.f().textVariable (tests/e2e/detectors/test_data/similar-names/0.7.6/similar_variables.sol#4)

@ -1,7 +0,0 @@
contract Similar {
function f() public returns (uint) {
uint testVariable = 1;
uint textVariable = 2;
return testVariable + textVariable;
}
}

@ -1,7 +0,0 @@
contract Similar {
function f() public returns (uint) {
uint testVariable = 1;
uint textVariable = 2;
return testVariable + textVariable;
}
}

@ -1,7 +0,0 @@
contract Similar {
function f() public returns (uint) {
uint testVariable = 1;
uint textVariable = 2;
return testVariable + textVariable;
}
}

@ -1,7 +0,0 @@
contract Similar {
function f() public returns (uint) {
uint testVariable = 1;
uint textVariable = 2;
return testVariable + textVariable;
}
}

@ -1453,26 +1453,6 @@ ALL_TESTS = [
"type_based_tautology.sol",
"0.7.6",
),
Test(
all_detectors.SimilarVarsDetection,
"similar_variables.sol",
"0.4.25",
),
Test(
all_detectors.SimilarVarsDetection,
"similar_variables.sol",
"0.5.16",
),
Test(
all_detectors.SimilarVarsDetection,
"similar_variables.sol",
"0.6.11",
),
Test(
all_detectors.SimilarVarsDetection,
"similar_variables.sol",
"0.7.6",
),
Test(
all_detectors.MsgValueInLoop,
"msg_value_loop.sol",

Loading…
Cancel
Save