Skip to content

Commit e81b1d9

Browse files
Merge pull request #821 from smowton/smowton/fix/checkcast_against_null
Allow checkcast of null pointer
2 parents b5e06ba + 3c5ae1a commit e81b1d9

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

src/java_bytecode/java_bytecode_convert_method.cpp

+14-4
Original file line numberDiff line numberDiff line change
@@ -1368,10 +1368,20 @@ codet java_bytecode_convert_methodt::convert_instructions(
13681368
// TODO: convert assertions to exceptions.
13691369
assert(op.size()==1 && results.size()==1);
13701370
binary_predicate_exprt check(op[0], ID_java_instanceof, arg0);
1371-
c=code_assertt(check);
1372-
c.add_source_location().set_comment("Dynamic cast check");
1373-
c.add_source_location().set_property_class("bad-dynamic-cast");
1374-
1371+
code_assertt assert_class(check);
1372+
assert_class.add_source_location().set_comment("Dynamic cast check");
1373+
assert_class.add_source_location().set_property_class("bad-dynamic-cast");
1374+
// checkcast passes when the operand is null.
1375+
empty_typet voidt;
1376+
pointer_typet voidptr(voidt);
1377+
exprt null_check_op=op[0];
1378+
if(null_check_op.type()!=voidptr)
1379+
null_check_op.make_typecast(voidptr);
1380+
code_ifthenelset conditional_check;
1381+
notequal_exprt op_not_null(null_check_op, null_pointer_exprt(voidptr));
1382+
conditional_check.cond()=std::move(op_not_null);
1383+
conditional_check.then_case()=std::move(assert_class);
1384+
c=std::move(conditional_check);
13751385
results[0]=op[0];
13761386
}
13771387
else if(statement=="invokedynamic")

0 commit comments

Comments
 (0)