Skip to content

Commit

Permalink
Fix custom error signature lookback (crytic#1156)
Browse files Browse the repository at this point in the history
  • Loading branch information
montyly authored Apr 5, 2022
1 parent 7dde5fe commit 3af6616
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
7 changes: 6 additions & 1 deletion slither/core/declarations/custom_error.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,12 @@ def solidity_signature(self) -> str:
Contract and converted into address
:return: the solidity signature
"""
assert self._solidity_signature is not None
# Ideally this should be an assert
# But due to a logic limitation in the solc parsing (find_variable)
# We need to raise an error if the custom error sig was not yet built
# (set_solidity_sig was not called before find_variable)
if self._solidity_signature is None:
raise ValueError("Custom Error not yet built")
return self._solidity_signature

def set_solidity_sig(self) -> None:
Expand Down
24 changes: 18 additions & 6 deletions slither/solc_parsing/expressions/find_variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,15 @@ def _find_top_level(
# For example, a top variable that use another top level variable
# IF more top level objects are added to Solidity, we have to be careful with the order of the lookup
# in this function
for custom_error in scope.custom_errors:
if custom_error.solidity_signature == var_name:
return custom_error, False
try:
for custom_error in scope.custom_errors:
if custom_error.solidity_signature == var_name:
return custom_error, False
except ValueError:
# This can happen as custom error sol signature might not have been built
# when find_variable was called
# TODO refactor find_variable to prevent this from happening
pass

return None, False

Expand Down Expand Up @@ -208,9 +214,15 @@ def _find_in_contract(
# This is because when the dic is populated the underlying object is not yet parsed
# As a result, we need to iterate over all the custom errors here instead of using the dict
custom_errors = contract.custom_errors
for custom_error in custom_errors:
if var_name == custom_error.solidity_signature:
return custom_error
try:
for custom_error in custom_errors:
if var_name == custom_error.solidity_signature:
return custom_error
except ValueError:
# This can happen as custom error sol signature might not have been built
# when find_variable was called
# TODO refactor find_variable to prevent this from happening
pass

# If the enum is refered as its name rather than its canonicalName
enums = {e.name: e for e in contract.enums}
Expand Down

0 comments on commit 3af6616

Please sign in to comment.