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[codegen]: bounds check for signed index accesses #3817

Merged
10 changes: 4 additions & 6 deletions vyper/codegen/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -567,12 +567,10 @@ def _get_element_ptr_array(parent, key, array_bounds_check):
if array_bounds_check:
is_darray = isinstance(parent.typ, DArrayT)
bound = get_dyn_array_count(parent) if is_darray else parent.typ.count
# uclamplt works, even for signed ints. since two's-complement
# is used, if the index is negative, (unsigned) LT will interpret
# it as a very large number, larger than any practical value for
# an array index, and the clamp will throw an error.
# NOTE: there are optimization rules for this when ix or bound is literal
ix = clamp("lt", ix, bound)
# NOTE: there are optimization rules for the bounds check when
# ix or bound is literal. there is also an optimization rule to
# optimize out ix>=0 when ix is unsigned.
ix = clamp2(ix, 0, bound)
ix.set_error_msg(f"{parent.typ} bounds check")

if parent.encoding == Encoding.ABI:
Expand Down
Loading