slither.utils.arithmetic
1from typing import List, TYPE_CHECKING 2 3from slither.exceptions import SlitherException 4from slither.utils.integer_conversion import convert_string_to_fraction 5 6 7if TYPE_CHECKING: 8 from slither.core.declarations import Contract, Function 9 10# pylint: disable=too-many-branches 11def convert_subdenomination( 12 value: str, sub: str 13) -> int: # pylint: disable=too-many-return-statements 14 15 decimal_value = convert_string_to_fraction(value) 16 if sub == "wei": 17 return int(decimal_value) 18 if sub == "gwei": 19 return int(decimal_value * int(1e9)) 20 if sub == "szabo": 21 return int(decimal_value * int(1e12)) 22 if sub == "finney": 23 return int(decimal_value * int(1e15)) 24 if sub == "ether": 25 return int(decimal_value * int(1e18)) 26 if sub == "seconds": 27 return int(decimal_value) 28 if sub == "minutes": 29 return int(decimal_value * 60) 30 if sub == "hours": 31 return int(decimal_value * 60 * 60) 32 if sub == "days": 33 return int(decimal_value * 60 * 60 * 24) 34 if sub == "weeks": 35 return int(decimal_value * 60 * 60 * 24 * 7) 36 if sub == "years": 37 return int(decimal_value * 60 * 60 * 24 * 7 * 365) 38 39 raise SlitherException(f"Subdemonination conversion impossible {decimal_value} {sub}") 40 41 42# Number of unchecked arithmetic operation needed to be interesting 43THRESHOLD_ARITHMETIC_USAGE = 3 44 45 46def _unchecked_arithemtic_usage(function: "Function") -> bool: 47 """ 48 Check if the function has more than THRESHOLD_ARITHMETIC_USAGE unchecked arithmetic operation 49 50 Args: 51 function: 52 53 Returns: 54 55 """ 56 57 # pylint: disable=import-outside-toplevel 58 from slither.slithir.operations import Binary 59 60 score = 0 61 for node in function.nodes: 62 if not node.scope.is_checked: 63 for ir in node.irs: 64 if isinstance(ir, Binary): 65 score += 1 66 if score >= THRESHOLD_ARITHMETIC_USAGE: 67 return True 68 return False 69 70 71def unchecked_arithemtic_usage(contract: "Contract") -> List["Function"]: 72 """ 73 Return the list of function with some unchecked arithmetics 74 75 Args: 76 contract: 77 78 Returns: 79 80 """ 81 # pylint: disable=import-outside-toplevel 82 from slither.core.declarations import Function 83 84 ret: List[Function] = [] 85 for function in contract.all_functions_called: 86 if isinstance(function, Function) and _unchecked_arithemtic_usage(function): 87 ret.append(function) 88 return ret
def
convert_subdenomination(value: str, sub: str) -> int:
12def convert_subdenomination( 13 value: str, sub: str 14) -> int: # pylint: disable=too-many-return-statements 15 16 decimal_value = convert_string_to_fraction(value) 17 if sub == "wei": 18 return int(decimal_value) 19 if sub == "gwei": 20 return int(decimal_value * int(1e9)) 21 if sub == "szabo": 22 return int(decimal_value * int(1e12)) 23 if sub == "finney": 24 return int(decimal_value * int(1e15)) 25 if sub == "ether": 26 return int(decimal_value * int(1e18)) 27 if sub == "seconds": 28 return int(decimal_value) 29 if sub == "minutes": 30 return int(decimal_value * 60) 31 if sub == "hours": 32 return int(decimal_value * 60 * 60) 33 if sub == "days": 34 return int(decimal_value * 60 * 60 * 24) 35 if sub == "weeks": 36 return int(decimal_value * 60 * 60 * 24 * 7) 37 if sub == "years": 38 return int(decimal_value * 60 * 60 * 24 * 7 * 365) 39 40 raise SlitherException(f"Subdemonination conversion impossible {decimal_value} {sub}")
THRESHOLD_ARITHMETIC_USAGE =
3
def
unchecked_arithemtic_usage( contract: slither.core.declarations.contract.Contract) -> list[slither.core.declarations.function.Function]:
72def unchecked_arithemtic_usage(contract: "Contract") -> List["Function"]: 73 """ 74 Return the list of function with some unchecked arithmetics 75 76 Args: 77 contract: 78 79 Returns: 80 81 """ 82 # pylint: disable=import-outside-toplevel 83 from slither.core.declarations import Function 84 85 ret: List[Function] = [] 86 for function in contract.all_functions_called: 87 if isinstance(function, Function) and _unchecked_arithemtic_usage(function): 88 ret.append(function) 89 return ret
Return the list of function with some unchecked arithmetics
Args: contract:
Returns: