Skip to content

Commit aa322d9

Browse files
authored
Merge pull request #2693 from crytic/dev-fix-typealias-inherited
Propagate type aliases from base to derived contracts
2 parents ec5b8d8 + 28269c4 commit aa322d9

File tree

5 files changed

+35
-7
lines changed

5 files changed

+35
-7
lines changed

Diff for: slither/solc_parsing/declarations/contract.py

+12-5
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def __init__(
5353
self._enumsNotParsed: List[Dict] = []
5454
self._structuresNotParsed: List[Dict] = []
5555
self._usingForNotParsed: List[Dict] = []
56-
self._customErrorParsed: List[Dict] = []
56+
self._customErrorsNotParsed: List[Dict] = []
5757

5858
self._functions_parser: List[FunctionSolc] = []
5959
self._modifiers_parser: List[ModifierSolc] = []
@@ -277,13 +277,20 @@ def _parse_contract_items(self) -> None:
277277
elif item[self.get_key()] == "UsingForDirective":
278278
self._usingForNotParsed.append(item)
279279
elif item[self.get_key()] == "ErrorDefinition":
280-
self._customErrorParsed.append(item)
280+
self._customErrorsNotParsed.append(item)
281281
elif item[self.get_key()] == "UserDefinedValueTypeDefinition":
282282
self._parse_type_alias(item)
283283
else:
284284
raise ParsingError("Unknown contract item: " + item[self.get_key()])
285285
return
286286

287+
def parse_type_alias(self) -> None:
288+
# We keep parse_ in the name just to keep the naming convention even if we already parsed them initially.
289+
# Here we only update the current contract type_aliases_as_dict with the fathers' values
290+
# It's useful to keep using the same pattern anyway as we know all the fathers have been analyzed
291+
for father in self._contract.inheritance_reverse:
292+
self._contract.type_aliases_as_dict.update(father.type_aliases_as_dict)
293+
287294
def _parse_type_alias(self, item: Dict) -> None:
288295
assert "name" in item
289296
assert "underlyingType" in item
@@ -337,9 +344,9 @@ def parse_custom_errors(self) -> None:
337344
for father in self._contract.inheritance_reverse:
338345
self._contract.custom_errors_as_dict.update(father.custom_errors_as_dict)
339346

340-
for custom_error in self._customErrorParsed:
347+
for custom_error in self._customErrorsNotParsed:
341348
self._parse_custom_error(custom_error)
342-
self._customErrorParsed = []
349+
self._customErrorsNotParsed = []
343350

344351
def parse_state_variables(self) -> None:
345352
for father in self._contract.inheritance_reverse:
@@ -793,7 +800,7 @@ def delete_content(self) -> None:
793800
self._enumsNotParsed = []
794801
self._structuresNotParsed = []
795802
self._usingForNotParsed = []
796-
self._customErrorParsed = []
803+
self._customErrorsNotParsed = []
797804

798805
def _handle_comment(self, attributes: Dict) -> None:
799806
"""

Diff for: slither/solc_parsing/slither_compilation_unit_solc.py

+1
Original file line numberDiff line numberDiff line change
@@ -730,6 +730,7 @@ def _parse_struct_var_modifiers_functions(self, contract: ContractSolc) -> None:
730730
contract.parse_modifiers()
731731
contract.parse_functions()
732732
contract.parse_custom_errors()
733+
contract.parse_type_alias()
733734
contract.set_is_analyzed(True)
734735

735736
def _analyze_struct_events(self, contract: ContractSolc) -> None:
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
{
22
"OtherTest": {
3-
"myfunc()": "digraph{\n0[label=\"Node Type: ENTRY_POINT 0\n\"];\n0->1;\n1[label=\"Node Type: NEW VARIABLE 1\n\"];\n}\n"
3+
"myfunc()": "digraph{\n0[label=\"Node Type: ENTRY_POINT 0\n\"];\n0->1;\n1[label=\"Node Type: NEW VARIABLE 1\n\"];\n1->2;\n2[label=\"Node Type: NEW VARIABLE 2\n\"];\n}\n"
44
},
5-
"DeleteTest": {}
5+
"DeleteTest": {},
6+
"A": {
7+
"q()": "digraph{\n0[label=\"Node Type: ENTRY_POINT 0\n\"];\n0->1;\n1[label=\"Node Type: EXPRESSION 1\n\"];\n}\n"
8+
},
9+
"B": {
10+
"q()": "digraph{\n0[label=\"Node Type: ENTRY_POINT 0\n\"];\n0->1;\n1[label=\"Node Type: EXPRESSION 1\n\"];\n}\n",
11+
"y()": "digraph{\n0[label=\"Node Type: ENTRY_POINT 0\n\"];\n0->1;\n1[label=\"Node Type: RETURN 1\n\"];\n}\n"
12+
}
613
}

Diff for: tests/e2e/solc_parsing/test_data/type-aliases.sol

+13
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,16 @@ contract OtherTest {
2121
contract DeleteTest {
2222
type Z is int;
2323
}
24+
25+
contract A {
26+
type MyU is uint256;
27+
function q() public returns(MyU) {
28+
MyU.wrap(1);
29+
}
30+
}
31+
32+
contract B is A {
33+
function y() public returns(MyU) {
34+
return MyU.wrap(343);
35+
}
36+
}

0 commit comments

Comments
 (0)