Skip to content

Commit

Permalink
fix: add compile-time check for negative uint2str input (vyperlang#3671)
Browse files Browse the repository at this point in the history
  • Loading branch information
iFrostizz authored Dec 9, 2023
1 parent 21a47b6 commit 7c74aa2
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
25 changes: 25 additions & 0 deletions tests/functional/builtins/codegen/test_uint2str.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import pytest

from vyper.compiler import compile_code
from vyper.exceptions import InvalidType, OverflowException

VALID_BITS = list(range(8, 257, 8))


Expand Down Expand Up @@ -37,3 +40,25 @@ def foo(x: uint{bits}) -> uint256:
"""
c = get_contract(code)
assert c.foo(2**bits - 1) == 0, bits


def test_bignum_throws():
code = """
@external
def test():
a: String[78] = uint2str(2**256)
pass
"""
with pytest.raises(OverflowException):
compile_code(code)


def test_int_fails():
code = """
@external
def test():
a: String[78] = uint2str(-1)
pass
"""
with pytest.raises(InvalidType):
compile_code(code)
5 changes: 4 additions & 1 deletion vyper/builtins/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2090,7 +2090,10 @@ def evaluate(self, node):
if not isinstance(node.args[0], vy_ast.Int):
raise UnfoldableNode

value = str(node.args[0].value)
value = node.args[0].value
if value < 0:
raise InvalidType("Only unsigned ints allowed", node)
value = str(value)
return vy_ast.Str.from_node(node, value=value)

def infer_arg_types(self, node):
Expand Down

0 comments on commit 7c74aa2

Please sign in to comment.