Skip to content
New issue

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

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

Already on GitHub? # to your account

fix[codegen]: range bound check for signed integers #3814

Merged
merged 4 commits into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
16 changes: 16 additions & 0 deletions tests/functional/codegen/features/iteration/test_for_range.py
Original file line number Diff line number Diff line change
Expand Up @@ -414,3 +414,19 @@ def foo(a: {typ}) -> {typ}:
assert c.foo(100) == 6
assert c.foo(1) == 666
assert c.foo(0) == 31337


def test_for_range_signed_int_overflow(get_contract, tx_failed):
code = """
@external
def foo() -> DynArray[int256, 10]:
res: DynArray[int256, 10] = empty(DynArray[int256, 10])
x:int256 = max_value(int256)
y:int256 = min_value(int256)+2
for i:int256 in range(x,y , bound=10):
res.append(i)
return res
"""
c = get_contract(code)
with tx_failed():
c.foo()
2 changes: 1 addition & 1 deletion vyper/codegen/stmt.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ def _parse_For_range(self):
with end.cache_when_complex("end") as (b1, end):
# note: the check for rounds<=rounds_bound happens in asm
# generation for `repeat`.
clamped_start = clamp("le", start, end)
clamped_start = clamp("sle" if target_type.is_signed else "le", start, end)
charles-cooper marked this conversation as resolved.
Show resolved Hide resolved
rounds = b1.resolve(IRnode.from_list(["sub", end, clamped_start]))
rounds_bound = kwargs.pop("bound").int_value()
else:
Expand Down
Loading