From 92162ac6318667e3beac4afcdd938fdfe8f58fae Mon Sep 17 00:00:00 2001 From: Sebastian Harl Date: Sun, 5 Oct 2014 18:19:39 +0200 Subject: [PATCH] store_lookup: Fixed parsing of invalid compare expressions. Previously, expressions like 'host.foo != 123' would be silently accepted by the parser. Added test cases to reproduce and catch the problem. --- src/core/store_lookup.c | 2 ++ t/unit/core/store_lookup_test.c | 15 +++++++++++---- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/src/core/store_lookup.c b/src/core/store_lookup.c index 86acd10..d53bd58 100644 --- a/src/core/store_lookup.c +++ b/src/core/store_lookup.c @@ -1001,6 +1001,8 @@ sdb_store_matcher_parse_cmp(const char *obj_type, const char *attr, return NULL; if (value.type != SDB_TYPE_STRING) { sdb_data_free_datum(&value); + if (type != SDB_ATTRIBUTE) + return NULL; return parse_attr_cmp(attr, op, expr); } diff --git a/t/unit/core/store_lookup_test.c b/t/unit/core/store_lookup_test.c index b356fa0..754eb13 100644 --- a/t/unit/core/store_lookup_test.c +++ b/t/unit/core/store_lookup_test.c @@ -532,10 +532,11 @@ END_TEST START_TEST(test_parse_cmp) { - sdb_data_t hostname = { SDB_TYPE_STRING, { .string = "hostname" } }; - sdb_data_t metricname = { SDB_TYPE_STRING, { .string = "metricname" } }; - sdb_data_t srvname = { SDB_TYPE_STRING, { .string = "srvname" } }; - sdb_data_t attrname = { SDB_TYPE_STRING, { .string = "attrname" } }; + sdb_data_t hostname = { SDB_TYPE_STRING, { .string = "hostname" } }; + sdb_data_t metricname = { SDB_TYPE_STRING, { .string = "metricname" } }; + sdb_data_t srvname = { SDB_TYPE_STRING, { .string = "srvname" } }; + sdb_data_t attrname = { SDB_TYPE_STRING, { .string = "attrname" } }; + sdb_data_t attrvalue = { SDB_TYPE_INTEGER, { .integer = 4711 } }; sdb_store_matcher_t *check; @@ -554,6 +555,8 @@ START_TEST(test_parse_cmp) { "host", NULL, "!~", &hostname, MATCHER_NOT }, { "host", "attr", "=", &hostname, -1 }, { "host", "attr", "!=", &hostname, -1 }, + { "host", "attr", "!=", &attrvalue, -1 }, + { "host", "attr", "<=", &attrvalue, -1 }, { "host", NULL, "&^", &hostname, -1 }, { "host", NULL, "<", &hostname, -1 }, { "host", NULL, "<=", &hostname, -1 }, @@ -566,6 +569,8 @@ START_TEST(test_parse_cmp) { "metric", NULL, "!~", &metricname, MATCHER_NOT }, { "metric", "attr", "=", &metricname, -1 }, { "metric", "attr", "!=", &metricname, -1 }, + { "metric", "attr", "!=", &attrvalue, -1 }, + { "metric", "attr", "<=", &attrvalue, -1 }, { "metric", NULL, "&^", &metricname, -1 }, { "metric", NULL, "<", &metricname, -1 }, { "metric", NULL, "<=", &metricname, -1 }, @@ -578,6 +583,8 @@ START_TEST(test_parse_cmp) { "service", NULL, "!~", &srvname, MATCHER_NOT }, { "service", "attr", "=", &srvname, -1 }, { "service", "attr", "!=", &srvname, -1 }, + { "service", "attr", "!=", &attrvalue, -1 }, + { "service", "attr", "<=", &attrvalue, -1 }, { "service", NULL, "&^", &srvname, -1 }, { "service", NULL, "<", &srvname, -1 }, { "service", NULL, "<=", &srvname, -1 }, -- 2.30.2