diff --git a/slither/core/slither_core.py b/slither/core/slither_core.py index 8106930ef..f5fc88278 100644 --- a/slither/core/slither_core.py +++ b/slither/core/slither_core.py @@ -44,7 +44,7 @@ class Slither(Context): @property def source_code(self): - """ {filename: source_code}: source code """ + """ {filename: source_code (str)}: source code """ return self._raw_source_code @property diff --git a/slither/tools/slither_format/formatters/constable_states.py b/slither/tools/slither_format/formatters/constable_states.py index f07dd8c3a..67f51536f 100644 --- a/slither/tools/slither_format/formatters/constable_states.py +++ b/slither/tools/slither_format/formatters/constable_states.py @@ -13,7 +13,7 @@ def format(slither, result): def _patch(slither, result, in_file, match_text, replace_text, modify_loc_start, modify_loc_end): - in_file_str = slither.source_code[in_file].encode('utf-8') + in_file_str = slither.source_code[in_file].encode('utf8') old_str_of_interest = in_file_str[modify_loc_start:modify_loc_end] # Add keyword `constant` before the variable name (new_str_of_interest, num_repl) = re.subn(match_text, replace_text, old_str_of_interest.decode('utf-8'), 1) @@ -22,7 +22,7 @@ def _patch(slither, result, in_file, match_text, replace_text, modify_loc_start, in_file, modify_loc_start, modify_loc_end, - old_str_of_interest.decode('utf-8'), + old_str_of_interest, new_str_of_interest) else: diff --git a/slither/tools/slither_format/formatters/constant_function.py b/slither/tools/slither_format/formatters/constant_function.py index 23939acbc..02b09fa6a 100644 --- a/slither/tools/slither_format/formatters/constant_function.py +++ b/slither/tools/slither_format/formatters/constant_function.py @@ -20,7 +20,7 @@ def format(slither, patches, elements): def _patch(slither, patches, in_file, in_file_relative, modify_loc_start, modify_loc_end): - in_file_str = slither.source_code[in_file].encode('utf-8') + in_file_str = slither.source_code[in_file].encode('utf8') old_str_of_interest = in_file_str[modify_loc_start:modify_loc_end] # Find the keywords view|pure|constant and remove them m = re.search("(view|pure|constant)", old_str_of_interest.decode('utf-8')) diff --git a/slither/tools/slither_format/formatters/external_function.py b/slither/tools/slither_format/formatters/external_function.py index ec5e160f8..b3f999a41 100644 --- a/slither/tools/slither_format/formatters/external_function.py +++ b/slither/tools/slither_format/formatters/external_function.py @@ -16,7 +16,7 @@ def format(slither, result): def _patch(slither, result, in_file, modify_loc_start, modify_loc_end): - in_file_str = slither.source_code[in_file].encode('utf-8') + in_file_str = slither.source_code[in_file].encode('utf8') old_str_of_interest = in_file_str[modify_loc_start:modify_loc_end] # Search for 'public' keyword which is in-between the function name and modifier name (if present) # regex: 'public' could have spaces around or be at the end of the line diff --git a/slither/tools/slither_format/formatters/naming_convention.py b/slither/tools/slither_format/formatters/naming_convention.py index ab14e98f3..b56b99f92 100644 --- a/slither/tools/slither_format/formatters/naming_convention.py +++ b/slither/tools/slither_format/formatters/naming_convention.py @@ -81,7 +81,7 @@ def _convert_CapWords(original_name, slither): def _convert_mixedCase(original_name, slither): - name = original_name + name = str(original_name) while '_' in name: offset = name.find('_') if len(name) > offset: @@ -192,9 +192,9 @@ def _patch(slither, result, element, _target): # group 2: beginning of the to type # nested mapping are within the group 1 #RE_MAPPING = '[ ]*mapping[ ]*\([ ]*([\=\>\(\) a-zA-Z0-9\._\[\]]*)[ ]*=>[ ]*([a-zA-Z0-9\._\[\]]*)\)' -RE_MAPPING_FROM = '([a-zA-Z0-9\._\[\]]*)' -RE_MAPPING_TO = '([\=\>\(\) a-zA-Z0-9\._\[\]\ ]*)' -RE_MAPPING = '[ ]*mapping[ ]*\([ ]*' + RE_MAPPING_FROM + '[ ]*' + '=>' + '[ ]*'+ RE_MAPPING_TO + '\)' +RE_MAPPING_FROM = b'([a-zA-Z0-9\._\[\]]*)' +RE_MAPPING_TO = b'([\=\>\(\) a-zA-Z0-9\._\[\]\ ]*)' +RE_MAPPING = b'[ ]*mapping[ ]*\([ ]*' + RE_MAPPING_FROM + b'[ ]*' + b'=>' + b'[ ]*'+ RE_MAPPING_TO + b'\)' def _explore_type(slither, result, target, convert, type, filename_source_code, start, end): if isinstance(type, UserDefinedType): @@ -253,7 +253,7 @@ def _explore_type(slither, result, target, convert, type, filename_source_code, full_txt_start = start full_txt_end = end - full_txt = slither.source_code[filename_source_code][full_txt_start:full_txt_end] + full_txt = slither.source_code[filename_source_code].encode('utf8')[full_txt_start:full_txt_end] re_match = re.match(RE_MAPPING, full_txt) assert re_match @@ -306,7 +306,7 @@ def _explore_variables_declaration(slither, variables, result, target, convert): filename_source_code = variable.source_mapping['filename_absolute'] full_txt_start = variable.source_mapping['start'] full_txt_end = full_txt_start + variable.source_mapping['length'] - full_txt = slither.source_code[filename_source_code][full_txt_start:full_txt_end] + full_txt = slither.source_code[filename_source_code].encode('utf8')[full_txt_start:full_txt_end] _explore_type(slither, result, @@ -326,7 +326,7 @@ def _explore_variables_declaration(slither, variables, result, target, convert): # We take all the space, as we dont know the type # In comparison to other matches, it is ok as there will not be more than one # 'spaces' zone (ex: for function, the body and the signature will also contain space) - matches = re.finditer('[ ]*', full_txt) + matches = re.finditer(b'[ ]*', full_txt) # Look for the end offset of the largest list of ' ' loc_start = full_txt_start + max(matches, key=lambda x:len(x.group())).end() loc_end = loc_start + len(old_str) @@ -362,7 +362,7 @@ def _explore_structures_declaration(slither, structures, result, target, convert filename_source_code = st.source_mapping['filename_absolute'] full_txt_start = st.source_mapping['start'] full_txt_end = full_txt_start + st.source_mapping['length'] - full_txt = slither.source_code[filename_source_code][full_txt_start:full_txt_end] + full_txt = slither.source_code[filename_source_code].encode('utf8')[full_txt_start:full_txt_end] # The name is after the space matches = re.finditer('struct[ ]*', full_txt) @@ -427,12 +427,12 @@ def _explore_irs(slither, irs, result, target, convert): filename_source_code = source_mapping['filename_absolute'] full_txt_start = source_mapping['start'] full_txt_end = full_txt_start + source_mapping['length'] - full_txt = slither.source_code[filename_source_code][full_txt_start:full_txt_end] + full_txt = slither.source_code[filename_source_code].encode('utf8')[full_txt_start:full_txt_end] - if not str(target) in full_txt: + if not target.name.encode('utf8') in full_txt: raise FormatError(f'{target} not found in {full_txt} ({source_mapping}') - old_str = str(target) + old_str = target.name.encode('utf8') new_str = convert(old_str, slither) counter = 0 @@ -469,7 +469,7 @@ def _explore_functions(slither, functions, result, target, convert): filename_source_code = function.source_mapping['filename_absolute'] full_txt_start = function.source_mapping['start'] full_txt_end = full_txt_start + function.source_mapping['length'] - full_txt = slither.source_code[filename_source_code][full_txt_start:full_txt_end] + full_txt = slither.source_code[filename_source_code].encode('utf8')[full_txt_start:full_txt_end] # The name is after the space if isinstance(target, Modifier): @@ -497,7 +497,7 @@ def _explore_enums(slither, enums, result, target, convert): filename_source_code = enum.source_mapping['filename_absolute'] full_txt_start = enum.source_mapping['start'] full_txt_end = full_txt_start + enum.source_mapping['length'] - full_txt = slither.source_code[filename_source_code][full_txt_start:full_txt_end] + full_txt = slither.source_code[filename_source_code].encode('utf8')[full_txt_start:full_txt_end] # The name is after the space matches = re.finditer('enum([ ]*)', full_txt) @@ -523,7 +523,7 @@ def _explore_contract(slither, contract, result, target, convert): filename_source_code = contract.source_mapping['filename_absolute'] full_txt_start = contract.source_mapping['start'] full_txt_end = full_txt_start + contract.source_mapping['length'] - full_txt = slither.source_code[filename_source_code][full_txt_start:full_txt_end] + full_txt = slither.source_code[filename_source_code].encode('utf8')[full_txt_start:full_txt_end] old_str = contract.name new_str = convert(old_str, slither) diff --git a/slither/tools/slither_format/formatters/pragma.py b/slither/tools/slither_format/formatters/pragma.py index 57f01b3c9..5f8d65ede 100644 --- a/slither/tools/slither_format/formatters/pragma.py +++ b/slither/tools/slither_format/formatters/pragma.py @@ -59,11 +59,11 @@ def _determine_solc_version_replacement(used_solc_version): def _patch(slither, result, in_file, pragma, modify_loc_start, modify_loc_end): - in_file_str = slither.source_code[in_file].encode('utf-8') + in_file_str = slither.source_code[in_file].encode('utf8') old_str_of_interest = in_file_str[modify_loc_start:modify_loc_end] create_patch(result, in_file, int(modify_loc_start), int(modify_loc_end), - old_str_of_interest.decode('utf-8'), + old_str_of_interest, pragma) diff --git a/slither/tools/slither_format/formatters/solc_version.py b/slither/tools/slither_format/formatters/solc_version.py index fe7b3fe27..fbc3cace2 100644 --- a/slither/tools/slither_format/formatters/solc_version.py +++ b/slither/tools/slither_format/formatters/solc_version.py @@ -49,11 +49,11 @@ def _determine_solc_version_replacement(used_solc_version): def _patch(slither, result, in_file, solc_version, modify_loc_start, modify_loc_end): - in_file_str = slither.source_code[in_file].encode('utf-8') + in_file_str = slither.source_code[in_file].encode('utf8') old_str_of_interest = in_file_str[modify_loc_start:modify_loc_end] create_patch(result, in_file, int(modify_loc_start), int(modify_loc_end), - old_str_of_interest.decode('utf-8'), + old_str_of_interest, solc_version) diff --git a/slither/tools/slither_format/formatters/unused_state.py b/slither/tools/slither_format/formatters/unused_state.py index 2bb3abee1..0e0f30bde 100644 --- a/slither/tools/slither_format/formatters/unused_state.py +++ b/slither/tools/slither_format/formatters/unused_state.py @@ -12,7 +12,7 @@ def format(slither, result): def _patch(slither, result, in_file, modify_loc_start): - in_file_str = slither.source_code[in_file].encode('utf-8') + in_file_str = slither.source_code[in_file].encode('utf8') old_str_of_interest = in_file_str[modify_loc_start:] old_str = old_str_of_interest.decode('utf-8').partition(';')[0]\ + old_str_of_interest.decode('utf-8').partition(';')[1] diff --git a/slither/tools/slither_format/slither_format.py b/slither/tools/slither_format/slither_format.py index fe97ed82c..01cf1ee19 100644 --- a/slither/tools/slither_format/slither_format.py +++ b/slither/tools/slither_format/slither_format.py @@ -57,7 +57,7 @@ def slither_format(slither, **kwargs): logger.info(f'Issue: {one_line_description}') logger.info('Generated:') for file in result['patches']: - original_txt = slither.source_code[file] + original_txt = slither.source_code[file].encode('utf8') patched_txt = original_txt offset = 0 patches = result['patches'][file] diff --git a/slither/tools/slither_format/utils/patches.py b/slither/tools/slither_format/utils/patches.py index a8fb48d11..dc8414bd4 100644 --- a/slither/tools/slither_format/utils/patches.py +++ b/slither/tools/slither_format/utils/patches.py @@ -3,6 +3,10 @@ import difflib from collections import defaultdict def create_patch(result, file, start, end, old_str, new_str): + if isinstance(old_str, str): + old_str = old_str.encode('utf8') + if isinstance(new_str, str): + new_str = new_str.encode('utf8') p = {"start": start, "end": end, "old_string": old_str, @@ -30,8 +34,8 @@ def create_diff(slither, original_txt, patched_txt, filename): relative_path = os.path.join('.', relative_path) else: relative_path = filename - diff = difflib.unified_diff(original_txt.splitlines(False), - patched_txt.splitlines(False), + diff = difflib.unified_diff(original_txt.decode('utf8').splitlines(False), + patched_txt.decode('utf8').splitlines(False), fromfile=relative_path, tofile=relative_path, lineterm='')