Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: guard against pop in iter for range #3188

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
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
27 changes: 27 additions & 0 deletions tests/parser/syntax/test_for_range.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,30 @@ def kick_foos():
@pytest.mark.parametrize("good_code", valid_list)
def test_range_success(good_code):
assert compiler.compile_code(good_code) is not None


fail_list = [
# Cannot call `pop()` in for range because it modifies state
(
"""
arr: DynArray[uint256, 10]

@external
def test()-> (DynArray[uint256, 6], DynArray[uint256, 10]):
b: DynArray[uint256, 6] = []

self.arr = [1,0]

for i in range(self.arr.pop(), self.arr.pop() + 2):
b.append(i)

return b, self.arr
""",
ImmutableViolation,
)
]


@pytest.mark.parametrize("bad_code,exc", fail_list)
def test_range_fail(assert_compile_failed, get_contract_with_gas_estimation, bad_code, exc):
assert_compile_failed(lambda: get_contract_with_gas_estimation(bad_code), exc)
9 changes: 9 additions & 0 deletions vyper/semantics/analysis/local.py
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,15 @@ def visit_For(self, node):
)
validate_call_args(node.iter, (1, 2))

# Check if pop` operation is used in `range`
darray_pop_nodes = node.iter.get_descendants(vy_ast.Attribute, {"attr": "pop"})
if len(darray_pop_nodes) > 0:
pop_node = darray_pop_nodes.pop()
raise ImmutableViolation(
"Cannot call `pop` inside for loop because it modifies the dynamic array",
pop_node,
)

args = node.iter.args
if len(args) == 1:
# range(CONSTANT)
Expand Down