Skip to content

Commit

Permalink
fix[ux]: fix false positive for overflow in type checker (#4385)
Browse files Browse the repository at this point in the history
this commit fixes a false positive for integer overflow in
the typechecker involving nested pow operations by filtering
`OverflowException` in `_validate_op`. the previous code assumed that
`validate_numeric_op` could throw anything besides `InvalidOperation`,
but for the `Pow` binop, it can throw `OverflowException`.

---------

Co-authored-by: Charles Cooper <[email protected]>
  • Loading branch information
tserg and charles-cooper authored Dec 9, 2024
1 parent c8691ac commit 12ab491
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
14 changes: 14 additions & 0 deletions tests/functional/codegen/types/numbers/test_exponents.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,3 +173,17 @@ def foo(b: int128) -> int128:
c.foo(max_power)
with tx_failed():
c.foo(max_power + 1)


valid_list = [
"""
@external
def foo() -> uint256:
return (10**18)**2
"""
]


@pytest.mark.parametrize("good_code", valid_list)
def test_exponent_success(good_code):
assert compile_code(good_code) is not None
2 changes: 1 addition & 1 deletion vyper/semantics/analysis/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def _validate_op(node, types_list, validation_fn_name):
try:
_validate_fn(node)
ret.append(type_)
except InvalidOperation as e:
except (InvalidOperation, OverflowException) as e:
err_list.append(e)

if ret:
Expand Down
4 changes: 2 additions & 2 deletions vyper/semantics/types/primitives.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,11 +173,11 @@ def _get_lr():
if isinstance(left, vy_ast.Int):
if left.value >= 2**value_bits:
raise OverflowException(
"Base is too large, calculation will always overflow", left
f"Base is too large for {self}, calculation will always overflow", left
)
elif left.value < -(2**value_bits):
raise OverflowException(
"Base is too small, calculation will always underflow", left
f"Base is too small for {self}, calculation will always underflow", left
)
elif isinstance(right, vy_ast.Int):
if right.value < 0:
Expand Down

0 comments on commit 12ab491

Please sign in to comment.