From 3cf394e4b89dbdc6c28d865f8d2b730914e266cd Mon Sep 17 00:00:00 2001 From: webthethird Date: Thu, 18 May 2023 14:02:26 -0500 Subject: [PATCH] Add more test cases to test_implicit_returns.py --- tests/unit/slithir/test_implicit_returns.py | 68 ++++++++++++++++++++- 1 file changed, 67 insertions(+), 1 deletion(-) diff --git a/tests/unit/slithir/test_implicit_returns.py b/tests/unit/slithir/test_implicit_returns.py index 3ad1ef30b..052457ebb 100644 --- a/tests/unit/slithir/test_implicit_returns.py +++ b/tests/unit/slithir/test_implicit_returns.py @@ -10,7 +10,7 @@ from slither.slithir.operations import ( ) -def test_named_return_with_explicit_return(slither_from_source) -> None: +def test_with_explicit_return(slither_from_source) -> None: source = """ contract Contract { function foo(int x) public returns (int y) { @@ -40,6 +40,72 @@ def test_named_return_with_explicit_return(slither_from_source) -> None: assert node_ret.irs[0].values[0] == f.get_local_variable_from_name("y") +def test_return_multiple_with_struct(slither_from_source) -> None: + source = """ + struct St { + uint256 value; + } + + contract Contract { + function foo(St memory x) public returns (St memory y, uint256 z) { + z = x.value; + y = St({value: z + 1}); + } + } + """ + with slither_from_source(source) as slither: + c: Contract = slither.get_contract_from_name("Contract")[0] + f: Function = c.functions[0] + assert len(f.nodes) == 4 + node = f.nodes[3] + assert node.type == NodeType.RETURN + assert isinstance(node_true.irs[0], Return) + assert node_true.irs[0].values[0] == f.get_local_variable_from_name("y") + assert node_true.irs[0].values[1] == f.get_local_variable_from_name("z") + + +def test_nested_ifs_with_loop(slither_from_source) -> None: + source = """ + contract Contract { + function foo(uint a) public returns (uint x) { + x = a; + if(a == 1) { + return a; + } else { + for (uint i = 0; i < 10; i++) { + if (x > 10) { + if (a < 0) { + x = 10 * x; + } else { + throw; + } + } else { + x++; + } + } + } + } + } + """ + with slither_from_source(source) as slither: + c: Contract = slither.get_contract_from_name("Contract")[0] + f: Function = c.functions[0] + node_if = f.nodes[2] + assert node_if.son_true.type == NodeType.RETURN + node_explicit = node_if.son_true + assert isinstance(node_explicit.irs[0], Return) + assert node_explicit.irs[0].values[0] == f.get_local_variable_from_name("a") + node_end_if = f.nodes[16] + assert node_end_if.type == NodeType.ENDIF + assert node_end_if.sons[0].type == NodeType.RETURN + node_implicit = node_end_if.sons[0] + assert isinstance(node_implicit.irs[0], Return) + assert node_implicit.irs[0].values[0] == f.get_local_variable_from_name("x") + node_throw = f.nodes[11] + assert node_throw.type == NodeType.THROW + assert len(node_throw.sons) == 0 + + def test_issue_1846_ternary_in_ternary(slither_from_source): source = """ contract Contract {