Skip to content

Commit

Permalink
add safety check for missing file (#2)
Browse files Browse the repository at this point in the history
* add safety check for missing file

* set `index->val` to `nullptr` after deleting it

* updated test for loading missing file

---------

Co-authored-by: Cocoa <[email protected]>
  • Loading branch information
andrew-combs and cocoa-xu authored Jun 4, 2023
1 parent a447799 commit 3165951
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 6 deletions.
10 changes: 6 additions & 4 deletions c_src/hnswlib_nif.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ static ERL_NIF_TERM hnswlib_index_new(ErlNifEnv *env, int argc, const ERL_NIF_TE
} catch (std::runtime_error &err) {
if (index->val) {
delete index->val;
index->val = nullptr;
}
enif_release_resource(index);
return erlang::nif::error(env, err.what());
Expand Down Expand Up @@ -229,15 +230,15 @@ static ERL_NIF_TERM hnswlib_index_load_index(ErlNifEnv *env, int argc, const ERL
index->val->loadIndex(path, max_elements, allow_replace_deleted);

ret = erlang::nif::ok(env, enif_make_resource(env, index));
enif_release_resource(index);
} catch (std::runtime_error &err) {
if (index->val) {
delete index->val;
index->val = nullptr;
}
enif_release_resource(index);
ret = erlang::nif::error(env, err.what());
}
enif_rwlock_rwunlock(index->rwlock);
enif_release_resource(index);

return ret;
}
Expand Down Expand Up @@ -513,6 +514,7 @@ static ERL_NIF_TERM hnswlib_bfindex_new(ErlNifEnv *env, int argc, const ERL_NIF_
} catch (std::runtime_error &err) {
if (index->val) {
delete index->val;
index->val = nullptr;
}
enif_release_resource(index);
return erlang::nif::error(env, err.what());
Expand Down Expand Up @@ -686,15 +688,15 @@ static ERL_NIF_TERM hnswlib_bfindex_load_index(ErlNifEnv *env, int argc, const E
index->val->loadIndex(path, max_elements);

ret = erlang::nif::ok(env, enif_make_resource(env, index));
enif_release_resource(index);
} catch (std::runtime_error &err) {
if (index->val) {
delete index->val;
index->val = nullptr;
}
ret = erlang::nif::error(env, err.what());
enif_release_resource(index);
}
enif_rwlock_runlock(index->rwlock);
enif_release_resource(index);

return ret;
}
Expand Down
2 changes: 0 additions & 2 deletions lib/hnswlib_index.ex
Original file line number Diff line number Diff line change
Expand Up @@ -248,8 +248,6 @@ defmodule HNSWLib.Index do
max_elements = Helper.get_keyword!(opts, :max_elements, :non_neg_integer, 0)
allow_replace_deleted = Helper.get_keyword!(opts, :allow_replace_deleted, :boolean, false)

HNSWLib.Nif.index_load_index(space, dim, path, max_elements, allow_replace_deleted)

with {:ok, ref} <-
HNSWLib.Nif.index_load_index(space, dim, path, max_elements, allow_replace_deleted) do
{:ok,
Expand Down
6 changes: 6 additions & 0 deletions test/hnswlib_index_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,12 @@ defmodule HNSWLib.Index.Test do
File.rm(save_to)
end

test "HNSWLib.Index.load_index/3 with missing file" do
bad_filepath = "this/file/doesnt/exist"
refute File.exists?(bad_filepath)
assert {:error, "Cannot open file"} = HNSWLib.Index.load_index(:l2, 2, bad_filepath)
end

test "HNSWLib.Index.mark_deleted/2" do
space = :ip
dim = 2
Expand Down

0 comments on commit 3165951

Please sign in to comment.