Skip to content

Commit 3404b76

Browse files
committed
remap offsets from bytes to chars during source map conversion
1 parent f6413e6 commit 3404b76

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

slither/core/source_mapping/source_mapping.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -165,16 +165,19 @@ def _convert_source_mapping(
165165
if len(position) != 1:
166166
return Source(compilation_unit)
167167

168-
s, l, f = position[0]
169-
s = int(s)
170-
l = int(l)
168+
s_bytes, l_bytes, f = position[0]
169+
s_bytes = int(s_bytes)
170+
l_bytes = int(l_bytes)
171171
f = int(f)
172172

173173
if f not in sourceUnits:
174+
# TODO: figure out the filename so we can get the source code
175+
# to remap bytes to chars.. When is this branch even used?
174176
new_source = Source(compilation_unit)
175-
new_source.start = s
176-
new_source.length = l
177+
new_source.start = s_bytes
178+
new_source.length = l_bytes
177179
return new_source
180+
178181
filename_used = sourceUnits[f]
179182

180183
# If possible, convert the filename to its absolute/relative version
@@ -183,6 +186,12 @@ def _convert_source_mapping(
183186
filename: Filename = compilation_unit.core.crytic_compile.filename_lookup(filename_used)
184187
is_dependency = compilation_unit.core.crytic_compile.is_dependency(filename.absolute)
185188

189+
# convert byte-offset indicies to char-offset
190+
source_code = compilation_unit.core.source_code[filename.absolute]
191+
s = int(len(source_code.encode("utf-8")[:s_bytes].decode("utf-8")))
192+
l = int(len(source_code.encode("utf-8")[s_bytes : s_bytes + l_bytes].decode("utf-8")))
193+
f = int(f)
194+
186195
(lines, starting_column, ending_column) = _compute_line(compilation_unit, filename, s, l)
187196

188197
new_source = Source(compilation_unit)

0 commit comments

Comments
 (0)