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_SUB: DO_FOLD(-); break;
|
||||||
case EXPR_REMOVE_FROM: DO_FOLD(-); break;
|
case EXPR_REMOVE_FROM: DO_FOLD(-); break;
|
||||||
case EXPR_TIMES: 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_AND: DO_INT_FOLD(&&); break;
|
||||||
case EXPR_OR: DO_INT_FOLD(||); break;
|
case EXPR_OR: DO_INT_FOLD(||); break;
|
||||||
|
|
||||||
|
|
5
testing/btest/Baseline/core.div-by-zero/out
Normal file
5
testing/btest/Baseline/core.div-by-zero/out
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
expression error in /Users/jsiwek/Projects/bro/bro/testing/btest/.tmp/core.div-by-zero/div-by-zero.bro, line 6: division by zero [a / b]
|
||||||
|
expression error in /Users/jsiwek/Projects/bro/bro/testing/btest/.tmp/core.div-by-zero/div-by-zero.bro, line 11: division by zero [a / b]
|
||||||
|
expression error in /Users/jsiwek/Projects/bro/bro/testing/btest/.tmp/core.div-by-zero/div-by-zero.bro, line 16: division by zero [a / b]
|
||||||
|
expression error in /Users/jsiwek/Projects/bro/bro/testing/btest/.tmp/core.div-by-zero/div-by-zero.bro, line 21: modulo by zero [a % b]
|
||||||
|
expression error in /Users/jsiwek/Projects/bro/bro/testing/btest/.tmp/core.div-by-zero/div-by-zero.bro, line 26: modulo by zero [a % b]
|
36
testing/btest/core/div-by-zero.bro
Normal file
36
testing/btest/core/div-by-zero.bro
Normal file
|
@ -0,0 +1,36 @@
|
||||||
|
# @TEST-EXEC: bro -b %INPUT >out 2>&1
|
||||||
|
# @TEST-EXEC: TEST_DIFF_CANONIFIER=$SCRIPTS/diff-remove-abspath btest-diff out
|
||||||
|
|
||||||
|
event div_int(a: int, b: int)
|
||||||
|
{
|
||||||
|
print a / b;
|
||||||
|
}
|
||||||
|
|
||||||
|
event div_count(a: count, b: count)
|
||||||
|
{
|
||||||
|
print a / b;
|
||||||
|
}
|
||||||
|
|
||||||
|
event div_double(a: double, b: double)
|
||||||
|
{
|
||||||
|
print a / b;
|
||||||
|
}
|
||||||
|
|
||||||
|
event mod_int(a: int, b: int)
|
||||||
|
{
|
||||||
|
print a % b;
|
||||||
|
}
|
||||||
|
|
||||||
|
event mod_count(a: count, b: count)
|
||||||
|
{
|
||||||
|
print a % b;
|
||||||
|
}
|
||||||
|
|
||||||
|
event bro_init()
|
||||||
|
{
|
||||||
|
event div_int(10, 0);
|
||||||
|
event div_count(10, 0);
|
||||||
|
event div_double(10.0, 0.0);
|
||||||
|
event mod_int(10, 0);
|
||||||
|
event mod_count(10, 0);
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue