From 1324ebc4bf84438715744afb9bcf1469b8e00b05 Mon Sep 17 00:00:00 2001 From: chriseth Date: Tue, 7 Mar 2017 13:44:11 +0100 Subject: Warn about literal constant base in exponentiation. --- Changelog.md | 1 + libsolidity/analysis/TypeChecker.cpp | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/Changelog.md b/Changelog.md index 5eb1e401..115c30db 100644 --- a/Changelog.md +++ b/Changelog.md @@ -6,6 +6,7 @@ Features: * Code generator: Support ``revert()`` to abort with rolling back, but not consuming all gas. * Inline assembly: Support ``revert`` (EIP140) as an opcode. * Type system: Support explicit conversion of external function to address. + * Type system: Warn if base of exponentiation is literal (result type might be unexpected). Bugfixes: * Commandline interface: Always escape filenames (replace ``/``, ``:`` and ``.`` with ``_``). diff --git a/libsolidity/analysis/TypeChecker.cpp b/libsolidity/analysis/TypeChecker.cpp index 5426cd17..88078d3d 100644 --- a/libsolidity/analysis/TypeChecker.cpp +++ b/libsolidity/analysis/TypeChecker.cpp @@ -1092,6 +1092,26 @@ void TypeChecker::endVisit(BinaryOperation const& _operation) Token::isCompareOp(_operation.getOperator()) ? make_shared() : commonType; + if (_operation.getOperator() == Token::Exp) + { + if ( + leftType->category() == Type::Category::RationalNumber && + rightType->category() != Type::Category::RationalNumber + ) + if (( + commonType->category() == Type::Category::Integer && + dynamic_cast(*commonType).numBits() != 256 + ) || ( + commonType->category() == Type::Category::FixedPoint && + dynamic_cast(*commonType).numBits() != 256 + )) + warning( + _operation.location(), + "Result of exponentiation has type " + commonType->toString() + " and thus " + "might overflow. Silence this warning by converting the literal to the " + "expected type." + ); + } } bool TypeChecker::visit(FunctionCall const& _functionCall) -- cgit v1.2.3