Skip to content

Commit d9a74a9

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 d9a74a9

File tree

13 files changed

+85
-14
lines changed

13 files changed

+85
-14
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

+13-14
Original file line numberDiff line numberDiff line change
@@ -841,20 +841,19 @@ 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()))
847-
{
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);
856-
}
857-
else
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+
if(!java_string_library_preprocesst::implements_java_char_sequence_pointer(
848+
expr.type()) ||
849+
add_nondet_string_pointer_initialization(
850+
expr,
851+
object_factory_parameters.max_nondet_string_length,
852+
object_factory_parameters.string_printable,
853+
symbol_table,
854+
loc,
855+
object_factory_parameters.function_id,
856+
assignments))
858857
{
859858
gen_pointer_target_init(
860859
non_null_inst,

0 commit comments

Comments
 (0)