diff options
author | chriseth <chris@ethereum.org> | 2017-08-16 19:52:06 +0800 |
---|---|---|
committer | chriseth <chris@ethereum.org> | 2017-08-16 19:54:28 +0800 |
commit | 3d595d4b149e590138f35a6f535c0a20a28e6f04 (patch) | |
tree | eb1cc0d4735e34f121684d6099f4f0c663b850ef | |
parent | 83561e136c992287211f348e0b7bd85b8087fe23 (diff) | |
download | dexon-solidity-3d595d4b149e590138f35a6f535c0a20a28e6f04.tar.gz dexon-solidity-3d595d4b149e590138f35a6f535c0a20a28e6f04.tar.zst dexon-solidity-3d595d4b149e590138f35a6f535c0a20a28e6f04.zip |
Warn about shift of literals.
-rw-r--r-- | Changelog.md | 1 | ||||
-rw-r--r-- | libsolidity/analysis/TypeChecker.cpp | 5 | ||||
-rw-r--r-- | test/libsolidity/SolidityNameAndTypeResolution.cpp | 38 |
3 files changed, 42 insertions, 2 deletions
diff --git a/Changelog.md b/Changelog.md index 9bc0b354..7d026862 100644 --- a/Changelog.md +++ b/Changelog.md @@ -7,6 +7,7 @@ Features: * Static Analyzer: Warn about large storage structures. * Metadata: Store experimental flag in metadata CBOR. * Type Checker: More detailed error message for invalid overrides. + * Type Checker: Warn about shifting a literal. Bugfixes: * Parser: Enforce commas between array and tuple elements. diff --git a/libsolidity/analysis/TypeChecker.cpp b/libsolidity/analysis/TypeChecker.cpp index dbc95c4f..ca848dd0 100644 --- a/libsolidity/analysis/TypeChecker.cpp +++ b/libsolidity/analysis/TypeChecker.cpp @@ -1305,8 +1305,9 @@ void TypeChecker::endVisit(BinaryOperation const& _operation) _operation.leftExpression().annotation().isPure && _operation.rightExpression().annotation().isPure; - if (_operation.getOperator() == Token::Exp) + if (_operation.getOperator() == Token::Exp || _operation.getOperator() == Token::SHL) { + string operation = _operation.getOperator() == Token::Exp ? "exponentiation" : "shift"; if ( leftType->category() == Type::Category::RationalNumber && rightType->category() != Type::Category::RationalNumber @@ -1320,7 +1321,7 @@ void TypeChecker::endVisit(BinaryOperation const& _operation) )) m_errorReporter.warning( _operation.location(), - "Result of exponentiation has type " + commonType->toString() + " and thus " + "Result of " + operation + " has type " + commonType->toString() + " and thus " "might overflow. Silence this warning by converting the literal to the " "expected type." ); diff --git a/test/libsolidity/SolidityNameAndTypeResolution.cpp b/test/libsolidity/SolidityNameAndTypeResolution.cpp index 51d60596..1c83e1a3 100644 --- a/test/libsolidity/SolidityNameAndTypeResolution.cpp +++ b/test/libsolidity/SolidityNameAndTypeResolution.cpp @@ -1765,6 +1765,44 @@ BOOST_AUTO_TEST_CASE(exp_warn_literal_base) CHECK_SUCCESS(sourceCode); } +BOOST_AUTO_TEST_CASE(shift_warn_literal_base) +{ + char const* sourceCode = R"( + contract test { + function f() returns(uint) { + uint8 x = 100; + return 10 << x; + } + } + )"; + CHECK_WARNING(sourceCode, "might overflow"); + sourceCode = R"( + contract test { + function f() returns(uint) { + uint8 x = 100; + return uint8(10) << x; + } + } + )"; + CHECK_SUCCESS(sourceCode); + sourceCode = R"( + contract test { + function f() returns(uint) { + return 2 << 80; + } + } + )"; + CHECK_SUCCESS(sourceCode); + sourceCode = R"( + contract test { + function f() returns(uint) { + uint8 x = 100; + return 10 >> x; + } + } + )"; + CHECK_SUCCESS(sourceCode); +} BOOST_AUTO_TEST_CASE(warn_var_from_zero) { |