Skip to content

Commit 8c472b3

Browse files
Fix invalid maybe null initialization of Strings
The initialization of String arrays was broken. It was impossible to create a String array that is non-null or maybe-null. When jbmc is run without a valid model for the String class (which makes little sense anyways) then a String object cannot be instantiated in the harness; the initialization will be null in this case.
1 parent 91aa783 commit 8c472b3

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

src/java_bytecode/java_object_factory.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -695,7 +695,8 @@ static bool add_nondet_string_pointer_initialization(
695695
const struct_typet &struct_type =
696696
to_struct_type(ns.follow(to_symbol_type(obj.type())));
697697

698-
if(!struct_type.has_component("data") || !struct_type.has_component("length"))
698+
// if no String model is loaded then we cannot construct a String object
699+
if(struct_type.get_bool(ID_incomplete_class))
699700
return true;
700701

701702
allocate_dynamic_object_with_decl(expr, symbol_table, loc, code);
@@ -834,17 +835,18 @@ void java_object_factoryt::gen_nondet_pointer_init(
834835
// and asign to `expr` the address of such object
835836
code_blockt non_null_inst;
836837

838+
bool string_init_failed = false;
837839
if(
838840
java_string_library_preprocesst::implements_java_char_sequence_pointer(
839841
expr.type()))
840842
{
841-
add_nondet_string_pointer_initialization(
843+
string_init_failed = add_nondet_string_pointer_initialization(
842844
expr,
843845
object_factory_parameters.max_nondet_string_length,
844846
object_factory_parameters.string_printable,
845847
symbol_table,
846848
loc,
847-
assignments);
849+
non_null_inst);
848850
}
849851
else
850852
{
@@ -867,8 +869,10 @@ void java_object_factoryt::gen_nondet_pointer_init(
867869

868870
// Alternatively, if this is a void* we *must* initialise with null:
869871
// (This can currently happen for some cases of #exception_value)
872+
// Also, if the String model is not loaded then we can only
873+
// initialize to null.
870874
bool must_be_null=
871-
subtype==empty_typet();
875+
string_init_failed || subtype==empty_typet();
872876

873877
if(must_be_null)
874878
{

0 commit comments

Comments
 (0)