Skip to content

Commit 4ece6fd

Browse files
committed
Java: make null-pointer and similar checks fatal
These previously used code_assertt, which asserts but then continues -- this changes them to use assert(condition); assume(condition), which ensures subsequent code is not executed in conditions which are in practice impossible. With --java-throw-runtime-exceptions this is irrelevant, as a real exception is raised and the conditional GOTO guarding the throw has the same effect as the assume.
1 parent 06d8bea commit 4ece6fd

File tree

15 files changed

+185
-40
lines changed

15 files changed

+185
-40
lines changed

regression/cbmc-java/NullPointer3/test.desc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ NullPointer3.class
33
--pointer-check
44
^EXIT=10$
55
^SIGNAL=0$
6-
^.*Throw null: FAILURE$
6+
^.*Null pointer check: FAILURE$
77
^VERIFICATION FAILED$
88
--
99
^warning: ignoring
251 Bytes
Binary file not shown.
216 Bytes
Binary file not shown.
1.36 KB
Binary file not shown.
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
public class Test {
2+
3+
// In each of these cases a guard is repeated -- either an explicit assertion,
4+
// or a guard implied by Java's semantics, e.g. that an array index is in bounds.
5+
// It should be possible to violate the condition the first time, but not the second,
6+
// as the program would have aborted (without --java-throw-runtime-exceptions) or a
7+
// runtime exception thrown (with --java-throw-runtime-exceptions).
8+
9+
public static void testAssertion(boolean condition) {
10+
assert(condition);
11+
assert(condition);
12+
}
13+
14+
public static void testArrayBounds(int[] array, int index) {
15+
if(array == null)
16+
return;
17+
int got = array[index];
18+
got = array[index];
19+
}
20+
21+
public static void testClassCast(boolean unknown) {
22+
A maybe_b = unknown ? new A() : new B();
23+
B definitely_b = (B)maybe_b;
24+
definitely_b = (B)maybe_b;
25+
}
26+
27+
public static void testNullDeref(A maybeNull) {
28+
int got = maybeNull.field;
29+
got = maybeNull.field;
30+
}
31+
32+
public static void testDivByZero(int unknown) {
33+
int div = 1 / unknown;
34+
div = 1 / unknown;
35+
}
36+
37+
public static void testArrayCreation(int maybeNegative) {
38+
int[] arr = new int[maybeNegative];
39+
arr = new int[maybeNegative];
40+
}
41+
42+
}
43+
44+
class A { public int field; }
45+
class B extends A {}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
CORE
2+
Test.class
3+
--function Test.testArrayBounds
4+
^EXIT=10$
5+
^SIGNAL=0$
6+
^VERIFICATION FAILED$
7+
array-index-out-of-bounds-low\.1.*: FAILURE$
8+
array-index-out-of-bounds-low\.2.*: SUCCESS$
9+
array-index-out-of-bounds-high\.1.*: FAILURE$
10+
array-index-out-of-bounds-high\.2.*: SUCCESS$
11+
--
12+
^warning: ignoring
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
CORE
2+
Test.class
3+
--function Test.testArrayCreation
4+
^EXIT=10$
5+
^SIGNAL=0$
6+
^VERIFICATION FAILED$
7+
array-create-negative-size\.1.*: FAILURE$
8+
array-create-negative-size\.2.*: SUCCESS$
9+
--
10+
^warning: ignoring
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
CORE
2+
Test.class
3+
--function Test.testAssertion
4+
^EXIT=10$
5+
^SIGNAL=0$
6+
^VERIFICATION FAILED$
7+
assertion at file Test\.java line 10.*: FAILURE$
8+
assertion at file Test\.java line 11.*: SUCCESS$
9+
--
10+
^warning: ignoring
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
CORE
2+
Test.class
3+
--function Test.testClassCast
4+
^EXIT=10$
5+
^SIGNAL=0$
6+
^VERIFICATION FAILED$
7+
bad-dynamic-cast\.1.*: FAILURE$
8+
bad-dynamic-cast\.2.*: SUCCESS$
9+
--
10+
^warning: ignoring
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
CORE
2+
Test.class
3+
--function Test.testDivByZero
4+
^EXIT=10$
5+
^SIGNAL=0$
6+
^VERIFICATION FAILED$
7+
integer-divide-by-zero\.1.*: FAILURE$
8+
integer-divide-by-zero\.2.*: SUCCESS$
9+
--
10+
^warning: ignoring

0 commit comments

Comments
 (0)