Skip to content

Commit 9dff5dd

Browse files
authored
Solve invalid memory read issue in find_nearest (#112)
This PR addresses the corner cases where neighbor id may be [max](https://github.com/intel/ScalableVectorSearch/blob/72633231807d46ff59f651916858fa0ed6ba3ee4/include/svs/lib/neighbor.h#L160) when distance is infinity. In such scenarios, the system could return a neighbor with the maximum ID, leading to an invalid memory read. To resolve this, the PR modifies the behavior to reset the ID to the first index, ensuring a valid existing neighbor is returned.
1 parent 7263323 commit 9dff5dd

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

include/svs/core/kmeans.h

+6
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ Neighbor<size_t> find_nearest(const Query& query, const Data& data) {
4545
auto d = distance::compute(f, query, data.get_datum(i));
4646
nearest = std::min(nearest, Neighbor<size_t>(i, d));
4747
}
48+
49+
// The case where the distance is infinity and the id remains unchanged from sentinel_v,
50+
// reset to the first index to return to the exsiting neighbor.
51+
if (nearest.id() >= data.size()) {
52+
nearest = Neighbor<size_t>(0, nearest.distance());
53+
}
4854
return nearest;
4955
}
5056

0 commit comments

Comments
 (0)