Skip to content

Commit bdc4946

Browse files
Fix eval of insert to match string constraints
String constraints for insert are permisive with index that are negative or exceed the length of the string so the eval method should do the same, and in particular should not make an invariant fail.
1 parent d83fa17 commit bdc4946

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

src/solvers/refinement/string_refinement_util.cpp

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -322,18 +322,22 @@ std::vector<mp_integer> string_insertion_builtin_functiont::eval(
322322
const std::vector<mp_integer> &args_value) const
323323
{
324324
PRECONDITION(args_value.size() >= 1 || args_value.size() <= 3);
325-
const std::size_t &offset = numeric_cast_v<std::size_t>(args_value[0]);
326-
const std::size_t &start =
327-
args_value.size() > 1 ? numeric_cast_v<std::size_t>(args_value[1]) : 0;
328-
const std::size_t &end = args_value.size() > 2
329-
? numeric_cast_v<std::size_t>(args_value[2])
330-
: input2_value.size();
325+
const auto offset = std::max<BigInt::llong_t>(args_value[0].to_long(), 0);
326+
const auto start = std::max<BigInt::llong_t>(
327+
args_value.size() > 1 ? args_value[1].to_long() : 0, 0);
328+
329+
const auto end = std::max<BigInt::llong_t>(
330+
std::min<BigInt::llong_t>(
331+
args_value.size() > 2 ? args_value[2].to_long()
332+
: (BigInt::llong_t)input2_value.size(),
333+
input2_value.size()),
334+
0);
331335

332336
std::vector<mp_integer> result(input1_value);
333337
result.insert(
334338
result.begin() + offset,
335339
input2_value.begin() + start,
336-
input2_value.end() + end);
340+
input2_value.begin() + end);
337341
return result;
338342
}
339343

0 commit comments

Comments
 (0)