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 5 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
28 changes: 28 additions & 0 deletions tests/parser/syntax/test_for_range.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import pytest

from vyper import compiler
from vyper.exceptions import StateAccessViolation

valid_list = [
"""
Expand Down Expand Up @@ -37,3 +38,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
""",
StateAccessViolation,
)
]


@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)
7 changes: 7 additions & 0 deletions vyper/codegen/expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
from vyper.evm.opcodes import version_check
from vyper.exceptions import (
EvmVersionException,
StateAccessViolation,
StructureException,
TypeCheckFailure,
TypeMismatch,
Expand Down Expand Up @@ -670,6 +671,12 @@ def parse_Call(self):
darray = Expr(self.expr.func.value, self.context).ir_node
assert len(self.expr.args) == 0
assert isinstance(darray.typ, DArrayType)

if self.context.is_constant():
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i don't think this is the right place to do this

raise StateAccessViolation(
f"May not call `pop()` within {self.context.pp_constancy()}", self.expr
)

return pop_dyn_array(darray, return_popped_item=True)

elif (
Expand Down