Skip to content

Commit dc5cbf9

Browse files
Merge pull request #4997 from thomasspriggs/tas/java_constructor_fix
Fix the `this` argument used when checking a constructor
2 parents 36dc29a + f4f01cd commit dc5cbf9

File tree

4 files changed

+34
-2
lines changed

4 files changed

+34
-2
lines changed
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class Constructor2 {
2+
int myField;
3+
4+
Constructor2() { assert myField == 0; }
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
CORE
2+
Constructor2.class
3+
--function 'Constructor2.<init>'
4+
^EXIT=0$
5+
^SIGNAL=0$
6+
^VERIFICATION SUCCESSFUL$
7+
--
8+
^warning: ignoring
9+
--
10+
Test that a field of a class being constructed is zero initialised before the
11+
constructor is called, where the function being checked is a constructor.

jbmc/src/java_bytecode/java_entry_point.cpp

+18-2
Original file line numberDiff line numberDiff line change
@@ -297,8 +297,8 @@ std::pair<code_blockt, std::vector<exprt>> java_build_arguments(
297297
const select_pointer_typet &pointer_type_selector,
298298
message_handlert &message_handler)
299299
{
300-
const java_method_typet::parameterst &parameters =
301-
to_java_method_type(function.type).parameters();
300+
const java_method_typet &function_type = to_java_method_type(function.type);
301+
const java_method_typet::parameterst &parameters = function_type.parameters();
302302

303303
code_blockt init_code;
304304
exprt::operandst main_arguments;
@@ -324,6 +324,22 @@ std::pair<code_blockt, std::vector<exprt>> java_build_arguments(
324324
// be null
325325
bool is_this=(param_number==0) && parameters[param_number].get_this();
326326

327+
if(is_this && function_type.get_is_constructor())
328+
{
329+
const symbol_exprt result = fresh_java_symbol(
330+
p.type(),
331+
"this_parameter",
332+
function.location,
333+
function.name,
334+
symbol_table)
335+
.symbol_expr();
336+
main_arguments[param_number] = result;
337+
init_code.add(code_declt{result});
338+
init_code.add(
339+
code_assignt{result, side_effect_exprt(ID_java_new, p.type())});
340+
continue;
341+
}
342+
327343
java_object_factory_parameterst factory_parameters =
328344
object_factory_parameters;
329345
// only pointer must be non-null

0 commit comments

Comments
 (0)