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[ux]: catch state modifying functions in range expr and loop iterator #3884

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 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
38 changes: 37 additions & 1 deletion tests/functional/syntax/test_for_range.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@
import pytest

from vyper import compiler
from vyper.exceptions import ArgumentException, StructureException, TypeMismatch, UnknownType
from vyper.exceptions import (
ArgumentException,
StateAccessViolation,
StructureException,
TypeMismatch,
UnknownType,
)

fail_list = [
(
Expand Down Expand Up @@ -322,6 +328,36 @@ def foo():
None,
"10.1",
),
(
"""
interface I:
def bar() -> uint256: payable

@external
def bar(t: address):
for i: uint256 in range(extcall I(t).bar(), bound=10):
pass
""",
StateAccessViolation,
"May not call state modifying function within a range expression.",
None,
"extcall I(t).bar()",
),
(
"""
interface I:
def bar() -> uint256: payable

@external
def bar(t: address):
for i: uint256 in range(1, extcall I(t).bar(), bound=10):
pass
""",
StateAccessViolation,
"May not call state modifying function within a range expression.",
None,
"extcall I(t).bar()",
),
]

for_code_regex = re.compile(r"for .+ in (.*):", re.DOTALL)
Expand Down
5 changes: 5 additions & 0 deletions vyper/semantics/analysis/local.py
Original file line number Diff line number Diff line change
Expand Up @@ -935,6 +935,11 @@ def _validate_range_call(node: vy_ast.Call):
start, end = (vy_ast.Int(value=0), node.args[0]) if len(node.args) == 1 else node.args
start, end = [i.get_folded_value() if i.has_folded_value else i for i in (start, end)]

if any(isinstance((extcall := n), vy_ast.ExtCall) for n in (start, end)):
raise StateAccessViolation(
"May not call state modifying function within a range expression.", extcall
)

if "bound" in kwargs:
bound = kwargs["bound"]
if bound.has_folded_value:
Expand Down
Loading