Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Produce compiler error for compound division and mod zero operations #15074

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Bugfixes:
* SMTChecker: Fix internal error when using an empty tuple in a conditional operator.
* SMTChecker: Fix internal error when using bitwise operators with an array element as argument.
* Standard JSON Interface: Fix ICE when the optimizer is disabled and an empty/blank string is used for ``optimizerSteps`` sequence.
* StaticAnalyzer: Only raise a compile time error for division and modulo by zero when it's between literals.
r0qs marked this conversation as resolved.
Show resolved Hide resolved
* Yul Optimizer: Fix the order of assignments generated by ``SSATransform`` being dependent on AST IDs, sometimes resulting in different (but equivalent) bytecode when unrelated files were added to the compilation pipeline.


Expand Down
3 changes: 2 additions & 1 deletion libsolidity/analysis/StaticAnalyzer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,8 @@ bool StaticAnalyzer::visit(BinaryOperation const& _operation)
{
if (
*_operation.rightExpression().annotation().isPure &&
(_operation.getOperator() == Token::Div || _operation.getOperator() == Token::Mod)
(_operation.getOperator() == Token::Div || _operation.getOperator() == Token::Mod) &&
ConstantEvaluator::evaluate(m_errorReporter, _operation.leftExpression())
)
if (auto rhs = ConstantEvaluator::evaluate(m_errorReporter, _operation.rightExpression()))
if (rhs->value == 0)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
contract A {
uint a;
constructor() { a /= (((2)*2)%4); }
}
// ----
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
contract A {
uint a = 5;
constructor() { a /= uint(0); }
}
// ----
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
contract A {
constructor() { uint a; a / 0; }
}
// ----
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
contract A {
uint a = 5;
constructor() { a %= uint(((2)*2)%4); }
}
// ----
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
contract A {
uint a;
constructor() { a = 5; a %= 0; }
}
// ----
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
contract A {
constructor() { uint a; a % 0; }
}
// ----