From af5bfa65b8b3c27f249e0463b5c84ce53c408a82 Mon Sep 17 00:00:00 2001 From: Josselin Date: Fri, 5 Jul 2019 19:12:44 +0200 Subject: [PATCH] Improve patches generation --- utils/slither_format/formatters/naming_convention.py | 2 +- utils/slither_format/slither_format.py | 12 +++++++++--- utils/slither_format/utils/patches.py | 11 +++++++---- 3 files changed, 17 insertions(+), 8 deletions(-) diff --git a/utils/slither_format/formatters/naming_convention.py b/utils/slither_format/formatters/naming_convention.py index d568f4e78..323732142 100644 --- a/utils/slither_format/formatters/naming_convention.py +++ b/utils/slither_format/formatters/naming_convention.py @@ -409,7 +409,7 @@ def _explore_irs(slither, irs, result, target, convert): def _explore_functions(slither, functions, result, target, convert): for function in functions: - _explore_variables_declaration(slither, function.local_variables, result, target, convert) + _explore_variables_declaration(slither, function.variables, result, target, convert) _explore_modifiers_calls(slither, function, result, target, convert) _explore_irs(slither, function.all_slithir_operations(), result, target, convert) diff --git a/utils/slither_format/slither_format.py b/utils/slither_format/slither_format.py index 458649579..1e024e774 100644 --- a/utils/slither_format/slither_format.py +++ b/utils/slither_format/slither_format.py @@ -59,14 +59,20 @@ def slither_format(slither, **kwargs): for file in result['patches']: original_txt = slither.source_code[file] patched_txt = original_txt - for patch in result['patches'][file]: - patched_txt = apply_patch(patched_txt, patch) + offset = 0 + patches = result['patches'][file] + patches.sort(key=lambda x:x['start']) + if not all(patches[i]['end'] <= patches[i+1]['end'] for i in range(len(patches)-1)): + logger.info(f'Impossible to generate patch; patches collisions: {patches}') + continue + for patch in patches: + patched_txt, offset = apply_patch(patched_txt, patch, offset) diff = create_diff(slither, original_txt, patched_txt, file) result['paches_diff'] = diff if skip_file_generation: continue if not diff: - logger.info(f'Empty patch generated {result}') + logger.info(f'Impossible to generate patch; empty {result}') continue path = os.path.join(export, f'fix_{counter}.patch') logger.info(f'\t- {path}') diff --git a/utils/slither_format/utils/patches.py b/utils/slither_format/utils/patches.py index cf4bacb72..a8fb48d11 100644 --- a/utils/slither_format/utils/patches.py +++ b/utils/slither_format/utils/patches.py @@ -14,11 +14,14 @@ def create_patch(result, file, start, end, old_str, new_str): result['patches'][file].append(p) -def apply_patch(original_txt, patch): - patched_txt = original_txt[:int(patch['start'])] +def apply_patch(original_txt, patch, offset): + patched_txt = original_txt[:int(patch['start'] + offset)] patched_txt += patch['new_string'] - patched_txt += original_txt[int(patch['end']):] - return patched_txt + patched_txt += original_txt[int(patch['end'] + offset):] + + # Keep the diff of text added or sub, in case of multiple patches + patch_length_diff = len(patch['new_string']) - (patch['end'] - patch['start']) + return patched_txt, patch_length_diff + offset def create_diff(slither, original_txt, patched_txt, filename):