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[lang]: fix array index checks when the subscript is folded #3924

Merged
Merged
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
22 changes: 22 additions & 0 deletions tests/unit/ast/nodes/test_fold_subscript.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
from hypothesis import strategies as st

from tests.utils import parse_and_fold
from vyper.compiler import compile_code
from vyper.exceptions import ArrayIndexException


@pytest.mark.fuzzing
Expand All @@ -24,3 +26,23 @@ def foo(array: int128[10], idx: uint256) -> int128:
new_node = old_node.get_folded_value()

assert contract.foo(array, idx) == new_node.value


def test_negative_index():
source = """
@external
def foo(array: int128[10]) -> int128:
return array[0 - 1]
"""
with pytest.raises(ArrayIndexException):
compile_code(source)


def test_oob_index():
source = """
@external
def foo(array: int128[10]) -> int128:
return array[9 + 1]
"""
with pytest.raises(ArrayIndexException):
compile_code(source)
3 changes: 3 additions & 0 deletions vyper/semantics/types/subscriptable.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,9 @@ def validate_index_type(self, node):
# TODO break this cycle
from vyper.semantics.analysis.utils import validate_expected_type

if node.has_folded_value:
node = node.get_folded_value()

if isinstance(node, vy_ast.Int):
if node.value < 0:
raise ArrayIndexException("Vyper does not support negative indexing", node)
Expand Down
Loading