Skip to content

Commit d0a844b

Browse files
committed
Assert denominator non-zero when Java runtime exceptions are disabled
All the other Java-instrument checks are presented as exceptions when enabled or as assertions when exceptions are disabled; ArithmeticException is uniquely not checked. This adds an assertion to match the other cases.
1 parent 94ffce3 commit d0a844b

File tree

4 files changed

+21
-2
lines changed

4 files changed

+21
-2
lines changed
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
public class DivideByZero {
2+
public static void main(String args[]) {
3+
int i=0;
4+
int j=10/i;
5+
}
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
CORE
2+
DivideByZero.class
3+
4+
^EXIT=10$
5+
^SIGNAL=0$
6+
Denominator should be nonzero: FAILURE
7+
^VERIFICATION FAILED
8+
--
9+
^warning: ignoring

src/java_bytecode/java_bytecode_instrument.cpp

+6-2
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ codet java_bytecode_instrumentt::throw_exception(
143143
/// flag is set.
144144
/// \return Based on the value of the flag `throw_runtime_exceptions`,
145145
/// it returns code that either throws an ArithmeticException
146-
/// or is a skip
146+
/// or asserts a nonzero denominator.
147147
codet java_bytecode_instrumentt::check_arithmetic_exception(
148148
const exprt &denominator,
149149
const source_locationt &original_loc)
@@ -157,7 +157,11 @@ codet java_bytecode_instrumentt::check_arithmetic_exception(
157157
original_loc,
158158
"java.lang.ArithmeticException");
159159

160-
return code_skipt();
160+
code_assertt ret(binary_relation_exprt(denominator, ID_notequal, zero));
161+
ret.add_source_location()=original_loc;
162+
ret.add_source_location().set_comment("Denominator should be nonzero");
163+
ret.add_source_location().set_property_class("integer-divide-by-zero");
164+
return ret;
161165
}
162166

163167
/// Checks whether the array access array_struct[idx] is out-of-bounds,

0 commit comments

Comments
 (0)