From 7c74aa2618c8051db88acfac3bd71a3017c524cb Mon Sep 17 00:00:00 2001 From: Franfran <51274081+iFrostizz@users.noreply.github.com> Date: Sat, 9 Dec 2023 14:20:46 +0100 Subject: [PATCH] fix: add compile-time check for negative uint2str input (#3671) --- .../builtins/codegen/test_uint2str.py | 25 +++++++++++++++++++ vyper/builtins/functions.py | 5 +++- 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/tests/functional/builtins/codegen/test_uint2str.py b/tests/functional/builtins/codegen/test_uint2str.py index 9d2b7fe3f5..d9edea154b 100644 --- a/tests/functional/builtins/codegen/test_uint2str.py +++ b/tests/functional/builtins/codegen/test_uint2str.py @@ -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)) @@ -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) diff --git a/vyper/builtins/functions.py b/vyper/builtins/functions.py index b2d817ec5c..22931508a6 100644 --- a/vyper/builtins/functions.py +++ b/vyper/builtins/functions.py @@ -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):