Skip to content

Commit

Permalink
Fix: Error handling, mem safety bugs unum-cloud#335 (unum-cloud#339)
Browse files Browse the repository at this point in the history
* Add move construction tests and fix an issue caused by them

* Only consider zero length IO an error if input buffer was larger than zero

* Move option-override policy opt-in before policy definitions so overrides actually take effect
  • Loading branch information
Ngalstyan4 committed Feb 5, 2024
1 parent 3458d27 commit 4bccc71
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 10 deletions.
10 changes: 5 additions & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ if (CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)
set(USEARCH_IS_MAIN_PROJECT ON)
endif ()

# Allow CMake 3.13+ to override options when using FetchContent / add_subdirectory
if (POLICY CMP0077)
cmake_policy(SET CMP0077 NEW)
endif ()

# Options
option(USEARCH_INSTALL "Install CMake targets" OFF)

Expand All @@ -31,11 +36,6 @@ option(USEARCH_BUILD_WOLFRAM "Compile Wolfram Language bindings" OFF)
set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake ${CMAKE_MODULE_PATH})
include(ExternalProject)

# Allow CMake 3.13+ to override options when using FetchContent / add_subdirectory
if (POLICY CMP0077)
cmake_policy(SET CMP0077 NEW)
endif ()

# Configuration
include(GNUInstallDirs)
set(USEARCH_TARGET_NAME ${PROJECT_NAME})
Expand Down
11 changes: 9 additions & 2 deletions cpp/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,14 @@ void test_cosine(std::size_t collection_size, std::size_t dimensions) {
metric_punned_t metric(dimensions, metric_kind_t::cos_k, scalar_kind<scalar_at>());
index_dense_config_t config(connectivity);
config.multi = multi;
index_t index = index_t::make(metric, config);
index_t index;
{
index_t index_tmp1 = index_t::make(metric, config);
// move construction
index_t index_tmp2 = std::move(index_tmp1);
// move assignment
index = std::move(index_tmp2);
}
test_cosine<true>(index, matrix);
}
}
Expand Down Expand Up @@ -313,4 +320,4 @@ int main(int, char**) {
test_tanimoto<std::int64_t, std::uint32_t>(dimensions, connectivity);

return 0;
}
}
4 changes: 2 additions & 2 deletions include/usearch/index.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1362,7 +1362,7 @@ class output_file_t {
serialization_result_t write(void const* begin, std::size_t length) noexcept {
serialization_result_t result;
std::size_t written = std::fwrite(begin, length, 1, file_);
if (!written)
if (length && !written)
return result.failed(std::strerror(errno));
return result;
}
Expand Down Expand Up @@ -1404,7 +1404,7 @@ class input_file_t {
serialization_result_t read(void* begin, std::size_t length) noexcept {
serialization_result_t result;
std::size_t read = std::fread(begin, length, 1, file_);
if (!read)
if (length && !read)
return result.failed(std::feof(file_) ? "End of file reached!" : std::strerror(errno));
return result;
}
Expand Down
3 changes: 2 additions & 1 deletion include/usearch/index_plugins.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1819,7 +1819,8 @@ class flat_hash_multi_set_gt {
}

// Reset populated slots count
std::memset(data_, 0, buckets_ * bytes_per_bucket());
if (data_)
std::memset(data_, 0, buckets_ * bytes_per_bucket());
populated_slots_ = 0;
}

Expand Down

0 comments on commit 4bccc71

Please sign in to comment.