-
Notifications
You must be signed in to change notification settings - Fork 273
Throw ArithmeticException whenever a division-by-zero is encountered #950
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
public class ArithmeticExceptionTest { | ||
public static void main(String args[]) { | ||
try { | ||
int i=0; | ||
int j=10/i; | ||
} | ||
catch(ArithmeticException exc) { | ||
assert false; | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
CORE | ||
ArithmeticExceptionTest.class | ||
--java-throw-runtime-exceptions | ||
^EXIT=10$ | ||
^SIGNAL=0$ | ||
^.*assertion at file ArithmeticExceptionTest.java line 8 function.*: FAILURE$ | ||
^VERIFICATION FAILED | ||
-- | ||
^warning: ignoring |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -57,6 +57,10 @@ class java_bytecode_instrumentt:public messaget | |
const exprt &idx, | ||
const source_locationt &original_loc); | ||
|
||
codet check_arithmetic_exception( | ||
const exprt &denominator, | ||
const source_locationt &original_loc); | ||
|
||
codet check_null_dereference( | ||
const exprt &expr, | ||
const source_locationt &original_loc, | ||
|
@@ -132,6 +136,32 @@ codet java_bytecode_instrumentt::throw_exception( | |
return init_code; | ||
} | ||
|
||
|
||
/// Checks whether there is a division by zero | ||
/// and throws ArithmeticException if necessary. | ||
/// Exceptions are thrown when the `throw_runtime_exceptions` | ||
/// flag is set. | ||
/// \return Based on the value of the flag `throw_runtime_exceptions`, | ||
/// it returns code that either throws an ArithmeticException | ||
/// or is a skip | ||
codet java_bytecode_instrumentt::check_arithmetic_exception( | ||
const exprt &denominator, | ||
const source_locationt &original_loc) | ||
{ | ||
symbolt exc_symbol; | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. as only integral div-by-zero throws, maybe add invariant for |
||
const constant_exprt &zero=from_integer(0, denominator.type()); | ||
const binary_relation_exprt equal_zero(denominator, ID_equal, zero); | ||
|
||
if(throw_runtime_exceptions) | ||
return throw_exception( | ||
equal_zero, | ||
original_loc, | ||
"java.lang.ArithmeticException"); | ||
|
||
return code_skipt(); | ||
} | ||
|
||
/// Checks whether the array access array_struct[idx] is out-of-bounds, | ||
/// and throws ArrayIndexOutofBoundsException/generates an assertion | ||
/// if necessary; Exceptions are thrown when the `throw_runtime_exceptions` | ||
|
@@ -463,6 +493,13 @@ codet java_bytecode_instrumentt::instrument_expr( | |
expr.op0(), | ||
expr.source_location()); | ||
} | ||
else if(expr.id()==ID_div) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. needs |
||
{ | ||
// check division by zero | ||
return check_arithmetic_exception( | ||
expr.op1(), | ||
expr.source_location()); | ||
} | ||
else if(expr.id()==ID_member && | ||
expr.get_bool(ID_java_member_access)) | ||
{ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
only
idiv
andldiv
throwArithmeticException
,fdiv
andddiv
do not. In additionirem
andlrem
throw, too.