Skip to content

Commit a61ea38

Browse files
Merge pull request #1655 from diffblue/bugfix/string-last-index-of#TG592
Fix bug with lastIndexOf TG-592
2 parents 04766b2 + 3ab853e commit a61ea38

File tree

4 files changed

+36
-1
lines changed

4 files changed

+36
-1
lines changed
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
public class Test {
2+
// This compares the model of indexOf to an implementation
3+
// using loops
4+
5+
public int math_min(int i, int j) {
6+
if (i < j)
7+
return i;
8+
else
9+
return j;
10+
}
11+
12+
public int referenceLastIndexOf(String s, char ch, int fromIndex) {
13+
for (int i = math_min(fromIndex, s.length() - 1); i >= 0; i--) {
14+
if (s.charAt(i) == ch) {
15+
return i;
16+
}
17+
}
18+
return -1;
19+
}
20+
21+
public int check(String s, char ch, int fromIndex) {
22+
int reference = referenceLastIndexOf(s, ch, fromIndex);
23+
int jbmc_result = s.lastIndexOf(ch, fromIndex);
24+
assert(reference == jbmc_result);
25+
return jbmc_result;
26+
}
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
THOROUGH
2+
Test.class
3+
--function Test.check --refine-strings --string-max-length 50 --unwind 50 --java-assume-inputs-non-null
4+
^EXIT=10$
5+
^SIGNAL=0$
6+
assertion at file Test.java line 32 .* SUCCESS$
7+
assertion at file Test.java line 34 .* FAILURE$

src/solvers/refinement/string_constraint_generator_indexof.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,8 @@ exprt string_constraint_generatort::add_axioms_for_last_index_of(
348348
axioms.push_back(a3);
349349

350350
const exprt index1 = from_integer(1, index_type);
351-
const plus_exprt from_index_plus_one(from_index, index1);
351+
const exprt from_index_plus_one =
352+
plus_exprt_with_overflow_check(from_index, index1);
352353
const if_exprt end_index(
353354
binary_relation_exprt(from_index_plus_one, ID_le, str.length()),
354355
from_index_plus_one,

0 commit comments

Comments
 (0)