Skip to content

Commit 8dee964

Browse files
committed
fix formatting
1 parent b3a226e commit 8dee964

File tree

3 files changed

+46
-27
lines changed

3 files changed

+46
-27
lines changed

slither/core/source_mapping/source_mapping.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,8 @@ def content(self) -> str:
8484
assert self.compilation_unit
8585
return (
8686
self.compilation_unit.core.source_code[self.filename.absolute]
87-
.encode("utf8")[self.start : self.end]
88-
.decode("utf8")
87+
.encode("utf8")[self.start : self.end]
88+
.decode("utf8")
8989
)
9090

9191
@property

slither/tools/documentation/__main__.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,9 @@ def _handle_function(
156156
prompt = "Create a natpsec documentation for this solidity code with only notice and dev.\n"
157157
srcmap = function.source_mapping
158158
src = function.compilation_unit.core.source_code[srcmap.filename.absolute]
159-
first_char_index = len(src.encode("utf8")[:srcmap.start].decode("utf8")) # convert byte offset to char offset
159+
first_char_index = len(
160+
src.encode("utf8")[: srcmap.start].decode("utf8")
161+
) # convert byte offset to char offset
160162
prev_char = src[first_char_index - 1]
161163
prompt += srcmap.content
162164

@@ -201,7 +203,9 @@ def _handle_function(
201203
if not answer_processed:
202204
return overwrite
203205

204-
create_patch(all_patches, srcmap.filename.absolute, srcmap.start, srcmap.start, "", answer_processed)
206+
create_patch(
207+
all_patches, srcmap.filename.absolute, srcmap.start, srcmap.start, "", answer_processed
208+
)
205209

206210
return overwrite
207211

slither/tools/flattening/flattening.py

Lines changed: 38 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,9 @@ def _get_source_code(
107107
:return:
108108
"""
109109
src_mapping = contract.source_mapping
110-
# TODO: this needs to be encoded before it gets indexed!
111-
src_bytes = self._compilation_unit.core.source_code[src_mapping.filename.absolute]
110+
src_bytes = self._compilation_unit.core.source_code[src_mapping.filename.absolute].encode(
111+
"utf8"
112+
)
112113

113114
to_patch = []
114115
# interface must use external
@@ -123,8 +124,8 @@ def _get_source_code(
123124
+ f.parameters_src().source_mapping.length
124125
)
125126
attributes_end = f.returns_src().source_mapping.start
126-
attributes = src_bytes[attributes_start:attributes_end]
127-
regex = re.search(r"((\sexternal)\s+)|(\sexternal)$|(\)external)$", attributes.decode("utf8"))
127+
attributes = src_bytes[attributes_start:attributes_end].decode("utf8")
128+
regex = re.search(r"((\sexternal)\s+)|(\sexternal)$|(\)external)$", attributes)
128129
if regex:
129130
to_patch.append(
130131
Patch(
@@ -133,7 +134,7 @@ def _get_source_code(
133134
)
134135
)
135136
else:
136-
raise SlitherException(f"External keyword not found {f.name} {attributes.decode("utf8")}")
137+
raise SlitherException(f"External keyword not found {f.name} {attributes}")
137138

138139
for var in f.parameters:
139140
if var.location == "calldata":
@@ -157,11 +158,11 @@ def _get_source_code(
157158
+ f.parameters_src().source_mapping["length"]
158159
)
159160
attributes_end = f.returns_src().source_mapping["start"]
160-
attributes = src_bytes[attributes_start:attributes_end]
161+
attributes = src_bytes[attributes_start:attributes_end].decode("utf8")
161162
regex = (
162-
re.search(r"((\sexternal)\s+)|(\sexternal)$|(\)external)$", attributes.decode("utf8"))
163+
re.search(r"((\sexternal)\s+)|(\sexternal)$|(\)external)$", attributes)
163164
if visibility == "external"
164-
else re.search(r"((\spublic)\s+)|(\spublic)$|(\)public)$", attributes.decode("utf8"))
165+
else re.search(r"((\spublic)\s+)|(\spublic)$|(\)public)$", attributes)
165166
)
166167
if regex:
167168
to_patch.append(
@@ -174,16 +175,16 @@ def _get_source_code(
174175
)
175176
else:
176177
raise SlitherException(
177-
f"{visibility} keyword not found {f.name} {attributes.decode("utf8")}"
178+
f"{visibility} keyword not found {f.name} {attributes}"
178179
)
179180

180181
if self._private_to_internal:
181182
for variable in contract.state_variables_declared:
182183
if variable.visibility == "private":
183184
attributes_start = variable.source_mapping.start
184185
attributes_end = attributes_start + variable.source_mapping.length
185-
attributes = src_bytes[attributes_start:attributes_end]
186-
regex = re.search(r" private ", attributes.decode("utf8"))
186+
attributes = src_bytes[attributes_start:attributes_end].decode("utf8")
187+
regex = re.search(r" private ", attributes)
187188
if regex:
188189
to_patch.append(
189190
Patch(
@@ -193,7 +194,7 @@ def _get_source_code(
193194
)
194195
else:
195196
raise SlitherException(
196-
f"private keyword not found {variable.name} {attributes.decode("utf8")}"
197+
f"private keyword not found {variable.name} {attributes}"
197198
)
198199

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

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

213-
# Note: foundry and solc and everything else return srcmap offsets per-byte
214-
# and it seems the rest of slither operates on bytes also
215-
# it might just be the mutator and flattener that are incorrectly applying offsets directly to strings
216-
# I think I just need to do the following (and similar for mutations)
217-
# content = content.encode("utf8")[start:end].decode("utf8")
218-
219214
content = src_mapping.content.encode("utf8")
220215
start = src_mapping.start
221216
for patch in to_patch:
222217
patch_type = patch.patch_type
223218
index = patch.index
224219
index = index - start
225220
if patch_type == "public_to_external":
226-
content = content[:index].decode("utf8") + "public" + content[index + len("external") :].decode("utf8")
221+
content = (
222+
content[:index].decode("utf8")
223+
+ "public"
224+
+ content[index + len("external") :].decode("utf8")
225+
)
227226
elif patch_type == "external_to_internal":
228-
content = content[:index].decode("utf8") + "internal" + content[index + len("external") :].decode("utf8")
227+
content = (
228+
content[:index].decode("utf8")
229+
+ "internal"
230+
+ content[index + len("external") :].decode("utf8")
231+
)
229232
elif patch_type == "public_to_internal":
230-
content = content[:index].decode("utf8") + "internal" + content[index + len("public") :].decode("utf8")
233+
content = (
234+
content[:index].decode("utf8")
235+
+ "internal"
236+
+ content[index + len("public") :].decode("utf8")
237+
)
231238
elif patch_type == "private_to_internal":
232-
content = content[:index].decode("utf8") + "internal" + content[index + len("private") :].decode("utf8")
239+
content = (
240+
content[:index].decode("utf8")
241+
+ "internal"
242+
+ content[index + len("private") :].decode("utf8")
243+
)
233244
elif patch_type == "calldata_to_memory":
234-
content = content[:index].decode("utf8") + "memory" + content[index + len("calldata") :].decode("utf8")
245+
content = (
246+
content[:index].decode("utf8")
247+
+ "memory"
248+
+ content[index + len("calldata") :].decode("utf8")
249+
)
235250
else:
236251
assert patch_type == "line_removal"
237252
content = content[:index].decode("utf8") + " // " + content[index:].decode("utf8")

0 commit comments

Comments
 (0)