Skip to content

Commit 933d635

Browse files
author
Matthias Güdemann
authored
Merge pull request #1716 from mgudemann/fix/null_check_for_java_instanceof
[TG-1793] Complete instanceof for Java.
2 parents 1659314 + 9c457b7 commit 933d635

File tree

4 files changed

+28
-2
lines changed

4 files changed

+28
-2
lines changed
716 Bytes
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
public class InstanceOf8 {
2+
public static boolean test(Integer i) {
3+
if (i instanceof Integer) {
4+
return true;
5+
} else {
6+
return false;
7+
}
8+
}
9+
public static void main(String[] args)
10+
{
11+
assert(!test(null));
12+
assert(test(new Integer(1)));
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
CORE
2+
InstanceOf8.class
3+
4+
^EXIT=0$
5+
^SIGNAL=0$
6+
^VERIFICATION SUCCESSFUL$
7+
--

src/goto-programs/remove_instanceof.cpp

+7-2
Original file line numberDiff line numberDiff line change
@@ -113,15 +113,20 @@ std::size_t remove_instanceoft::lower_instanceof(
113113
newinst->source_location=this_inst->source_location;
114114
newinst->function=this_inst->function;
115115

116-
// Replace the instanceof construct with a big-or.
116+
// Replace the instanceof construct with a conjunction of non-null and the
117+
// disjunction of all possible object types. According to the Java
118+
// specification, null instanceof T is false for all possible values of T.
119+
// (http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.20.2)
120+
notequal_exprt non_null_expr(
121+
check_ptr, null_pointer_exprt(to_pointer_type(check_ptr.type())));
117122
exprt::operandst or_ops;
118123
for(const auto &clsname : children)
119124
{
120125
constant_exprt clsexpr(clsname, string_typet());
121126
equal_exprt test(newsym.symbol_expr(), clsexpr);
122127
or_ops.push_back(test);
123128
}
124-
expr=disjunction(or_ops);
129+
expr = and_exprt(non_null_expr, disjunction(or_ops));
125130

126131
return 1;
127132
}

0 commit comments

Comments
 (0)