Skip to content

Commit

Permalink
fix formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
bohendo committed Feb 7, 2025
1 parent b3a226e commit 8dee964
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 27 deletions.
4 changes: 2 additions & 2 deletions slither/core/source_mapping/source_mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ def content(self) -> str:
assert self.compilation_unit
return (
self.compilation_unit.core.source_code[self.filename.absolute]
.encode("utf8")[self.start : self.end]
.decode("utf8")
.encode("utf8")[self.start : self.end]
.decode("utf8")
)

@property
Expand Down
8 changes: 6 additions & 2 deletions slither/tools/documentation/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,9 @@ def _handle_function(
prompt = "Create a natpsec documentation for this solidity code with only notice and dev.\n"
srcmap = function.source_mapping
src = function.compilation_unit.core.source_code[srcmap.filename.absolute]
first_char_index = len(src.encode("utf8")[:srcmap.start].decode("utf8")) # convert byte offset to char offset
first_char_index = len(
src.encode("utf8")[: srcmap.start].decode("utf8")
) # convert byte offset to char offset
prev_char = src[first_char_index - 1]
prompt += srcmap.content

Expand Down Expand Up @@ -201,7 +203,9 @@ def _handle_function(
if not answer_processed:
return overwrite

create_patch(all_patches, srcmap.filename.absolute, srcmap.start, srcmap.start, "", answer_processed)
create_patch(
all_patches, srcmap.filename.absolute, srcmap.start, srcmap.start, "", answer_processed
)

return overwrite

Expand Down
61 changes: 38 additions & 23 deletions slither/tools/flattening/flattening.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,9 @@ def _get_source_code(
:return:
"""
src_mapping = contract.source_mapping
# TODO: this needs to be encoded before it gets indexed!
src_bytes = self._compilation_unit.core.source_code[src_mapping.filename.absolute]
src_bytes = self._compilation_unit.core.source_code[src_mapping.filename.absolute].encode(
"utf8"
)

to_patch = []
# interface must use external
Expand All @@ -123,8 +124,8 @@ def _get_source_code(
+ f.parameters_src().source_mapping.length
)
attributes_end = f.returns_src().source_mapping.start
attributes = src_bytes[attributes_start:attributes_end]
regex = re.search(r"((\sexternal)\s+)|(\sexternal)$|(\)external)$", attributes.decode("utf8"))
attributes = src_bytes[attributes_start:attributes_end].decode("utf8")
regex = re.search(r"((\sexternal)\s+)|(\sexternal)$|(\)external)$", attributes)
if regex:
to_patch.append(
Patch(
Expand All @@ -133,7 +134,7 @@ def _get_source_code(
)
)
else:
raise SlitherException(f"External keyword not found {f.name} {attributes.decode("utf8")}")
raise SlitherException(f"External keyword not found {f.name} {attributes}")

for var in f.parameters:
if var.location == "calldata":
Expand All @@ -157,11 +158,11 @@ def _get_source_code(
+ f.parameters_src().source_mapping["length"]
)
attributes_end = f.returns_src().source_mapping["start"]
attributes = src_bytes[attributes_start:attributes_end]
attributes = src_bytes[attributes_start:attributes_end].decode("utf8")
regex = (
re.search(r"((\sexternal)\s+)|(\sexternal)$|(\)external)$", attributes.decode("utf8"))
re.search(r"((\sexternal)\s+)|(\sexternal)$|(\)external)$", attributes)
if visibility == "external"
else re.search(r"((\spublic)\s+)|(\spublic)$|(\)public)$", attributes.decode("utf8"))
else re.search(r"((\spublic)\s+)|(\spublic)$|(\)public)$", attributes)
)
if regex:
to_patch.append(
Expand All @@ -174,16 +175,16 @@ def _get_source_code(
)
else:
raise SlitherException(
f"{visibility} keyword not found {f.name} {attributes.decode("utf8")}"
f"{visibility} keyword not found {f.name} {attributes}"
)

if self._private_to_internal:
for variable in contract.state_variables_declared:
if variable.visibility == "private":
attributes_start = variable.source_mapping.start
attributes_end = attributes_start + variable.source_mapping.length
attributes = src_bytes[attributes_start:attributes_end]
regex = re.search(r" private ", attributes.decode("utf8"))
attributes = src_bytes[attributes_start:attributes_end].decode("utf8")
regex = re.search(r" private ", attributes)
if regex:
to_patch.append(
Patch(
Expand All @@ -193,7 +194,7 @@ def _get_source_code(
)
else:
raise SlitherException(
f"private keyword not found {variable.name} {attributes.decode("utf8")}"
f"private keyword not found {variable.name} {attributes}"
)

if self._remove_assert:
Expand All @@ -210,28 +211,42 @@ def _get_source_code(

to_patch.sort(key=lambda x: x.index, reverse=True)

# Note: foundry and solc and everything else return srcmap offsets per-byte
# and it seems the rest of slither operates on bytes also
# it might just be the mutator and flattener that are incorrectly applying offsets directly to strings
# I think I just need to do the following (and similar for mutations)
# content = content.encode("utf8")[start:end].decode("utf8")

content = src_mapping.content.encode("utf8")
start = src_mapping.start
for patch in to_patch:
patch_type = patch.patch_type
index = patch.index
index = index - start
if patch_type == "public_to_external":
content = content[:index].decode("utf8") + "public" + content[index + len("external") :].decode("utf8")
content = (
content[:index].decode("utf8")
+ "public"
+ content[index + len("external") :].decode("utf8")
)
elif patch_type == "external_to_internal":
content = content[:index].decode("utf8") + "internal" + content[index + len("external") :].decode("utf8")
content = (
content[:index].decode("utf8")
+ "internal"
+ content[index + len("external") :].decode("utf8")
)
elif patch_type == "public_to_internal":
content = content[:index].decode("utf8") + "internal" + content[index + len("public") :].decode("utf8")
content = (
content[:index].decode("utf8")
+ "internal"
+ content[index + len("public") :].decode("utf8")
)
elif patch_type == "private_to_internal":
content = content[:index].decode("utf8") + "internal" + content[index + len("private") :].decode("utf8")
content = (
content[:index].decode("utf8")
+ "internal"
+ content[index + len("private") :].decode("utf8")
)
elif patch_type == "calldata_to_memory":
content = content[:index].decode("utf8") + "memory" + content[index + len("calldata") :].decode("utf8")
content = (
content[:index].decode("utf8")
+ "memory"
+ content[index + len("calldata") :].decode("utf8")
)
else:
assert patch_type == "line_removal"
content = content[:index].decode("utf8") + " // " + content[index:].decode("utf8")
Expand Down

0 comments on commit 8dee964

Please sign in to comment.