Skip to content

Commit a3e7557

Browse files
authored
Merge pull request #1098 from smowton/smowton/fix/cast_null_pointer
Accept null pointer casts
2 parents 5bca0b2 + 73bd18d commit a3e7557

File tree

7 files changed

+55
-20
lines changed

7 files changed

+55
-20
lines changed
237 Bytes
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
CORE
2+
test.class
3+
4+
^EXIT=0$
5+
^SIGNAL=0$
6+
^VERIFICATION SUCCESSFUL$
7+
--
8+
Dynamic cast check: FAILURE
9+
^warning: ignoring
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
public class test {
3+
4+
public static void main() {
5+
test t = (test)null;
6+
}
7+
8+
}
237 Bytes
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
CORE
2+
test.class
3+
--java-throw-runtime-exceptions
4+
^EXIT=0$
5+
^SIGNAL=0$
6+
^VERIFICATION SUCCESSFUL$
7+
--
8+
^warning: ignoring
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
public class test {
3+
4+
public static void main() {
5+
test t = (test)null;
6+
}
7+
8+
}

src/java_bytecode/java_bytecode_instrument.cpp

+22-20
Original file line numberDiff line numberDiff line change
@@ -200,26 +200,30 @@ codet java_bytecode_instrumentt::check_class_cast(
200200
exprt null_check_op=class1;
201201
if(null_check_op.type()!=voidptr)
202202
null_check_op.make_typecast(voidptr);
203-
notequal_exprt op_not_null(null_check_op, null_pointer_exprt(voidptr));
204-
205-
// checkcast passes when the operand is null
206-
and_exprt and_expr(op_not_null, not_exprt(class_cast_check));
207203

204+
codet check_code;
208205
if(throw_runtime_exceptions)
209-
return throw_exception(
210-
and_expr,
211-
original_loc,
212-
"ClassCastException");
213-
214-
code_assertt assert_class(class_cast_check);
215-
assert_class.add_source_location().
216-
set_comment("Dynamic cast check");
217-
assert_class.add_source_location().
218-
set_property_class("bad-dynamic-cast");
206+
{
207+
check_code=
208+
throw_exception(
209+
not_exprt(class_cast_check),
210+
original_loc,
211+
"ClassCastException");
212+
}
213+
else
214+
{
215+
code_assertt assert_class(class_cast_check);
216+
assert_class.add_source_location().
217+
set_comment("Dynamic cast check");
218+
assert_class.add_source_location().
219+
set_property_class("bad-dynamic-cast");
220+
check_code=std::move(assert_class);
221+
}
219222

220223
code_ifthenelset conditional_check;
224+
notequal_exprt op_not_null(null_check_op, null_pointer_exprt(voidptr));
221225
conditional_check.cond()=std::move(op_not_null);
222-
conditional_check.then_case()=std::move(assert_class);
226+
conditional_check.then_case()=std::move(check_code);
223227
return conditional_check;
224228
}
225229

@@ -328,7 +332,7 @@ void java_bytecode_instrumentt::instrument_code(exprt &expr)
328332
}
329333
else if(statement==ID_assert)
330334
{
331-
code_assertt code_assert=to_code_assert(code);
335+
const code_assertt &code_assert=to_code_assert(code);
332336

333337
// does this correspond to checkcast?
334338
if(code_assert.assertion().id()==ID_java_instanceof)
@@ -339,13 +343,11 @@ void java_bytecode_instrumentt::instrument_code(exprt &expr)
339343
code_assert.assertion().operands().size()==2,
340344
"Instanceof should have 2 operands");
341345

342-
block.copy_to_operands(
346+
code=
343347
check_class_cast(
344348
code_assert.assertion().op0(),
345349
code_assert.assertion().op1(),
346-
code_assert.source_location()));
347-
block.copy_to_operands(code_assert);
348-
code=block;
350+
code_assert.source_location());
349351
}
350352
}
351353
else if(statement==ID_block)

0 commit comments

Comments
 (0)