@@ -62,7 +62,8 @@ void SendReplyNoContent(ValkeyModuleCtx *ctx,
6262 const std::deque<indexes::Neighbor> &neighbors,
6363 const query::SearchParameters ¶meters) {
6464 const size_t start_index = CalcStartIndex (neighbors, parameters);
65- const size_t end_index = start_index + CalcEndIndex (neighbors, parameters);
65+ const size_t end_index = std::min (start_index + CalcEndIndex (neighbors, parameters),
66+ neighbors.size ());
6667 ValkeyModule_ReplyWithArray (ctx, end_index - start_index + 1 );
6768 ReplyAvailNeighbors (ctx, neighbors, parameters);
6869 for (auto i = start_index; i < end_index; ++i) {
@@ -84,7 +85,8 @@ void SerializeNeighbors(ValkeyModuleCtx *ctx,
8485 const query::SearchParameters ¶meters) {
8586 CHECK_GT (static_cast <size_t >(parameters.k ), parameters.limit .first_index );
8687 const size_t start_index = CalcStartIndex (neighbors, parameters);
87- const size_t end_index = start_index + CalcEndIndex (neighbors, parameters);
88+ const size_t end_index = std::min (start_index + CalcEndIndex (neighbors, parameters),
89+ neighbors.size ());
8890 ValkeyModule_ReplyWithArray (ctx, 2 * (end_index - start_index) + 1 );
8991 ReplyAvailNeighbors (ctx, neighbors, parameters);
9092
0 commit comments