mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Error on invalid arithmetic with constant expressions.
This commit is contained in:
@@ -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())
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user