diff options
author | chriseth <chris@ethereum.org> | 2018-04-12 17:49:08 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-04-12 17:49:08 +0800 |
commit | c3dc67d0e0c311a907e7a27e159f9159d78af949 (patch) | |
tree | 5931874703a65e74c9319a06381ea769c2e402a9 /libsolidity | |
parent | ea445b9d372ad99f5c705628104db36bcb0d33a2 (diff) | |
parent | d42476e241489447e5dc4f5b1fafc8000e635fbc (diff) | |
download | dexon-solidity-c3dc67d0e0c311a907e7a27e159f9159d78af949.tar dexon-solidity-c3dc67d0e0c311a907e7a27e159f9159d78af949.tar.gz dexon-solidity-c3dc67d0e0c311a907e7a27e159f9159d78af949.tar.bz2 dexon-solidity-c3dc67d0e0c311a907e7a27e159f9159d78af949.tar.lz dexon-solidity-c3dc67d0e0c311a907e7a27e159f9159d78af949.tar.xz dexon-solidity-c3dc67d0e0c311a907e7a27e159f9159d78af949.tar.zst dexon-solidity-c3dc67d0e0c311a907e7a27e159f9159d78af949.zip |
Merge pull request #3848 from ethereum/constantDivisionByZero
Error on invalid arithmetic with constant expressions.
Diffstat (limited to 'libsolidity')
-rw-r--r-- | libsolidity/analysis/StaticAnalyzer.cpp | 42 | ||||
-rw-r--r-- | libsolidity/analysis/StaticAnalyzer.h | 2 | ||||
-rw-r--r-- | libsolidity/ast/Types.h | 3 |
3 files changed, 47 insertions, 0 deletions
diff --git a/libsolidity/analysis/StaticAnalyzer.cpp b/libsolidity/analysis/StaticAnalyzer.cpp index 33b0e296..51aa0b28 100644 --- a/libsolidity/analysis/StaticAnalyzer.cpp +++ b/libsolidity/analysis/StaticAnalyzer.cpp @@ -21,6 +21,7 @@ */ #include <libsolidity/analysis/StaticAnalyzer.h> +#include <libsolidity/analysis/ConstantEvaluator.h> #include <libsolidity/ast/AST.h> #include <libsolidity/interface/ErrorReporter.h> #include <memory> @@ -231,6 +232,47 @@ bool StaticAnalyzer::visit(InlineAssembly const& _inlineAssembly) return true; } +bool StaticAnalyzer::visit(BinaryOperation const& _operation) +{ + if ( + _operation.rightExpression().annotation().isPure && + (_operation.getOperator() == Token::Div || _operation.getOperator() == Token::Mod) + ) + if (auto rhs = dynamic_pointer_cast<RationalNumberType const>( + ConstantEvaluator(m_errorReporter).evaluate(_operation.rightExpression()) + )) + if (rhs->isZero()) + m_errorReporter.typeError( + _operation.location(), + (_operation.getOperator() == Token::Div) ? "Division by zero." : "Modulo zero." + ); + + return true; +} + +bool StaticAnalyzer::visit(FunctionCall const& _functionCall) +{ + if (_functionCall.annotation().kind == FunctionCallKind::FunctionCall) + { + auto functionType = dynamic_pointer_cast<FunctionType const>(_functionCall.expression().annotation().type); + solAssert(functionType, ""); + if (functionType->kind() == FunctionType::Kind::AddMod || functionType->kind() == FunctionType::Kind::MulMod) + { + solAssert(_functionCall.arguments().size() == 3, ""); + if (_functionCall.arguments()[2]->annotation().isPure) + if (auto lastArg = dynamic_pointer_cast<RationalNumberType const>( + ConstantEvaluator(m_errorReporter).evaluate(*(_functionCall.arguments())[2]) + )) + if (lastArg->isZero()) + m_errorReporter.typeError( + _functionCall.location(), + "Arithmetic modulo zero." + ); + } + } + return true; +} + bigint StaticAnalyzer::structureSizeEstimate(Type const& _type, set<StructDefinition const*>& _structsSeen) { switch (_type.category()) diff --git a/libsolidity/analysis/StaticAnalyzer.h b/libsolidity/analysis/StaticAnalyzer.h index 0a806bbd..2a62e391 100644 --- a/libsolidity/analysis/StaticAnalyzer.h +++ b/libsolidity/analysis/StaticAnalyzer.h @@ -64,6 +64,8 @@ private: virtual bool visit(Return const& _return) override; virtual bool visit(MemberAccess const& _memberAccess) override; virtual bool visit(InlineAssembly const& _inlineAssembly) override; + virtual bool visit(BinaryOperation const& _operation) override; + virtual bool visit(FunctionCall const& _functionCall) override; /// @returns the size of this type in storage, including all sub-types. static bigint structureSizeEstimate(Type const& _type, std::set<StructDefinition const*>& _structsSeen); diff --git a/libsolidity/ast/Types.h b/libsolidity/ast/Types.h index 05f506f1..ecfc2333 100644 --- a/libsolidity/ast/Types.h +++ b/libsolidity/ast/Types.h @@ -446,6 +446,9 @@ public: /// @returns true if the value is negative. bool isNegative() const { return m_value < 0; } + /// @returns true if the value is zero. + bool isZero() const { return m_value == 0; } + private: rational m_value; |