Skip to content

Commit 204da89

Browse files
allocate_and_assign_array_from_json function
This is so that we directly declare an array of the right length instead of giving a non-deterministic size and constraining it.
1 parent 8f82c84 commit 204da89

File tree

1 file changed

+38
-8
lines changed

1 file changed

+38
-8
lines changed

jbmc/src/java_bytecode/assignments_from_json.cpp

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,22 @@ static void assign_array_from_json(
496496
assign_array_data_component_from_json(expr, json, type_from_array, info);
497497
}
498498

499+
/// Allocate an array of size given by the json object, and assign its elemnts
500+
/// according tho the json content.
501+
static void allocate_and_assign_array_from_json(
502+
const exprt &array,
503+
const jsont &json,
504+
const optionalt<std::string> &type_from_array,
505+
object_creation_infot &info)
506+
{
507+
PRECONDITION(is_java_array_type(array.type()));
508+
const auto &element_type =
509+
java_array_element_type(to_struct_tag_type(array.type().subtype()));
510+
const json_arrayt json_array = get_untyped_array(json, element_type);
511+
allocate_array(array, from_integer(json_array.size(), java_int_type()), info);
512+
assign_array_data_component_from_json(array, json, type_from_array, info);
513+
}
514+
499515
/// One of the cases in the recursive algorithm: the case where \p expr
500516
/// represents a string.
501517
/// See \ref assign_from_json_rec.
@@ -769,10 +785,19 @@ static void assign_reference_from_json(
769785
{
770786
if(is_java_array_type(expr.type()))
771787
{
772-
INVARIANT(
773-
reference.array_length, "an array reference should stores its length");
774-
assign_array_from_json(
775-
reference.expr, json, *reference.array_length, type_from_array, info);
788+
if(has_nondet_length(json))
789+
{
790+
const exprt length =
791+
nondet_length(info.allocate_objects, info.block, info.loc);
792+
allocate_array(expr, length, info);
793+
INVARIANT(
794+
reference.array_length,
795+
"an array reference should stores its length");
796+
assign_array_from_json(
797+
reference.expr, json, *reference.array_length, type_from_array, info);
798+
}
799+
else
800+
allocate_and_assign_array_from_json(expr, json, type_from_array, info);
776801
}
777802
else
778803
assign_struct_from_json(dereference_exprt(reference.expr), json, info);
@@ -811,10 +836,15 @@ void assign_from_json_rec(
811836
}
812837
else if(is_java_array_type(expr.type()))
813838
{
814-
const exprt length =
815-
nondet_length(info.allocate_objects, info.block, info.loc);
816-
allocate_array(expr, length, info);
817-
assign_array_from_json(expr, json, length, type_from_array, info);
839+
if(has_nondet_length(json))
840+
{
841+
const exprt length =
842+
nondet_length(info.allocate_objects, info.block, info.loc);
843+
allocate_array(expr, length, info);
844+
assign_array_from_json(expr, json, length, type_from_array, info);
845+
}
846+
else
847+
allocate_and_assign_array_from_json(expr, json, type_from_array, info);
818848
}
819849
else if(
820850
const auto runtime_type =

0 commit comments

Comments
 (0)