Skip to content

Commit 2080cd3

Browse files
author
Matthias Güdemann
committed
Complete instanceof for Java.
The current implementation did not consider correctly the case of `null` in `ID_java_instanceof`. An instanceof with `null` always fails. For `a instanceof B` this now adds `a != null` to the condition.
1 parent 85193a0 commit 2080cd3

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

src/goto-programs/remove_instanceof.cpp

Lines changed: 7 additions & 2 deletions
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)