From f4e9cd5fbf241f40da6abf694df5dc264347b51b Mon Sep 17 00:00:00 2001 From: Josselin Date: Tue, 24 Nov 2020 13:38:45 +0100 Subject: [PATCH 1/3] Use get_line_from_offset from crytic_compile (a75e2e816972c50a9e94d47a33394dfceda31c0a) --- slither/core/source_mapping/source_mapping.py | 66 ++++++------------- 1 file changed, 20 insertions(+), 46 deletions(-) diff --git a/slither/core/source_mapping/source_mapping.py b/slither/core/source_mapping/source_mapping.py index 9cafc8643..80156220a 100644 --- a/slither/core/source_mapping/source_mapping.py +++ b/slither/core/source_mapping/source_mapping.py @@ -1,64 +1,44 @@ import re -from typing import Dict, Union, Optional +from typing import Dict, Union, Optional, List, Tuple from slither.core.context.context import Context + + class SourceMapping(Context): def __init__(self): super().__init__() # TODO create a namedtuple for the source mapping rather than a dict self._source_mapping: Optional[Dict] = None + self._start: Optional[int] = None + self._length: Optional[int] = None + self._filename_used: Optional[str] = None + self._filename_relative: Optional[str] = None + self._filename_absolute: Optional[str] = None + self._filename_short: Optional[str] = None + self._is_dependency: Optional[bool] = None + self._lines: Optional[List[int]] = None + self._starting_column: Optional[int] = None + self._ending_column: Optional[int] = None @property def source_mapping(self) -> Optional[Dict]: return self._source_mapping @staticmethod - def _compute_line(source_code, start, length): + def _compute_line(slither, filename, start: int, length: int) -> Tuple[List[int], int, int]: """ Compute line(s) numbers and starting/ending columns from a start/end offset. All numbers start from 1. Not done in an efficient way """ - source_code = source_code.encode("utf-8") - total_length = len(source_code) - source_code = source_code.splitlines(True) - counter = 0 - i = 0 - lines = [] - starting_column = None - ending_column = None - while counter < total_length: - # Determine the length of the line, and advance the line number - line_content = source_code[i] - line_length = len(line_content) - i = i + 1 - - # Determine our column numbers. - if starting_column is None and counter + line_length > start: - starting_column = (start - counter) + 1 - if ( - starting_column is not None - and ending_column is None - and counter + line_length > start + length - ): - ending_column = ((start + length) - counter) + 1 - - # Advance the current position counter, and determine line numbers. - counter += line_length - if counter > start: - lines.append(i) + start_line, starting_column = slither.crytic_compile.get_line_from_offset(filename, start) + end_line, ending_column = slither.crytic_compile.get_line_from_offset(filename, start+length) + return list(range(start_line, end_line+1)), starting_column, ending_column - # If our advanced position for the next line is out of range, stop. - if counter > start + length: - break - - return lines, starting_column, ending_column - - @staticmethod - def _convert_source_mapping(offset: str, slither): # pylint: disable=too-many-locals + def _convert_source_mapping(self, offset: str, slither): # pylint: disable=too-many-locals """ Convert a text offset to a real offset see https://solidity.readthedocs.io/en/develop/miscellaneous.html#source-mappings @@ -85,8 +65,6 @@ class SourceMapping(Context): is_dependency = False - lines = [] - # If possible, convert the filename to its absolute/relative version if slither.crytic_compile: filenames = slither.crytic_compile.filename_lookup(filename_used) @@ -110,12 +88,8 @@ class SourceMapping(Context): else: filename = filename_used - if slither.crytic_compile and filename in slither.crytic_compile.src_content: - source_code = slither.crytic_compile.src_content[filename] - (lines, starting_column, ending_column) = SourceMapping._compute_line(source_code, s, l) - elif filename in slither.source_code: - source_code = slither.source_code[filename] - (lines, starting_column, ending_column) = SourceMapping._compute_line(source_code, s, l) + if slither.crytic_compile: + (lines, starting_column, ending_column) = self._compute_line(slither, filename, s, l) else: (lines, starting_column, ending_column) = ([], None, None) From 448374ee3095d939533cffbe84ab22f54af82ec0 Mon Sep 17 00:00:00 2001 From: Josselin Date: Tue, 24 Nov 2020 14:17:40 +0100 Subject: [PATCH 2/3] black --- slither/core/source_mapping/source_mapping.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/slither/core/source_mapping/source_mapping.py b/slither/core/source_mapping/source_mapping.py index 80156220a..72bc5c0ff 100644 --- a/slither/core/source_mapping/source_mapping.py +++ b/slither/core/source_mapping/source_mapping.py @@ -4,8 +4,6 @@ from typing import Dict, Union, Optional, List, Tuple from slither.core.context.context import Context - - class SourceMapping(Context): def __init__(self): super().__init__() @@ -35,8 +33,10 @@ class SourceMapping(Context): Not done in an efficient way """ start_line, starting_column = slither.crytic_compile.get_line_from_offset(filename, start) - end_line, ending_column = slither.crytic_compile.get_line_from_offset(filename, start+length) - return list(range(start_line, end_line+1)), starting_column, ending_column + end_line, ending_column = slither.crytic_compile.get_line_from_offset( + filename, start + length + ) + return list(range(start_line, end_line + 1)), starting_column, ending_column def _convert_source_mapping(self, offset: str, slither): # pylint: disable=too-many-locals """ From 72c0a1b8d4061f17d721b1acd667f6850717a506 Mon Sep 17 00:00:00 2001 From: Josselin Date: Tue, 24 Nov 2020 14:59:00 +0100 Subject: [PATCH 3/3] Minor --- slither/core/source_mapping/source_mapping.py | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/slither/core/source_mapping/source_mapping.py b/slither/core/source_mapping/source_mapping.py index 72bc5c0ff..943b82c83 100644 --- a/slither/core/source_mapping/source_mapping.py +++ b/slither/core/source_mapping/source_mapping.py @@ -9,16 +9,16 @@ class SourceMapping(Context): super().__init__() # TODO create a namedtuple for the source mapping rather than a dict self._source_mapping: Optional[Dict] = None - self._start: Optional[int] = None - self._length: Optional[int] = None - self._filename_used: Optional[str] = None - self._filename_relative: Optional[str] = None - self._filename_absolute: Optional[str] = None - self._filename_short: Optional[str] = None - self._is_dependency: Optional[bool] = None - self._lines: Optional[List[int]] = None - self._starting_column: Optional[int] = None - self._ending_column: Optional[int] = None + # self._start: Optional[int] = None + # self._length: Optional[int] = None + # self._filename_used: Optional[str] = None + # self._filename_relative: Optional[str] = None + # self._filename_absolute: Optional[str] = None + # self._filename_short: Optional[str] = None + # self._is_dependency: Optional[bool] = None + # self._lines: Optional[List[int]] = None + # self._starting_column: Optional[int] = None + # self._ending_column: Optional[int] = None @property def source_mapping(self) -> Optional[Dict]: