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

Fix crash when an int32 field is filtered with an int64 value.

kishorenc13fd927944ef
diff --git a/include/field.h b/include/field.h
index 44761e23..b9263a24 100644
--- a/include/field.h
+++ b/include/field.h
@@ -58,6 +58,14 @@ struct field {
type == field_types::INT64 || type == field_types::INT64_ARRAY);
}
+ bool is_int32() const {
+ return (type == field_types::INT32 || type == field_types::INT32_ARRAY);
+ }
+
+ bool is_int64() const {
+ return (type == field_types::INT64 || type == field_types::INT64_ARRAY);
+ }
+
bool is_float() const {
return (type == field_types::FLOAT || type == field_types::FLOAT_ARRAY);
}
diff --git a/include/string_utils.h b/include/string_utils.h
index 7f131904..41eab296 100644
--- a/include/string_utils.h
+++ b/include/string_utils.h
@@ -169,6 +169,16 @@ struct StringUtils {
return (*p == 0) && ull <= std::numeric_limits<uint64_t>::max();
}
+ static bool is_int64_t(const std::string &s) {
+ if(s.empty()) {
+ return false;
+ }
+
+ char * p ;
+ long long val = strtoll(s.c_str(), &p, 10);
+ return (*p == 0) && val >= std::numeric_limits<int64_t>::min() && val <= std::numeric_limits<int64_t>::max();
+ }
+
static bool is_uint32_t(const std::string &s) {
if(s.empty()) {
return false;
@@ -179,6 +189,16 @@ struct StringUtils {
return (*p == 0) && ul <= std::numeric_limits<uint32_t>::max();
}
+ static bool is_int32_t(const std::string &s) {
+ if(s.empty()) {
+ return false;
+ }
+
+ char * p ;
+ long val = strtol(s.c_str(), &p, 10);
+ return (*p == 0) && val >= std::numeric_limits<int32_t>::min() && val <= std::numeric_limits<int32_t>::max();
+ }
+
static void toupper(std::string& str) {
std::transform(str.begin(), str.end(), str.begin(), ::toupper);
}
diff --git a/src/collection.cpp b/src/collection.cpp
index ca7b3215..121c53eb 100644
--- a/src/collection.cpp
+++ b/src/collection.cpp
@@ -547,12 +547,22 @@ Option<nlohmann::json> Collection::search(const std::string & query, const std::
StringUtils::split(raw_value.substr(1, raw_value.size() - 2), filter_values, ",");
for(const std::string & filter_value: filter_values) {
- if(_field.is_integer() && !StringUtils::is_integer(filter_value)) {
- return Option<nlohmann::json>(400, "Error with field `" + _field.name + "`: Not an integer.");
+ if(_field.is_int32()) {
+ if(!StringUtils::is_int32_t(filter_value)) {
+ return Option<nlohmann::json>(400, "Error with filter field `" + _field.name + "`: Not an int32.");
+ }
}
- if(_field.is_float() && !StringUtils::is_float(filter_value)) {
- return Option<nlohmann::json>(400, "Error with field `" + _field.name + "`: Not a float.");
+ else if(_field.is_int64()) {
+ if(!StringUtils::is_int64_t(filter_value)) {
+ return Option<nlohmann::json>(400, "Error with filter field `" + _field.name + "`: Not an int64.");
+ }
+ }
+
+ else if(_field.is_float()) {
+ if(!StringUtils::is_float(filter_value)) {
+ return Option<nlohmann::json>(400, "Error with filter field `" + _field.name + "`: Not a float.");
+ }
}
}
@@ -560,7 +570,7 @@ Option<nlohmann::json> Collection::search(const std::string & query, const std::
} else {
Option<NUM_COMPARATOR> op_comparator = filter::extract_num_comparator(raw_value);
if(!op_comparator.ok()) {
- return Option<nlohmann::json>(400, "Error with field `" + _field.name + "`: " + op_comparator.error());
+ return Option<nlohmann::json>(400, "Error with filter field `" + _field.name + "`: " + op_comparator.error());
}
// extract numerical value
@@ -576,12 +586,22 @@ Option<nlohmann::json> Collection::search(const std::string & query, const std::
filter_value = StringUtils::trim(filter_value);
- if(_field.is_integer() && !StringUtils::is_integer(filter_value)) {
- return Option<nlohmann::json>(400, "Error with field `" + _field.name + "`: Not an integer.");
+ if(_field.is_int32()) {
+ if(!StringUtils::is_int32_t(filter_value)) {
+ return Option<nlohmann::json>(400, "Error with filter field `" + _field.name + "`: Not an int32.");
+ }
}
- if(_field.is_float() && !StringUtils::is_float(filter_value)) {
- return Option<nlohmann::json>(400, "Error with field `" + _field.name + "`: Not a float.");
+ else if(_field.is_int64()) {
+ if(!StringUtils::is_int64_t(filter_value)) {
+ return Option<nlohmann::json>(400, "Error with filter field `" + _field.name + "`: Not an int64.");
+ }
+ }
+
+ else if(_field.is_float()) {
+ if(!StringUtils::is_float(filter_value)) {
+ return Option<nlohmann::json>(400, "Error with filter field `" + _field.name + "`: Not a float.");
+ }
}
f = {field_name, {filter_value}, op_comparator.get()};
@@ -593,7 +613,7 @@ Option<nlohmann::json> Collection::search(const std::string & query, const std::
for(std::string & filter_value: filter_values) {
if(filter_value != "true" && filter_value != "false") {
- return Option<nlohmann::json>(400, "Values of field `" + _field.name +
+ return Option<nlohmann::json>(400, "Values of filter field `" + _field.name +
"`: must be `true` or `false`.");
}
@@ -603,7 +623,7 @@ Option<nlohmann::json> Collection::search(const std::string & query, const std::
f = {field_name, filter_values, EQUALS};
} else {
if(raw_value != "true" && raw_value != "false") {
- return Option<nlohmann::json>(400, "Value of field `" + _field.name + "`: must be `true` or `false`.");
+ return Option<nlohmann::json>(400, "Value of filter field `" + _field.name + "`: must be `true` or `false`.");
}
std::string bool_value = (raw_value == "true") ? "1" : "0";
f = {field_name, {bool_value}, EQUALS};
@@ -627,7 +647,7 @@ Option<nlohmann::json> Collection::search(const std::string & query, const std::
f = {field_name, {raw_value}, str_comparator};
}
} else {
- return Option<nlohmann::json>(400, "Error with field `" + _field.name + "`: Unidentified field type.");
+ return Option<nlohmann::json>(400, "Error with filter field `" + _field.name + "`: Unidentified field type.");
}
filters.push_back(f);
diff --git a/test/collection_test.cpp b/test/collection_test.cpp
index 14033a57..57b235ef 100644
--- a/test/collection_test.cpp
+++ b/test/collection_test.cpp
@@ -950,19 +950,40 @@ TEST_F(CollectionTest, FilterAndQueryFieldRestrictions) {
result_op = coll_mul_fields->search("captain", query_fields, "points: \"100\"", facets, sort_fields, 0, 10, 1,
FREQUENCY, false);
ASSERT_EQ(false, result_op.ok());
- ASSERT_STREQ("Error with field `points`: Numerical field has an invalid comparator.", result_op.error().c_str());
+ ASSERT_STREQ("Error with filter field `points`: Numerical field has an invalid comparator.", result_op.error().c_str());
// bad filter value type - equaling float on an integer field
result_op = coll_mul_fields->search("captain", query_fields, "points: 100.34", facets, sort_fields, 0, 10, 1,
FREQUENCY, false);
ASSERT_EQ(false, result_op.ok());
- ASSERT_STREQ("Error with field `points`: Numerical field has an invalid comparator.", result_op.error().c_str());
+ ASSERT_STREQ("Error with filter field `points`: Numerical field has an invalid comparator.", result_op.error().c_str());
// bad filter value type - less than float on an integer field
result_op = coll_mul_fields->search("captain", query_fields, "points: <100.0", facets, sort_fields, 0, 10, 1,
FREQUENCY, false);
ASSERT_EQ(false, result_op.ok());
- ASSERT_STREQ("Error with field `points`: Not an integer.", result_op.error().c_str());
+ ASSERT_STREQ("Error with filter field `points`: Not an int32.", result_op.error().c_str());
+
+ // when an int32 field is queried with a 64-bit number
+ result_op = coll_mul_fields->search("captain", query_fields, "points: <2230070399", facets, sort_fields, 0, 10, 1,
+ FREQUENCY, false);
+ ASSERT_EQ(false, result_op.ok());
+ ASSERT_STREQ("Error with filter field `points`: Not an int32.", result_op.error().c_str());
+
+ // using a string filter value against an integer field
+ result_op = coll_mul_fields->search("captain", query_fields, "points: <sdsdfsdf", facets, sort_fields, 0, 10, 1,
+ FREQUENCY, false);
+ ASSERT_EQ(false, result_op.ok());
+
+ // large negative number
+ result_op = coll_mul_fields->search("captain", query_fields, "points: >-3230070399", facets, sort_fields, 0, 10, 1,
+ FREQUENCY, false);
+ ASSERT_EQ(false, result_op.ok());
+
+ // but should allow small negative number
+ result_op = coll_mul_fields->search("captain", query_fields, "points: >-3230", facets, sort_fields, 0, 10, 1,
+ FREQUENCY, false);
+ ASSERT_EQ(true, result_op.ok());
collectionManager.drop_collection("coll_mul_fields");
}