Skip to content

Commit 289b3f0

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 7161f17 commit 289b3f0

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

src/solvers/refinement/string_refinement_util.cpp

Lines changed: 13 additions & 9 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(args_value[0], mp_integer(0));
326+
const auto start = args_value.size() > 1
327+
? std::max(args_value[1], mp_integer(0))
328+
: mp_integer(0);
329+
330+
const mp_integer input2_size(input2_value.size());
331+
const auto end =
332+
args_value.size() > 2
333+
? std::max(std::min(args_value[2], input2_size), mp_integer(0))
334+
: input2_size;
331335

332336
std::vector<mp_integer> result(input1_value);
333337
result.insert(
334-
result.begin() + offset,
335-
input2_value.begin() + start,
336-
input2_value.end() + end);
338+
result.begin() + numeric_cast_v<std::size_t>(offset),
339+
input2_value.begin() + numeric_cast_v<std::size_t>(start),
340+
input2_value.begin() + numeric_cast_v<std::size_t>(end));
337341
return result;
338342
}
339343

0 commit comments

Comments
 (0)