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.
40 lines
1.9 KiB
40 lines
1.9 KiB
6 years ago
|
import re
|
||
|
from slither.exceptions import SlitherException
|
||
|
from ..utils.patches import create_patch
|
||
6 years ago
|
|
||
|
class FormatConstantFunction:
|
||
|
|
||
|
@staticmethod
|
||
|
def format(slither, patches, elements):
|
||
|
for element in elements:
|
||
|
if element['type'] != "function":
|
||
|
# Skip variable elements
|
||
|
continue
|
||
6 years ago
|
target_contract = slither.get_contract_from_name(element['type_specific_fields']['parent']['name'])
|
||
|
if target_contract:
|
||
|
for function in target_contract.functions:
|
||
|
if function.name == element['name']:
|
||
6 years ago
|
FormatConstantFunction.create_patch(slither, patches,
|
||
|
element['source_mapping']['filename_absolute'],
|
||
|
element['source_mapping']['filename_relative'],
|
||
|
int(function.parameters_src.source_mapping['start']),
|
||
6 years ago
|
int(function.returns_src.source_mapping['start']))
|
||
6 years ago
|
break
|
||
6 years ago
|
|
||
|
@staticmethod
|
||
6 years ago
|
def create_patch(slither, patches, in_file, in_file_relative, 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
|
m = re.search("(view|pure|constant)", old_str_of_interest.decode('utf-8'))
|
||
6 years ago
|
if m:
|
||
6 years ago
|
create_patch(patches,
|
||
|
"constant-function",
|
||
|
in_file_relative,
|
||
|
in_file,
|
||
|
modify_loc_start + m.span()[0],
|
||
|
modify_loc_start + m.span()[1],
|
||
|
m.groups(0)[0],
|
||
|
"")
|
||
6 years ago
|
else:
|
||
6 years ago
|
raise SlitherException("No view/pure/constant specifier exists. Regex failed to remove specifier!")
|