diff options
author | Daniel Kirchner <daniel@ekpyron.org> | 2018-04-09 21:22:45 +0800 |
---|---|---|
committer | Daniel Kirchner <daniel@ekpyron.org> | 2018-04-12 03:17:10 +0800 |
commit | daa69df447e167fe75d57a5bbbabee4d637218a4 (patch) | |
tree | 06651c92bf79cbc14a931d28fc161ad480997506 /libsolidity/analysis/StaticAnalyzer.cpp | |
parent | 418e2725b50aebc169dd80f78a0ce3eefc2e296e (diff) | |
download | dexon-solidity-daa69df447e167fe75d57a5bbbabee4d637218a4.tar dexon-solidity-daa69df447e167fe75d57a5bbbabee4d637218a4.tar.gz dexon-solidity-daa69df447e167fe75d57a5bbbabee4d637218a4.tar.bz2 dexon-solidity-daa69df447e167fe75d57a5bbbabee4d637218a4.tar.lz dexon-solidity-daa69df447e167fe75d57a5bbbabee4d637218a4.tar.xz dexon-solidity-daa69df447e167fe75d57a5bbbabee4d637218a4.tar.zst dexon-solidity-daa69df447e167fe75d57a5bbbabee4d637218a4.zip |
Error on invalid arithmetic with constant expressions.
Diffstat (limited to 'libsolidity/analysis/StaticAnalyzer.cpp')
-rw-r--r-- | libsolidity/analysis/StaticAnalyzer.cpp | 42 |
1 files changed, 42 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()) |