Improve patches generation

pull/238/head
Josselin 5 years ago
parent 6599ae69e6
commit af5bfa65b8
  1. 2
      utils/slither_format/formatters/naming_convention.py
  2. 12
      utils/slither_format/slither_format.py
  3. 11
      utils/slither_format/utils/patches.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)

@ -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}')

@ -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):

Loading…
Cancel
Save