Skip to content

Commit 973d34a

Browse files
Fix Issue: Division by zero crash in assembler constant expressions #347 (#390)
1 parent b30b76d commit 973d34a

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

src/assembler/expreval.cpp

+14-2
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,13 @@ VIntS evaluate(const std::shared_ptr<Expr> &expr,
240240
}
241241
FiExpr;
242242
IfExpr(Div, v) {
243-
return evaluate(v->lhs, variables) / evaluate(v->rhs, variables);
243+
auto rhs_value = evaluate(v->rhs, variables);
244+
if (rhs_value == 0) {
245+
throw std::runtime_error(
246+
"Division by zero error in expression evaluation.");
247+
}
248+
249+
return evaluate(v->lhs, variables) / rhs_value;
244250
}
245251
FiExpr;
246252
IfExpr(Mul, v) {
@@ -252,7 +258,13 @@ VIntS evaluate(const std::shared_ptr<Expr> &expr,
252258
}
253259
FiExpr;
254260
IfExpr(Mod, v) {
255-
return evaluate(v->lhs, variables) % evaluate(v->rhs, variables);
261+
auto rhs_value = evaluate(v->rhs, variables);
262+
if (rhs_value == 0) {
263+
throw std::runtime_error(
264+
"Modulo by zero error in expression evaluation.");
265+
}
266+
267+
return evaluate(v->lhs, variables) % rhs_value;
256268
}
257269
FiExpr;
258270
IfExpr(And, v) {

0 commit comments

Comments
 (0)