Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion slither/formatters/utils/patches.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from slither.core.compilation_unit import SlitherCompilationUnit


# pylint: disable=too-many-arguments
def create_patch(
result: Dict,
Expand All @@ -31,7 +32,8 @@ def apply_patch(original_txt: bytes, patch: Dict, offset: int) -> Tuple[bytes, i
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"])
# Note: must use byte length since patch offsets are byte offsets from solc
patch_length_diff = len(patch["new_string"].encode("utf8")) - (patch["end"] - patch["start"])
return patched_txt, patch_length_diff + offset


Expand Down
9 changes: 6 additions & 3 deletions slither/tools/mutator/utils/testing_generated_mutant.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,15 @@ def test_patch( # pylint: disable=too-many-arguments
function to verify whether each patch is caught by tests
returns: 0 (uncaught), 1 (caught), or 2 (compilation failure)
"""
with open(file, "r", encoding="utf8") as filepath:
with open(file, "rb") as filepath:
content = filepath.read()
# Perform the replacement based on the index values
replaced_content = content[: patch["start"]] + patch["new_string"] + content[patch["end"] :]
# Note: patch offsets are byte offsets from solc, so we must work with bytes
replaced_content = (
content[: patch["start"]] + patch["new_string"].encode("utf8") + content[patch["end"] :]
)
# Write the modified content back to the file
with open(file, "w", encoding="utf8") as filepath:
with open(file, "wb") as filepath:
filepath.write(replaced_content)

if compile_generated_mutant(file, mappings):
Expand Down