Skip to content

Commit

Permalink
remap offsets from bytes to chars during source map conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
bohendo committed Feb 5, 2025
1 parent cacf6f6 commit 4d7f623
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions slither/core/source_mapping/source_mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,16 +165,19 @@ def _convert_source_mapping(
if len(position) != 1:
return Source(compilation_unit)

s, l, f = position[0]
s = int(s)
l = int(l)
s_bytes, l_bytes, f = position[0]
s_bytes = int(s_bytes)
l_bytes = int(l_bytes)
f = int(f)

if f not in sourceUnits:
# TODO: figure out the filename so we can get the source code
# to remap bytes to chars.. When is this branch even used?
new_source = Source(compilation_unit)
new_source.start = s
new_source.length = l
new_source.start = s_bytes
new_source.length = l_bytes
return new_source

filename_used = sourceUnits[f]

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

# convert byte-offset indicies to char-offset
source_code = compilation_unit.core.source_code[filename.absolute]
s = int(len(source_code.encode("utf-8")[:s_bytes].decode("utf-8")))
l = int(len(source_code.encode("utf-8")[s_bytes:s_bytes + l_bytes].decode("utf-8")))
f = int(f)

(lines, starting_column, ending_column) = _compute_line(compilation_unit, filename, s, l)

new_source = Source(compilation_unit)
Expand Down

0 comments on commit 4d7f623

Please sign in to comment.