diff options
author | Alex Beregszaszi <alex@rtfs.hu> | 2017-08-16 23:54:06 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-08-16 23:54:06 +0800 |
commit | 4449820be3b23cb074c8db77915a799b3ff31c49 (patch) | |
tree | eb1cc0d4735e34f121684d6099f4f0c663b850ef | |
parent | 83561e136c992287211f348e0b7bd85b8087fe23 (diff) | |
parent | 3d595d4b149e590138f35a6f535c0a20a28e6f04 (diff) | |
download | dexon-solidity-4449820be3b23cb074c8db77915a799b3ff31c49.tar dexon-solidity-4449820be3b23cb074c8db77915a799b3ff31c49.tar.gz dexon-solidity-4449820be3b23cb074c8db77915a799b3ff31c49.tar.bz2 dexon-solidity-4449820be3b23cb074c8db77915a799b3ff31c49.tar.lz dexon-solidity-4449820be3b23cb074c8db77915a799b3ff31c49.tar.xz dexon-solidity-4449820be3b23cb074c8db77915a799b3ff31c49.tar.zst dexon-solidity-4449820be3b23cb074c8db77915a799b3ff31c49.zip |
Merge pull request #2758 from ethereum/warnShift
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) { |