mirror of https://github.com/crytic/slither
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
73 lines
3.1 KiB
73 lines
3.1 KiB
6 years ago
|
import re
|
||
|
from slither.exceptions import SlitherException
|
||
|
from ..utils.patches import create_patch
|
||
6 years ago
|
|
||
6 years ago
|
|
||
|
class FormatPragma:
|
||
|
|
||
|
# Indicates the recommended versions for replacement
|
||
|
REPLACEMENT_VERSIONS = ["0.4.25", "0.5.3"]
|
||
|
|
||
|
# group:
|
||
|
# 0: ^ > >= < <= (optional)
|
||
|
# 1: ' ' (optional)
|
||
|
# 2: version number
|
||
|
# 3: version number
|
||
|
# 4: version number
|
||
|
PATTERN = re.compile('(\^|>|>=|<|<=)?([ ]+)?(\d+)\.(\d+)\.(\d+)')
|
||
|
|
||
|
@staticmethod
|
||
|
def format(slither, patches, elements):
|
||
|
versions_used = []
|
||
|
for element in elements:
|
||
6 years ago
|
versions_used.append(''.join(element['type_specific_fields']['directive'][1:]))
|
||
6 years ago
|
solc_version_replace = FormatPragma.analyse_versions(versions_used)
|
||
|
for element in elements:
|
||
6 years ago
|
FormatPragma.create_patch(slither, patches, element['source_mapping']['filename_absolute'],
|
||
|
element['source_mapping']['filename_relative'], solc_version_replace,
|
||
|
element['source_mapping']['start'],
|
||
|
element['source_mapping']['start'] + element['source_mapping']['length'])
|
||
6 years ago
|
|
||
|
@staticmethod
|
||
|
def analyse_versions(used_solc_versions):
|
||
|
replace_solc_versions = list()
|
||
|
for version in used_solc_versions:
|
||
|
replace_solc_versions.append(FormatPragma.determine_solc_version_replacement(version))
|
||
|
if not all(version == replace_solc_versions[0] for version in replace_solc_versions):
|
||
6 years ago
|
raise SlitherException("Multiple incompatible versions!")
|
||
6 years ago
|
else:
|
||
|
return replace_solc_versions[0]
|
||
|
|
||
|
@staticmethod
|
||
|
def determine_solc_version_replacement(used_solc_version):
|
||
|
versions = FormatPragma.PATTERN.findall(used_solc_version)
|
||
|
if len(versions) == 1:
|
||
|
version = versions[0]
|
||
|
minor_version = '.'.join(version[2:])[2]
|
||
|
if minor_version == '4':
|
||
|
return "pragma solidity " + FormatPragma.REPLACEMENT_VERSIONS[0] + ';'
|
||
|
elif minor_version == '5':
|
||
|
return "pragma solidity " + FormatPragma.REPLACEMENT_VERSIONS[1] + ';'
|
||
|
else:
|
||
6 years ago
|
raise SlitherException("Unknown version!")
|
||
6 years ago
|
elif len(versions) == 2:
|
||
|
version_right = versions[1]
|
||
|
minor_version_right = '.'.join(version_right[2:])[2]
|
||
|
if minor_version_right == '4':
|
||
|
return "pragma solidity " + FormatPragma.REPLACEMENT_VERSIONS[0] + ';'
|
||
6 years ago
|
elif minor_version_right in ['5', '6']:
|
||
6 years ago
|
return "pragma solidity " + FormatPragma.REPLACEMENT_VERSIONS[1] + ';'
|
||
6 years ago
|
|
||
6 years ago
|
@staticmethod
|
||
6 years ago
|
def create_patch(slither, patches, in_file, in_file_relative, pragma, modify_loc_start, modify_loc_end):
|
||
6 years ago
|
in_file_str = slither.source_code[in_file].encode('utf-8')
|
||
6 years ago
|
old_str_of_interest = in_file_str[modify_loc_start:modify_loc_end]
|
||
6 years ago
|
create_patch(patches,
|
||
|
"pragma",
|
||
|
in_file_relative,
|
||
|
in_file,
|
||
|
int(modify_loc_start),
|
||
|
int(modify_loc_end),
|
||
|
old_str_of_interest.decode('utf-8'),
|
||
|
pragma)
|