Skip to content

Commit 57988fc

Browse files
committed
Fix String type initialisation when --refine-strings is not active
Previously this would incorrectly leave a branch on which the object was not initialised at all. Now it is always initialised, falling back to conventional initialisation when string-specific init is not possible.
1 parent c817486 commit 57988fc

File tree

13 files changed

+91
-12
lines changed

13 files changed

+91
-12
lines changed
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import org.cprover.CProver;
2+
3+
class NondetCharSequence
4+
{
5+
static void main()
6+
{
7+
CharSequence x = CProver.nondetWithNull();
8+
assert x == null || x instanceof CharSequence;
9+
}
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
CORE
2+
NondetCharSequence.class
3+
4+
^VERIFICATION SUCCESSFUL$
5+
^EXIT=0$
6+
^SIGNAL=0$
7+
--
8+
^warning: ignoring
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import org.cprover.CProver;
2+
3+
class NondetString
4+
{
5+
static void main()
6+
{
7+
String x = CProver.nondetWithNull();
8+
assert x == null || x instanceof String;
9+
}
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
CORE
2+
NondetString.class
3+
4+
^VERIFICATION SUCCESSFUL$
5+
^EXIT=0$
6+
^SIGNAL=0$
7+
--
8+
^warning: ignoring
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import org.cprover.CProver;
2+
3+
class NondetStringBuffer
4+
{
5+
static void main()
6+
{
7+
StringBuffer x = CProver.nondetWithNull();
8+
assert x == null || x instanceof StringBuffer;
9+
}
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
CORE
2+
NondetStringBuffer.class
3+
4+
^VERIFICATION SUCCESSFUL$
5+
^EXIT=0$
6+
^SIGNAL=0$
7+
--
8+
^warning: ignoring
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import org.cprover.CProver;
2+
3+
class NondetStringBuilder
4+
{
5+
static void main()
6+
{
7+
StringBuilder x = CProver.nondetWithNull();
8+
assert x == null || x instanceof StringBuilder;
9+
}
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
CORE
2+
NondetStringBuilder.class
3+
4+
^VERIFICATION SUCCESSFUL$
5+
^EXIT=0$
6+
^SIGNAL=0$
7+
--
8+
^warning: ignoring

src/java_bytecode/java_object_factory.cpp

+19-12
Original file line numberDiff line numberDiff line change
@@ -841,20 +841,27 @@ void java_object_factoryt::gen_nondet_pointer_init(
841841
// and asign to `expr` the address of such object
842842
code_blockt non_null_inst;
843843

844-
if(
845-
java_string_library_preprocesst::implements_java_char_sequence_pointer(
846-
expr.type()))
844+
// Note string-type-specific initialization might fail, e.g. if java.lang.CharSequence does not
845+
// have the expected fields (typically this happens if --refine-strings was not passed). In this
846+
// case we fall back to normal pointer target init.
847+
848+
bool string_init_succeeded = false;
849+
850+
if(java_string_library_preprocesst::implements_java_char_sequence_pointer(
851+
expr.type()))
847852
{
848-
add_nondet_string_pointer_initialization(
849-
expr,
850-
object_factory_parameters.max_nondet_string_length,
851-
object_factory_parameters.string_printable,
852-
symbol_table,
853-
loc,
854-
object_factory_parameters.function_id,
855-
assignments);
853+
string_init_succeeded =
854+
!add_nondet_string_pointer_initialization(
855+
expr,
856+
object_factory_parameters.max_nondet_string_length,
857+
object_factory_parameters.string_printable,
858+
symbol_table,
859+
loc,
860+
object_factory_parameters.function_id,
861+
assignments);
856862
}
857-
else
863+
864+
if(!string_init_succeeded)
858865
{
859866
gen_pointer_target_init(
860867
non_null_inst,

0 commit comments

Comments
 (0)