Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remap offsets from bytes to chars during source map conversion #2661

Closed
wants to merge 2 commits into from
Closed
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
26 changes: 20 additions & 6 deletions slither/core/source_mapping/source_mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,10 @@ def _compute_line(
return list(range(start_line, end_line + 1)), starting_column, ending_column


# pylint: disable=too-many-locals
def _convert_source_mapping(
offset: str, compilation_unit: "SlitherCompilationUnit"
) -> Source: # pylint: disable=too-many-locals
) -> Source:
Comment on lines 154 to +156
Copy link
Contributor

Choose a reason for hiding this comment

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

[blackfmt] reported by reviewdog 🐶

Suggested change
def _convert_source_mapping(
offset: str, compilation_unit: "SlitherCompilationUnit"
) -> Source: # pylint: disable=too-many-locals
) -> Source:
def _convert_source_mapping(offset: str, compilation_unit: "SlitherCompilationUnit") -> Source:

"""
Convert a text offset to a real offset
see https://solidity.readthedocs.io/en/develop/miscellaneous.html#source-mappings
Expand All @@ -165,16 +166,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 +187,16 @@ 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)

if filename.absolute in compilation_unit.core.source_code:
# 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")))
else:
# no source code is available, hopefully the source code doesn't contain unicode
s = int(s_bytes)
l = int(l_bytes)

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

new_source = Source(compilation_unit)
Expand Down
Loading