Skip to content

Commit e1f30e1

Browse files
Fix bounds in axioms for lastIndexOf(char)
1 parent 9437fa0 commit e1f30e1

File tree

1 file changed

+19
-13
lines changed

1 file changed

+19
-13
lines changed

src/solvers/refinement/string_constraint_generator_indexof.cpp

+19-13
Original file line numberDiff line numberDiff line change
@@ -309,13 +309,16 @@ exprt string_constraint_generatort::add_axioms_for_index_of(
309309
/// \todo Change argument names to match add_axioms_for_last_index_of_string
310310
///
311311
/// These axioms are :
312-
/// 1. \f$ -1 \le {\tt index} \le {\tt from\_index} \f$
312+
/// 1. \f$ -1 \le {\tt index} \le {\tt from\_index}
313+
/// \land {\tt index} < |{\tt haystack}| \f$
313314
/// 2. \f$ {\tt index} = -1 \Leftrightarrow \lnot contains\f$
314-
/// 3. \f$ contains \Rightarrow ({\tt index} \le {\tt from\_index} \land
315-
/// {\tt haystack}[i] = {\tt needle} )\f$
316-
/// 4. \f$ \forall n \in [{\tt index} +1, {\tt from\_index}+1)
315+
/// 3. \f$ contains \Rightarrow
316+
/// {\tt haystack}[{\tt index}] = {\tt needle} )\f$
317+
/// 4. \f$ \forall n \in [{\tt index} +1,
318+
/// min({\tt from\_index}+1, |{\tt haystack}|))
317319
/// .\ contains \Rightarrow {\tt haystack}[n] \ne {\tt needle} \f$
318-
/// 5. \f$ \forall m \in [0, {\tt from\_index}+1)
320+
/// 5. \f$ \forall m \in [0,
321+
/// min({\tt from\_index}+1, |{\tt haystack}|))
319322
/// .\ \lnot contains \Rightarrow {\tt haystack}[m] \ne {\tt needle}\f$
320323
/// \param str: an array of characters expression
321324
/// \param c: a character expression
@@ -331,12 +334,11 @@ exprt string_constraint_generatort::add_axioms_for_last_index_of(
331334
symbol_exprt index=fresh_exist_index("last_index_of", index_type);
332335
symbol_exprt contains=fresh_boolean("contains_in_last_index_of");
333336

334-
exprt index1=from_integer(1, index_type);
335337
exprt minus1=from_integer(-1, index_type);
336-
exprt from_index_plus_one=plus_exprt_with_overflow_check(from_index, index1);
337338
and_exprt a1(
338339
binary_relation_exprt(index, ID_ge, minus1),
339-
binary_relation_exprt(index, ID_lt, from_index_plus_one));
340+
binary_relation_exprt(index, ID_lt, from_index_plus_one),
341+
binary_relation_exprt(index, ID_lt, str.length()));
340342
axioms.push_back(a1);
341343

342344
equal_exprt a2(not_exprt(contains), equal_exprt(index, minus1));
@@ -351,19 +353,23 @@ exprt string_constraint_generatort::add_axioms_for_last_index_of(
351353

352354
symbol_exprt n=fresh_univ_index("QA_last_index_of1", index_type);
353355
string_constraintt a4(
356+
const exprt index1 = from_integer(1, index_type);
357+
const plus_exprt from_index_plus_one(from_index, index1);
358+
const if_exprt end_index(
359+
binary_relation_exprt(from_index_plus_one, ID_le, str.length()),
360+
from_index_plus_one,
361+
str.length());
362+
354363
n,
355364
plus_exprt(index, index1),
356-
from_index_plus_one,
365+
end_index,
357366
contains,
358367
not_exprt(equal_exprt(str[n], c)));
359368
axioms.push_back(a4);
360369

361370
symbol_exprt m=fresh_univ_index("QA_last_index_of2", index_type);
362371
string_constraintt a5(
363-
m,
364-
from_index_plus_one,
365-
not_exprt(contains),
366-
not_exprt(equal_exprt(str[m], c)));
372+
m, end_index, not_exprt(contains), notequal_exprt(str[m], c));
367373
axioms.push_back(a5);
368374

369375
return index;

0 commit comments

Comments
 (0)