@@ -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,
@@ -132,6 +136,30 @@ codet java_bytecode_instrumentt::throw_exception(
132
136
return if_code;
133
137
}
134
138
139
+
140
+ // / Checks whether there is a division by zero
141
+ // / and throws ArithmeticException if necessary.
142
+ // / Exceptions are thrown when the `throw_runtime_exceptions`
143
+ // / flag is set.
144
+ // / \return Based on the value of the flag `throw_runtime_exceptions`,
145
+ // / it returns code that either throws an ArithmeticException
146
+ // / or is a skip
147
+ codet java_bytecode_instrumentt::check_arithmetic_exception (
148
+ const exprt &denominator,
149
+ const source_locationt &original_loc)
150
+ {
151
+ const constant_exprt &zero=from_integer (0 , denominator.type ());
152
+ const binary_relation_exprt equal_zero (denominator, ID_equal, zero);
153
+
154
+ if (throw_runtime_exceptions)
155
+ return throw_exception (
156
+ equal_zero,
157
+ original_loc,
158
+ " java.lang.ArithmeticException" );
159
+
160
+ return code_skipt ();
161
+ }
162
+
135
163
// / Checks whether the array access array_struct[idx] is out-of-bounds,
136
164
// / and throws ArrayIndexOutofBoundsException/generates an assertion
137
165
// / if necessary; Exceptions are thrown when the `throw_runtime_exceptions`
@@ -463,6 +491,14 @@ codet java_bytecode_instrumentt::instrument_expr(
463
491
expr.op0 (),
464
492
expr.source_location ());
465
493
}
494
+ else if ((expr.id ()==ID_div || expr.id ()==ID_mod) &&
495
+ expr.type ().id ()==ID_signedbv)
496
+ {
497
+ // check division by zero (for integer types only)
498
+ return check_arithmetic_exception (
499
+ expr.op1 (),
500
+ expr.source_location ());
501
+ }
466
502
else if (expr.id ()==ID_member &&
467
503
expr.get_bool (ID_java_member_access))
468
504
{
0 commit comments