Skip to content

Commit 4747ef4

Browse files
authored
Fix: Error handling, mem safety bugs #335 (#339)
* 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
1 parent 5680920 commit 4747ef4

File tree

4 files changed

+18
-10
lines changed

4 files changed

+18
-10
lines changed

CMakeLists.txt

+5-5
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ if (CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)
1313
set(USEARCH_IS_MAIN_PROJECT ON)
1414
endif ()
1515

16+
# Allow CMake 3.13+ to override options when using FetchContent / add_subdirectory
17+
if (POLICY CMP0077)
18+
cmake_policy(SET CMP0077 NEW)
19+
endif ()
20+
1621
# Options
1722
option(USEARCH_INSTALL "Install CMake targets" OFF)
1823

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

34-
# Allow CMake 3.13+ to override options when using FetchContent / add_subdirectory
35-
if (POLICY CMP0077)
36-
cmake_policy(SET CMP0077 NEW)
37-
endif ()
38-
3939
# Configuration
4040
include(GNUInstallDirs)
4141
set(USEARCH_TARGET_NAME ${PROJECT_NAME})

cpp/test.cpp

+9-2
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,14 @@ void test_cosine(std::size_t collection_size, std::size_t dimensions) {
209209
metric_punned_t metric(dimensions, metric_kind_t::cos_k, scalar_kind<scalar_at>());
210210
index_dense_config_t config(connectivity);
211211
config.multi = multi;
212-
index_t index = index_t::make(metric, config);
212+
index_t index;
213+
{
214+
index_t index_tmp1 = index_t::make(metric, config);
215+
// move construction
216+
index_t index_tmp2 = std::move(index_tmp1);
217+
// move assignment
218+
index = std::move(index_tmp2);
219+
}
213220
test_cosine<true>(index, matrix);
214221
}
215222
}
@@ -313,4 +320,4 @@ int main(int, char**) {
313320
test_tanimoto<std::int64_t, std::uint32_t>(dimensions, connectivity);
314321

315322
return 0;
316-
}
323+
}

include/usearch/index.hpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -1362,7 +1362,7 @@ class output_file_t {
13621362
serialization_result_t write(void const* begin, std::size_t length) noexcept {
13631363
serialization_result_t result;
13641364
std::size_t written = std::fwrite(begin, length, 1, file_);
1365-
if (!written)
1365+
if (length && !written)
13661366
return result.failed(std::strerror(errno));
13671367
return result;
13681368
}
@@ -1404,7 +1404,7 @@ class input_file_t {
14041404
serialization_result_t read(void* begin, std::size_t length) noexcept {
14051405
serialization_result_t result;
14061406
std::size_t read = std::fread(begin, length, 1, file_);
1407-
if (!read)
1407+
if (length && !read)
14081408
return result.failed(std::feof(file_) ? "End of file reached!" : std::strerror(errno));
14091409
return result;
14101410
}

include/usearch/index_plugins.hpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -1819,7 +1819,8 @@ class flat_hash_multi_set_gt {
18191819
}
18201820

18211821
// Reset populated slots count
1822-
std::memset(data_, 0, buckets_ * bytes_per_bucket());
1822+
if (data_)
1823+
std::memset(data_, 0, buckets_ * bytes_per_bucket());
18231824
populated_slots_ = 0;
18241825
}
18251826

0 commit comments

Comments
 (0)