mirror of
https://github.com/exoticorn/curlywas.git
synced 2026-01-20 11:46:43 +01:00
protect agains division by zero in constant folding
This commit is contained in:
@@ -39,7 +39,10 @@ fn fold_expr(expr: &mut ast::Expression) {
|
|||||||
use ast::BinOp::*;
|
use ast::BinOp::*;
|
||||||
match expr.expr {
|
match expr.expr {
|
||||||
ast::Expr::BinOp {
|
ast::Expr::BinOp {
|
||||||
ref mut left, op, ref mut right, ..
|
ref mut left,
|
||||||
|
op,
|
||||||
|
ref mut right,
|
||||||
|
..
|
||||||
} => {
|
} => {
|
||||||
fold_expr(left);
|
fold_expr(left);
|
||||||
fold_expr(right);
|
fold_expr(right);
|
||||||
@@ -50,8 +53,20 @@ fn fold_expr(expr: &mut ast::Expression) {
|
|||||||
Add => left.wrapping_add(right),
|
Add => left.wrapping_add(right),
|
||||||
Sub => left.wrapping_sub(right),
|
Sub => left.wrapping_sub(right),
|
||||||
Mul => left.wrapping_mul(right),
|
Mul => left.wrapping_mul(right),
|
||||||
Div => left / right, // TODO: protect agains division by zero
|
Div => {
|
||||||
Rem => left % right, // TODO: check correct behavior with negative operands
|
if let Some(result) = left.checked_div(right) {
|
||||||
|
result
|
||||||
|
} else {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Rem => {
|
||||||
|
if let Some(result) = left.checked_rem(right) {
|
||||||
|
result
|
||||||
|
} else {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
And => left & right,
|
And => left & right,
|
||||||
Or => left | right,
|
Or => left | right,
|
||||||
Xor => left ^ right,
|
Xor => left ^ right,
|
||||||
@@ -64,7 +79,7 @@ fn fold_expr(expr: &mut ast::Expression) {
|
|||||||
};
|
};
|
||||||
expr.expr = ast::Expr::I32Const(result);
|
expr.expr = ast::Expr::I32Const(result);
|
||||||
}
|
}
|
||||||
_ => ()
|
_ => (),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ast::Expr::I32Const(_) | ast::Expr::Variable { .. } => (),
|
ast::Expr::I32Const(_) | ast::Expr::Variable { .. } => (),
|
||||||
|
|||||||
Reference in New Issue
Block a user