diff --git a/include/forarray.h b/include/forarray.h index 867fc10f..14f0e20e 100644 --- a/include/forarray.h +++ b/include/forarray.h @@ -19,8 +19,9 @@ private: uint32_t max = std::numeric_limits::min(); public: forarray(const uint32_t n=2) { - size_bytes = n * FOR_ELE_SIZE; + size_bytes = METADATA_OVERHEAD + (n * FOR_ELE_SIZE); in = new uint8_t[size_bytes]; + memset(in, 0, size_bytes); } static inline uint32_t required_bits(const uint32_t v) { @@ -28,8 +29,9 @@ public: } uint32_t inline sorted_append_size_required(uint32_t value) { - uint32_t m = *(uint32_t *)(in + 0); - uint32_t bnew = required_bits(value - m); + uint32_t m = std::min(min, value); + uint32_t M = std::max(max, value); + uint32_t bnew = required_bits(M - m); return METADATA_OVERHEAD + for_compressed_size_bits(length+1, bnew); } @@ -37,11 +39,13 @@ public: bool append_sorted(uint32_t value) { uint32_t size_required = sorted_append_size_required(value); - if(size_required > size_bytes) { + if(size_required+4 > size_bytes) { // grow the array first size_t new_size = (size_t) (size_required * FOR_GROWTH_FACTOR); uint8_t *new_location = (uint8_t *) realloc(in, new_size); - if(new_location == NULL) return false; + if(new_location == NULL) { + abort(); + } in = new_location; size_bytes = (uint32_t) new_size; } @@ -68,13 +72,12 @@ public: bool append_unsorted(uint32_t value) { uint32_t size_required = unsorted_append_size_required(value); - if(size_required > size_bytes) { + if(size_required+4 > size_bytes) { // grow the array first size_t new_size = (size_t) (size_required * FOR_GROWTH_FACTOR); uint8_t *new_location = (uint8_t *) realloc(in, new_size); if(new_location == NULL) { abort(); - return false; } in = new_location; size_bytes = (uint32_t) new_size; @@ -83,11 +86,10 @@ public: uint32_t new_length_bytes = for_append_unsorted(in, length, value); if(new_length_bytes == 0) { abort(); - return false; } - if (in[length] < min) min = in[length]; - if (in[length] > max) max = in[length]; + if(value < min) min = value; + if(value > max) max = value; length_bytes = new_length_bytes; length++; diff --git a/src/art.cpp b/src/art.cpp index b0877047..8110bdca 100644 --- a/src/art.cpp +++ b/src/art.cpp @@ -283,6 +283,8 @@ void* art_search(const art_tree *t, const unsigned char *key, int key_len) { if (prefix_len != min(MAX_PREFIX_LEN, n->partial_len)) return NULL; depth = depth + n->partial_len; + if(depth > key_len-1) + return NULL; } // Recursively search diff --git a/src/collection.cpp b/src/collection.cpp index 956c6ea6..1a6a6f80 100644 --- a/src/collection.cpp +++ b/src/collection.cpp @@ -36,10 +36,12 @@ void Collection::add(std::vector tokens, uint16_t score) { document.offsets = new uint32_t[kv.second.size()]; uint32_t num_hits = 0; - const unsigned char *key = (const unsigned char *) kv.first.c_str(); - int key_len = (int) kv.first.length() + 1; // for the terminating \0 char - art_leaf* leaf = (art_leaf *) art_search(&t, key, key_len); + int key_len = (int) kv.first.length() + 1; // for the terminating \0 char + char *key = new char[key_len]; + strcpy(key, kv.first.c_str()); + + art_leaf* leaf = (art_leaf *) art_search(&t, (const unsigned char *) key, key_len); if(leaf != NULL) { num_hits = leaf->values->ids.getLength(); } @@ -50,7 +52,7 @@ void Collection::add(std::vector tokens, uint16_t score) { document.offsets[i] = kv.second[i]; } - art_insert(&t, key, key_len, &document, num_hits); + art_insert(&t, (const unsigned char *) key, key_len, &document, num_hits); delete document.offsets; }