Skip to content

Commit 6ed24e5

Browse files
cristina-davidsmowton
authored andcommitted
Throw ArithmeticException whenever a division-by-zero is encountered
1 parent 3bf46bf commit 6ed24e5

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

src/java_bytecode/java_bytecode_convert_method.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -1051,6 +1051,10 @@ codet java_bytecode_convert_methodt::convert_instructions(
10511051
i_it->statement=="checkcast" ||
10521052
i_it->statement=="newarray" ||
10531053
i_it->statement=="anewarray" ||
1054+
i_it->statement=="idiv" ||
1055+
i_it->statement=="ldiv" ||
1056+
i_it->statement=="irem" ||
1057+
i_it->statement=="lrem" ||
10541058
i_it->statement==patternt("?astore") ||
10551059
i_it->statement==patternt("?aload") ||
10561060
i_it->statement=="invokestatic" ||

src/java_bytecode/java_bytecode_instrument.cpp

+36
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ class java_bytecode_instrumentt:public messaget
5757
const exprt &idx,
5858
const source_locationt &original_loc);
5959

60+
codet check_arithmetic_exception(
61+
const exprt &denominator,
62+
const source_locationt &original_loc);
63+
6064
codet check_null_dereference(
6165
const exprt &expr,
6266
const source_locationt &original_loc,
@@ -133,6 +137,30 @@ codet java_bytecode_instrumentt::throw_exception(
133137
return init_code;
134138
}
135139

140+
141+
/// Checks whether there is a division by zero
142+
/// and throws ArithmeticException if necessary.
143+
/// Exceptions are thrown when the `throw_runtime_exceptions`
144+
/// flag is set.
145+
/// \return Based on the value of the flag `throw_runtime_exceptions`,
146+
/// it returns code that either throws an ArithmeticException
147+
/// or is a skip
148+
codet java_bytecode_instrumentt::check_arithmetic_exception(
149+
const exprt &denominator,
150+
const source_locationt &original_loc)
151+
{
152+
const constant_exprt &zero=from_integer(0, denominator.type());
153+
const binary_relation_exprt equal_zero(denominator, ID_equal, zero);
154+
155+
if(throw_runtime_exceptions)
156+
return throw_exception(
157+
equal_zero,
158+
original_loc,
159+
"java.lang.ArithmeticException");
160+
161+
return code_skipt();
162+
}
163+
136164
/// Checks whether the array access array_struct[idx] is out-of-bounds,
137165
/// and throws ArrayIndexOutofBoundsException/generates an assertion
138166
/// if necessary; Exceptions are thrown when the `throw_runtime_exceptions`
@@ -464,6 +492,14 @@ codet java_bytecode_instrumentt::instrument_expr(
464492
expr.op0(),
465493
expr.source_location());
466494
}
495+
else if((expr.id()==ID_div || expr.id()==ID_mod) &&
496+
expr.type().id()==ID_signedbv)
497+
{
498+
// check division by zero (for integer types only)
499+
return check_arithmetic_exception(
500+
expr.op1(),
501+
expr.source_location());
502+
}
467503
else if(expr.id()==ID_member &&
468504
expr.get_bool(ID_java_member_access))
469505
{

0 commit comments

Comments
 (0)