Skip to content
Open
Show file tree
Hide file tree
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
10 changes: 8 additions & 2 deletions slither/utils/type.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ def _convert_type_for_solidity_signature_to_string(
# While having an array of a struct of two uint leads to a (uint, uint)[] signature
if isinstance(types, ArrayType):
underlying_type = convert_type_for_solidity_signature(types.type, seen)
# Descending through the array adds its element type to the ancestor set, so a
# struct that reaches itself via an array still terminates.
underlying_type_str = _convert_type_for_solidity_signature_to_string(
underlying_type, seen
underlying_type, seen | {types.type}
)

if types.length is None:
Expand Down Expand Up @@ -70,9 +72,13 @@ def convert_type_for_solidity_signature(t: Type, seen: set[Type]) -> Type | list
# function f(St memory s) internal{}
#
# }
# `seen` tracks the ancestors of `t` in the current descent, not every type visited
# anywhere in the traversal. Mutating a single shared set would make a type that appears
# twice as a *sibling* (e.g. two fields of the same user defined type) look like a
# recursive back-edge, and the second occurrence would be returned unconverted.
if t in seen:
return t
seen.add(t)
seen = seen | {t}

if isinstance(t, UserDefinedType):
underlying_type = t.type
Expand Down
36 changes: 36 additions & 0 deletions tests/unit/utils/test_data/type_helpers_repeated_fields.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

type Amount is uint256;

enum Flag {
A,
B
}

interface IThing {}

// Each struct holds the same convertible type twice. The second occurrence must be
// converted just like the first one -- it is a sibling, not a recursive back-edge.
struct WithAlias {
Amount a;
Amount b;
}

struct WithEnum {
Flag a;
Flag b;
}

struct WithContract {
IThing a;
IThing b;
}

contract B {
function fAlias(WithAlias calldata p) external {}

function fEnum(WithEnum calldata p) external {}

function fContract(WithContract calldata p) external {}
}
14 changes: 14 additions & 0 deletions tests/unit/utils/test_type_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,17 @@ def test_function_id_rec_structure(solc_binary_path) -> None:
for compilation_unit in slither.compilation_units:
for function in compilation_unit.functions:
assert function.solidity_signature


def test_solidity_signature_repeated_struct_field_types(solc_binary_path) -> None:
"""A type used by two fields of the same struct must be converted for both of them."""
solc_path = solc_binary_path("0.8.20")
slither = Slither(
Path(TEST_DATA_DIR, "type_helpers_repeated_fields.sol").as_posix(), solc=solc_path
)
contract = slither.get_contract_from_name("B")[0]
signatures = {f.name: f.solidity_signature for f in contract.functions_entry_points}

assert signatures["fAlias"] == "fAlias((uint256,uint256))"
assert signatures["fEnum"] == "fEnum((uint8,uint8))"
assert signatures["fContract"] == "fContract((address,address))"