Skip to content

Open Source alternative to Algolia + Pinecone and an Easier-to-Use alternative to ElasticSearch ⚡ 🔍 ✨ Fast, typo tolerant, in-memory fuzzy Search Engine for building delightful search experiences

Properly memset after malloc.

kishorenca854fe985889
diff --git a/src/array.cpp b/src/array.cpp
index 203e0cf3..c56e141e 100644
--- a/src/array.cpp
+++ b/src/array.cpp
@@ -47,6 +47,7 @@ void array::load(const uint32_t *sorted_array, const uint32_t array_length, cons
uint32_t size_required = (uint32_t) (unsorted_append_size_required(max, array_length) * FOR_GROWTH_FACTOR);
uint8_t *out = (uint8_t *) malloc(size_required * sizeof *out);
+ memset(out, 0, size_required);
uint32_t actual_size = for_compress_unsorted(sorted_array, out, array_length);
free(in);
@@ -103,6 +104,7 @@ void array::remove_index(uint32_t start_index, uint32_t end_index) {
uint32_t size_required = (uint32_t) (unsorted_append_size_required(max, new_index) * FOR_GROWTH_FACTOR);
uint8_t *out = (uint8_t *) malloc(size_required * sizeof *out);
+ memset(out, 0, size_required);
uint32_t actual_size = for_compress_unsorted(new_array, out, new_index);
delete[] curr_array;
diff --git a/src/sorted_array.cpp b/src/sorted_array.cpp
index a4d5b371..e72c953b 100644
--- a/src/sorted_array.cpp
+++ b/src/sorted_array.cpp
@@ -8,6 +8,7 @@ void sorted_array::load(const uint32_t *sorted_array, const uint32_t array_lengt
uint32_t size_required = (uint32_t) (sorted_append_size_required(max, array_length) * FOR_GROWTH_FACTOR);
uint8_t *out = (uint8_t *) malloc(size_required * sizeof *out);
+ memset(out, 0, size_required);
uint32_t actual_size = for_compress_sorted(sorted_array, out, array_length);
free(in);
diff --git a/test/array_test.cpp b/test/array_test.cpp
index d59a7825..8426a73a 100644
--- a/test/array_test.cpp
+++ b/test/array_test.cpp
@@ -96,6 +96,12 @@ TEST(ArrayTest, RemoveBetweenIndices) {
const size_t SIZE = 10*1000;
std::vector<uint32_t> unsorted;
+ // try removing from empty array
+ arr.append(100);
+ arr.remove_index(0, 1);
+ arr.contains(100);
+ arr.remove_index(0, 1);
+
for(size_t i=0; i<SIZE; i++) {
uint32_t r = (uint32_t) rand();
unsorted.push_back(r);
diff --git a/test/sorted_array_test.cpp b/test/sorted_array_test.cpp
index 425c8336..d1883a7c 100644
--- a/test/sorted_array_test.cpp
+++ b/test/sorted_array_test.cpp
@@ -156,6 +156,11 @@ TEST(SortedArrayTest, Uncompress) {
TEST(SortedArrayTest, RemoveValue) {
sorted_array arr;
+ // remove value on an empty arr
+ arr.append(100);
+ arr.remove_value(100);
+ arr.remove_value(110);
+
const size_t SIZE = 10*1000;
for(size_t i=0; i<SIZE; i++) {
arr.append(i);