@@ -57,6 +57,10 @@ class java_bytecode_instrumentt:public messaget
57
57
const exprt &idx,
58
58
const source_locationt &original_loc);
59
59
60
+ codet check_arithmetic_exception (
61
+ const exprt &denominator,
62
+ const source_locationt &original_loc);
63
+
60
64
codet check_null_dereference (
61
65
const exprt &expr,
62
66
const source_locationt &original_loc,
@@ -133,6 +137,30 @@ codet java_bytecode_instrumentt::throw_exception(
133
137
return init_code;
134
138
}
135
139
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
+
136
164
// / Checks whether the array access array_struct[idx] is out-of-bounds,
137
165
// / and throws ArrayIndexOutofBoundsException/generates an assertion
138
166
// / if necessary; Exceptions are thrown when the `throw_runtime_exceptions`
@@ -464,6 +492,14 @@ codet java_bytecode_instrumentt::instrument_expr(
464
492
expr.op0 (),
465
493
expr.source_location ());
466
494
}
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
+ }
467
503
else if (expr.id ()==ID_member &&
468
504
expr.get_bool (ID_java_member_access))
469
505
{
0 commit comments