Merge branch 'dev' into dev-more-types

pull/1666/head
Feist Josselin 2 years ago
commit 4175aaffdf
  1. 3
      slither/detectors/variables/predeclaration_usage_local.py
  2. 59
      tests/conftest.py
  3. 12
      tests/test_features.py

@ -10,6 +10,7 @@ from slither.core.variables.local_variable import LocalVariable
from slither.detectors.abstract_detector import (
AbstractDetector,
DetectorClassification,
ALL_SOLC_VERSIONS_04,
DETECTOR_INFO,
)
from slither.utils.output import Output
@ -58,6 +59,8 @@ Additionally, the for-loop uses the variable `max`, which is declared in a previ
WIKI_RECOMMENDATION = "Move all variable declarations prior to any usage of the variable, and ensure that reaching a variable declaration does not depend on some conditional if it is used unconditionally."
VULNERABLE_SOLC_VERSIONS = ALL_SOLC_VERSIONS_04
def detect_predeclared_local_usage(
self,
node: Node,

@ -1,59 +0,0 @@
import os
from contextlib import contextmanager
from pathlib import Path
from typing import Optional
import pytest
from crytic_compile import CryticCompile
from crytic_compile.platform.solc_standard_json import SolcStandardJson
from solc_select import solc_select
from slither import Slither
@contextmanager
def _select_solc_version(version: Optional[str]):
"""Selects solc version to use for running tests.
If no version is provided, latest is used."""
if not version:
# This sorts the versions numerically
vers = sorted(
map(
lambda x: (int(x[0]), int(x[1]), int(x[2])),
map(lambda x: x.split(".", 3), solc_select.installed_versions()),
)
)
ver = list(vers)[-1]
version = ".".join(map(str, ver))
env = dict(os.environ)
env_restore = dict(env)
env["SOLC_VERSION"] = version
os.environ.clear()
os.environ.update(env)
yield version
os.environ.clear()
os.environ.update(env_restore)
@pytest.fixture(name="select_solc_version")
def fixture_select_solc_version():
return _select_solc_version
@pytest.fixture
def slither_from_dir(select_solc_version):
@contextmanager
def _slither_from_dir(directory: str, solc_version: Optional[str] = None):
"""Yields a Slither instance using solidity files in directory and solc_version.
Temporarily changes the solc-version temporary to solc_version.
"""
standard_json = SolcStandardJson()
for source_file in Path(directory).rglob("*.sol"):
standard_json.add_source_file(Path(source_file).as_posix())
with select_solc_version(solc_version):
compilation = CryticCompile(standard_json)
yield Slither(compilation)
return _slither_from_dir

@ -1,4 +1,5 @@
import inspect
from pathlib import Path
from crytic_compile import CryticCompile
from crytic_compile.platform.solc_standard_json import SolcStandardJson
@ -162,6 +163,11 @@ def test_arithmetic_usage() -> None:
} == {"2b4bc73cf59d486dd9043e840b5028b679354dd9", "e4ecd4d0fda7e762d29aceb8425f2c5d4d0bf962"}
def test_using_for_global_collision(slither_from_dir) -> None:
with slither_from_dir("./tests/using-for-global-collision") as sl:
_run_all_detectors(sl)
def test_using_for_global_collision() -> None:
solc_select.switch_global_version("0.8.18", always_install=True)
standard_json = SolcStandardJson()
for source_file in Path("./tests/using-for-global-collision").rglob("*.sol"):
standard_json.add_source_file(Path(source_file).as_posix())
compilation = CryticCompile(standard_json)
sl = Slither(compilation)
_run_all_detectors(sl)

Loading…
Cancel
Save