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

Add some regression tests for checking out of bounds.

Kishore Nallanc7e58efafd10
diff --git a/src/art.cpp b/src/art.cpp
index 8238bc30..3e6bd7e5 100644
--- a/src/art.cpp
+++ b/src/art.cpp
@@ -287,13 +287,18 @@ void* art_search(const art_tree *t, const unsigned char *key, int key_len) {
// Bail if the prefix does not match
if (n->partial_len) {
prefix_len = check_prefix(n, key, key_len, depth);
- if (prefix_len != min(MAX_PREFIX_LEN, n->partial_len))
+ if (prefix_len != min(MAX_PREFIX_LEN, n->partial_len)) {
return NULL;
+ }
+
depth = depth + n->partial_len;
- if(depth > key_len-1)
+ if(depth >= key_len) {
return NULL;
+ }
}
+ assert(depth < key_len);
+
// Recursively search
child = find_child(n, key[depth]);
n = (child) ? *child : NULL;
@@ -800,11 +805,13 @@ static art_leaf* recursive_delete(art_node *n, art_node **ref, const unsigned ch
return NULL;
}
depth = depth + n->partial_len;
- if(depth > key_len-1) {
+ if(depth >= key_len) {
return NULL;
}
}
+ assert(depth < key_len);
+
// Find child node
art_node **child = find_child(n, key[depth]);
if (!child) return NULL;
@@ -1062,12 +1069,16 @@ int art_iter_prefix(art_tree *t, const unsigned char *key, int key_len, art_call
} else if (depth + prefix_len == key_len) {
// If we've matched the prefix, iterate on this node
return recursive_iter(n, cb, data);
+ } else if(depth + n->partial_len >= key_len) {
+ return 0;
}
// if there is a full match, go deeper
depth = depth + n->partial_len;
}
+ assert(depth < key_len);
+
// Recursively search
child = find_child(n, key[depth]);
n = (child) ? *child : NULL;
diff --git a/test/art_test.cpp b/test/art_test.cpp
index d0c345ea..c19d89a1 100644
--- a/test/art_test.cpp
+++ b/test/art_test.cpp
@@ -453,6 +453,95 @@ TEST(ArtTest, test_art_max_prefix_len_scan_prefix) {
ASSERT_TRUE(!art_iter_prefix(&t, (unsigned char*)prefix, strlen(prefix), test_prefix_cb, &p));
ASSERT_TRUE(p.count == p.max_count);
+ res = art_tree_destroy(&t);
+ ASSERT_TRUE(res == 0);
+}
+
+TEST(ArtTest, test_art_prefix_iter_out_of_bounds) {
+ // Regression: ensures that `assert(depth < key_len);` is not invoked
+ art_tree t;
+ int res = art_tree_init(&t);
+ ASSERT_TRUE(res == 0);
+
+ char* key1 = "foobarbaz1-long-test1-foo";
+ art_document doc = get_document((uint32_t) 1);
+ ASSERT_TRUE(NULL == art_insert(&t, (unsigned char*)key1, strlen(key1)+1, &doc, 1));
+
+ char *key2 = "foobarbaz1-long-test1-bar";
+ doc = get_document((uint32_t) 2);
+ ASSERT_TRUE(NULL == art_insert(&t, (unsigned char*)key2, strlen(key2)+1, &doc, 1));
+
+ char *key3 = "foobarbaz1-long-test2-foo";
+ doc = get_document((uint32_t) 3);
+ ASSERT_TRUE(NULL == art_insert(&t, (unsigned char*)key3, strlen(key3)+1, &doc, 2));
+
+ ASSERT_TRUE(art_size(&t) == 3);
+
+ // Iterate over api
+ const char *expected[] = {key2, key1};
+ prefix_data p = { 0, 0, expected };
+ char *prefix = "f2oobar";
+ ASSERT_TRUE(!art_iter_prefix(&t, (unsigned char*)prefix, strlen(prefix), test_prefix_cb, &p));
+ ASSERT_TRUE(p.count == p.max_count);
+
+ res = art_tree_destroy(&t);
+ ASSERT_TRUE(res == 0);
+}
+
+TEST(ArtTest, test_art_search_out_of_bounds) {
+ // Regression: ensures that `assert(depth < key_len);` is not invoked
+ art_tree t;
+ int res = art_tree_init(&t);
+ ASSERT_TRUE(res == 0);
+
+ char* key1 = "foobarbaz1-long-test1-foo";
+ art_document doc = get_document((uint32_t) 1);
+ ASSERT_TRUE(NULL == art_insert(&t, (unsigned char*)key1, strlen(key1)+1, &doc, 1));
+
+ char *key2 = "foobarbaz1-long-test1-bar";
+ doc = get_document((uint32_t) 2);
+ ASSERT_TRUE(NULL == art_insert(&t, (unsigned char*)key2, strlen(key2)+1, &doc, 1));
+
+ char *key3 = "foobarbaz1-long-test2-foo";
+ doc = get_document((uint32_t) 3);
+ ASSERT_TRUE(NULL == art_insert(&t, (unsigned char*)key3, strlen(key3)+1, &doc, 2));
+
+ ASSERT_TRUE(art_size(&t) == 3);
+
+ // Search for a non-existing key
+ char *prefix = "foobarbaz1-long-";
+ art_leaf* l = (art_leaf *) art_search(&t, (const unsigned char *) prefix, strlen(prefix));
+ ASSERT_EQ(NULL, l);
+
+ res = art_tree_destroy(&t);
+ ASSERT_TRUE(res == 0);
+}
+
+TEST(ArtTest, test_art_delete_out_of_bounds) {
+ // Regression: ensures that `assert(depth < key_len);` is not invoked
+ art_tree t;
+ int res = art_tree_init(&t);
+ ASSERT_TRUE(res == 0);
+
+ char* key1 = "foobarbaz1-long-test1-foo";
+ art_document doc = get_document((uint32_t) 1);
+ ASSERT_TRUE(NULL == art_insert(&t, (unsigned char*)key1, strlen(key1)+1, &doc, 1));
+
+ char *key2 = "foobarbaz1-long-test1-bar";
+ doc = get_document((uint32_t) 2);
+ ASSERT_TRUE(NULL == art_insert(&t, (unsigned char*)key2, strlen(key2)+1, &doc, 1));
+
+ char *key3 = "foobarbaz1-long-test2-foo";
+ doc = get_document((uint32_t) 3);
+ ASSERT_TRUE(NULL == art_insert(&t, (unsigned char*)key3, strlen(key3)+1, &doc, 2));
+
+ ASSERT_TRUE(art_size(&t) == 3);
+
+ // Try to delete a non-existing key
+ char *prefix = "foobarbaz1-long-";
+ art_values* values = (art_values *) art_delete(&t, (const unsigned char *) prefix, strlen(prefix));
+ ASSERT_EQ(NULL, values);
+
res = art_tree_destroy(&t);
ASSERT_TRUE(res == 0);
}
\ No newline at end of file