Skip to content

Commit 0e928a3

Browse files
Merge pull request #4443 from romainbrenguier/bugfix/string-null-arguments
Forbid null String arguments of builtin jbmc String functions
2 parents ec2ee3c + dd28355 commit 0e928a3

File tree

4 files changed

+23
-2
lines changed

4 files changed

+23
-2
lines changed
Binary file not shown.

jbmc/regression/jbmc-strings/StringSubstring/Test.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,10 @@ public void testFail(int i) {
4444
if(i == 4)
4545
assert !s[4].equals("foo");
4646
}
47+
48+
public void testNull(String s, int start, int end) {
49+
// Check that the CProverString version excludes the null case
50+
String sub = org.cprover.CProverString.substring(s, start, end);
51+
}
52+
4753
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
CORE
2+
Test.class
3+
--function Test.testNull
4+
^EXIT=0$
5+
^SIGNAL=0$
6+
VERIFICATION SUCCESSFUL
7+
--
8+
Null pointer check: FAILURE
9+
--
10+
Builtin function exclude the cases where one of the string arguments is null

jbmc/src/java_bytecode/java_string_library_preprocess.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -293,8 +293,9 @@ java_string_library_preprocesst::convert_exprt_to_string_exprt(
293293
}
294294

295295
/// for each expression that is of a type implementing strings, we declare a new
296-
/// `cprover_string` whose contents is deduced from the expression and replace
297-
/// the expression by this cprover_string in the output list; in the other case
296+
/// `cprover_string` whose contents is deduced from the expression, replace
297+
/// the expression by this cprover_string in the output list, and add an
298+
/// assumption that the object is not null; in the other case
298299
/// the expression is kept as is for the output list. Also does the same thing
299300
/// for char array references.
300301
/// \param operands: a list of expressions
@@ -315,8 +316,12 @@ exprt::operandst java_string_library_preprocesst::process_operands(
315316
for(const auto &p : operands)
316317
{
317318
if(implements_java_char_sequence_pointer(p.type()))
319+
{
320+
init_code.add(code_assumet(
321+
notequal_exprt(p, null_pointer_exprt(to_pointer_type(p.type())))));
318322
ops.push_back(convert_exprt_to_string_exprt(
319323
p, loc, symbol_table, function_id, init_code));
324+
}
320325
else if(is_java_char_array_pointer_type(p.type()))
321326
ops.push_back(
322327
replace_char_array(p, loc, function_id, symbol_table, init_code));

0 commit comments

Comments
 (0)