Skip to content
Merged
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
3 changes: 2 additions & 1 deletion slither/formatters/utils/patches.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,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"])
Comment on lines +34 to +35
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Subtle, the bug here would only manifest if the new_string contains unicode which I don't think it does usually. But yea, this change is correct

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 @@ -92,12 +92,15 @@ def test_patch(
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:
Comment on lines +95 to +103
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Man. I fixed a related issue in a prev PR and I thought that had us good to go wrt unicode support. But looking at this now, idk how the prev fix actually fixed anything. This is the same byte-wise indexing-against-strings bug that I was wrestling w earlier, just in a different but still critical spot. Good job finding & fixing this.

filepath.write(replaced_content)

if compile_generated_mutant(file, mappings):
Expand Down