Skip to content

Commit 813ebd1

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 String objects will be initialised to null.
1 parent 229964c commit 813ebd1

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

src/java_bytecode/java_object_factory.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -684,7 +684,8 @@ static bool add_nondet_string_pointer_initialization(
684684
const struct_typet &struct_type =
685685
to_struct_type(ns.follow(to_symbol_type(obj.type())));
686686

687-
if(!struct_type.has_component("data") || !struct_type.has_component("length"))
687+
// if no String class has been loaded then we cannot construct a String object
688+
if(struct_type.get_bool(ID_incomplete_class))
688689
return true;
689690

690691
allocate_dynamic_object_with_decl(expr, symbol_table, loc, code);
@@ -822,17 +823,18 @@ void java_object_factoryt::gen_nondet_pointer_init(
822823
// and asign to `expr` the address of such object
823824
code_blockt non_null_inst;
824825

826+
bool string_init_failed = false;
825827
if(
826828
java_string_library_preprocesst::implements_java_char_sequence_pointer(
827829
expr.type()))
828830
{
829-
add_nondet_string_pointer_initialization(
831+
string_init_failed = add_nondet_string_pointer_initialization(
830832
expr,
831833
object_factory_parameters.max_nondet_string_length,
832834
object_factory_parameters.string_printable,
833835
symbol_table,
834836
loc,
835-
assignments);
837+
non_null_inst);
836838
}
837839
else
838840
{
@@ -855,8 +857,9 @@ void java_object_factoryt::gen_nondet_pointer_init(
855857

856858
// Alternatively, if this is a void* we *must* initialise with null:
857859
// (This can currently happen for some cases of #exception_value)
858-
bool must_be_null=
859-
subtype==empty_typet();
860+
// Also, if no String class has been loaded then we will
861+
// initialize to null.
862+
bool must_be_null = string_init_failed || subtype == empty_typet();
860863

861864
if(must_be_null)
862865
{

0 commit comments

Comments
 (0)