diff --git a/CHANGELOG.md b/CHANGELOG.md index feb1411e3..f1350e532 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,7 +16,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), ### Features * Add parent join support for lucene knn [#1182](https://github.com/opensearch-project/k-NN/pull/1182) ### Enhancements -### Bug Fixes +### Bug Fixes +* Fix use-after-free case on nmslib search path [#1305](https://github.com/opensearch-project/k-NN/pull/1305) ### Infrastructure * Upgrade gradle to 8.4 [1289](https://github.com/opensearch-project/k-NN/pull/1289) ### Documentation diff --git a/jni/include/nmslib_wrapper.h b/jni/include/nmslib_wrapper.h index 6d862048a..9b555580a 100644 --- a/jni/include/nmslib_wrapper.h +++ b/jni/include/nmslib_wrapper.h @@ -48,10 +48,10 @@ namespace knn_jni { struct IndexWrapper { explicit IndexWrapper(const std::string& spaceType) { // Index gets constructed with a reference to data (see above) but is otherwise unused - similarity::ObjectVector data; space.reset(similarity::SpaceFactoryRegistry::Instance().CreateSpace(spaceType, similarity::AnyParams())); index.reset(similarity::MethodFactoryRegistry::Instance().CreateMethod(false, "hnsw", spaceType, *space, data)); } + similarity::ObjectVector data; std::unique_ptr> space; std::unique_ptr> index; }; diff --git a/jni/tests/nmslib_wrapper_test.cpp b/jni/tests/nmslib_wrapper_test.cpp index 96c3e1d1c..3a21e7401 100644 --- a/jni/tests/nmslib_wrapper_test.cpp +++ b/jni/tests/nmslib_wrapper_test.cpp @@ -21,6 +21,17 @@ using ::testing::NiceMock; using ::testing::Return; +TEST(NmslibIndexWrapperSearchTest, BasicAssertions) { + similarity::initLibrary(); + knn_jni::nmslib_wrapper::IndexWrapper indexWrapper = knn_jni::nmslib_wrapper::IndexWrapper("l2"); + int k = 10; + int dim = 2; + std::unique_ptr rawQueryvector(new float[dim]); + std::unique_ptr queryObject(new similarity::Object(-1, -1, dim*sizeof(float), rawQueryvector.get())); + similarity::KNNQuery knnQuery(*(indexWrapper.space), queryObject.get(), k); + indexWrapper.index->Search((similarity::KNNQuery *)nullptr); +} + TEST(NmslibCreateIndexTest, BasicAssertions) { // Initialize nmslib similarity::initLibrary();