mirror of
https://github.com/zeek/zeek.git
synced 2025-10-07 09:08:20 +00:00
Prevent division/modulo by zero in scripts.
Integral/floating-point division/modulo by zero in C++ is undefined behavior, so to prevent such cases in a script from crashing Bro, they're now reported as an error (with script location information) and the event handler in which it occurred returns immediately.
This commit is contained in:
parent
4e2e690bff
commit
1750e351c4
3 changed files with 82 additions and 2 deletions
43
src/Expr.cc
43
src/Expr.cc
|
@ -779,9 +779,48 @@ Val* BinaryExpr::Fold(Val* v1, Val* v2) const
|
|||
case EXPR_SUB: DO_FOLD(-); break;
|
||||
case EXPR_REMOVE_FROM: DO_FOLD(-); break;
|
||||
case EXPR_TIMES: DO_FOLD(*); break;
|
||||
case EXPR_DIVIDE: DO_FOLD(/); break;
|
||||
case EXPR_DIVIDE:
|
||||
{
|
||||
if ( is_integral )
|
||||
{
|
||||
if ( i2 == 0 )
|
||||
reporter->ExprRuntimeError(this, "division by zero");
|
||||
i3 = i1 / i2;
|
||||
}
|
||||
else if ( is_unsigned )
|
||||
{
|
||||
if ( u2 == 0 )
|
||||
reporter->ExprRuntimeError(this, "division by zero");
|
||||
u3 = u1 / u2;
|
||||
}
|
||||
else
|
||||
{
|
||||
if ( d2 == 0 )
|
||||
reporter->ExprRuntimeError(this, "division by zero");
|
||||
d3 = d1 / d2;
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case EXPR_MOD:
|
||||
{
|
||||
if ( is_integral )
|
||||
{
|
||||
if ( i2 == 0 )
|
||||
reporter->ExprRuntimeError(this, "modulo by zero");
|
||||
i3 = i1 % i2;
|
||||
}
|
||||
else if ( is_unsigned )
|
||||
{
|
||||
if ( u2 == 0 )
|
||||
reporter->ExprRuntimeError(this, "modulo by zero");
|
||||
u3 = u1 % u2;
|
||||
}
|
||||
else
|
||||
Internal("bad type in BinaryExpr::Fold");
|
||||
}
|
||||
break;
|
||||
|
||||
case EXPR_MOD: DO_INT_FOLD(%); break;
|
||||
case EXPR_AND: DO_INT_FOLD(&&); break;
|
||||
case EXPR_OR: DO_INT_FOLD(||); break;
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue