From c99d2aae042643d9d26a4b952e07ca90f11a83c3 Mon Sep 17 00:00:00 2001 From: Alex Beregszaszi Date: Mon, 18 Sep 2017 11:39:17 +0100 Subject: Validate each tuple literal --- Changelog.md | 1 + libsolidity/analysis/TypeChecker.cpp | 6 ++++ test/libsolidity/SolidityNameAndTypeResolution.cpp | 33 +++++++++++++++++++++- 3 files changed, 39 insertions(+), 1 deletion(-) diff --git a/Changelog.md b/Changelog.md index ed3cdfea..076df606 100644 --- a/Changelog.md +++ b/Changelog.md @@ -22,6 +22,7 @@ Bugfixes: * Type Checker: Prevent duplicate event declarations. * Type Checker: Do not mark event parameters as shadowing state variables. * Type Checker: Allow ``gas`` in view functions. + * Type Checker: Validate each number literal in tuple expressions even if they are not assigned from. ### 0.4.17 (2017-09-21) diff --git a/libsolidity/analysis/TypeChecker.cpp b/libsolidity/analysis/TypeChecker.cpp index c2fba565..054912dd 100644 --- a/libsolidity/analysis/TypeChecker.cpp +++ b/libsolidity/analysis/TypeChecker.cpp @@ -1293,6 +1293,12 @@ bool TypeChecker::visit(TupleExpression const& _tuple) { components[i]->accept(*this); types.push_back(type(*components[i])); + + // Note: code generation will visit each of the expression even if they are not assigned from. + if (types[i]->category() == Type::Category::RationalNumber) + if (!dynamic_cast(*types[i]).mobileType()) + m_errorReporter.fatalTypeError(components[i]->location(), "Invalid rational number."); + if (_tuple.isInlineArray()) solAssert(!!types[i], "Inline array cannot have empty components"); if (_tuple.isInlineArray()) diff --git a/test/libsolidity/SolidityNameAndTypeResolution.cpp b/test/libsolidity/SolidityNameAndTypeResolution.cpp index 2c720d03..e9066c32 100644 --- a/test/libsolidity/SolidityNameAndTypeResolution.cpp +++ b/test/libsolidity/SolidityNameAndTypeResolution.cpp @@ -5537,7 +5537,7 @@ BOOST_AUTO_TEST_CASE(invalid_mobile_type) } } )"; - CHECK_ERROR(text, TypeError, "Invalid mobile type."); + CHECK_ERROR(text, TypeError, "Invalid rational number."); } BOOST_AUTO_TEST_CASE(warns_msg_value_in_non_payable_public_function) @@ -7096,6 +7096,37 @@ BOOST_AUTO_TEST_CASE(non_external_fallback) CHECK_ERROR(text, TypeError, "Fallback function must be defined as \"external\"."); } +BOOST_AUTO_TEST_CASE(invalid_literal_in_tuple) +{ + char const* text = R"( + contract C { + function f() pure public { + uint x; + (x, ) = (1E111); + } + } + )"; + CHECK_ERROR(text, TypeError, "Invalid rational number."); + text = R"( + contract C { + function f() pure public { + uint x; + (x, ) = (1, 1E111); + } + } + )"; + CHECK_ERROR(text, TypeError, "Invalid rational number."); + text = R"( + contract C { + function f() pure public { + uint x; + (x, ) = (1E111, 1); + } + } + )"; + CHECK_ERROR(text, TypeError, "Invalid rational number."); +} + BOOST_AUTO_TEST_CASE(warn_about_sha3) { char const* text = R"( -- cgit v1.2.3 From 8a8a71de84f5bd71fcea4d31d5a53fde7820ead6 Mon Sep 17 00:00:00 2001 From: chriseth Date: Tue, 17 Oct 2017 19:14:49 +0200 Subject: Only check tuples for valid rational numbers if they have more than one element. --- libsolidity/analysis/TypeChecker.cpp | 2 +- test/libsolidity/SolidityNameAndTypeResolution.cpp | 18 +++++++++++++++++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/libsolidity/analysis/TypeChecker.cpp b/libsolidity/analysis/TypeChecker.cpp index 054912dd..746e762e 100644 --- a/libsolidity/analysis/TypeChecker.cpp +++ b/libsolidity/analysis/TypeChecker.cpp @@ -1295,7 +1295,7 @@ bool TypeChecker::visit(TupleExpression const& _tuple) types.push_back(type(*components[i])); // Note: code generation will visit each of the expression even if they are not assigned from. - if (types[i]->category() == Type::Category::RationalNumber) + if (types[i]->category() == Type::Category::RationalNumber && components.size() > 1) if (!dynamic_cast(*types[i]).mobileType()) m_errorReporter.fatalTypeError(components[i]->location(), "Invalid rational number."); diff --git a/test/libsolidity/SolidityNameAndTypeResolution.cpp b/test/libsolidity/SolidityNameAndTypeResolution.cpp index e9066c32..9b0647bf 100644 --- a/test/libsolidity/SolidityNameAndTypeResolution.cpp +++ b/test/libsolidity/SolidityNameAndTypeResolution.cpp @@ -7106,7 +7106,7 @@ BOOST_AUTO_TEST_CASE(invalid_literal_in_tuple) } } )"; - CHECK_ERROR(text, TypeError, "Invalid rational number."); + CHECK_ERROR(text, TypeError, "is not implicitly convertible to expected type"); text = R"( contract C { function f() pure public { @@ -7125,6 +7125,22 @@ BOOST_AUTO_TEST_CASE(invalid_literal_in_tuple) } )"; CHECK_ERROR(text, TypeError, "Invalid rational number."); + text = R"( + contract C { + function f() pure public { + (2**270, 1); + } + } + )"; + CHECK_ERROR(text, TypeError, "Invalid rational number."); + text = R"( + contract C { + function f() pure public { + ((2**270) / 2**100, 1); + } + } + )"; + CHECK_SUCCESS(text); } BOOST_AUTO_TEST_CASE(warn_about_sha3) -- cgit v1.2.3