Skip to content

Commit

Permalink
Merge pull request #15074 from clonker/14702-produce-compiler-error-c…
Browse files Browse the repository at this point in the history
…ompound-div-mod-zero

Produce compiler error for compound division and mod zero operations
  • Loading branch information
clonker committed May 7, 2024
2 parents ae79d13 + dd07096 commit aa6bbc2
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 1 deletion.
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.
* 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; }
}
// ----

0 comments on commit aa6bbc2

Please sign in to comment.