From 97cbe3c1b122366dcfeed65b039024870857704a Mon Sep 17 00:00:00 2001 From: Sebastian Harl Date: Sun, 5 Oct 2014 19:50:20 +0200 Subject: [PATCH] store_lookup: Fixed lookup by back-end name. Added an integration test which reproduces the problem -- previously, all compare operations reported a mismatch. --- src/core/store_lookup.c | 26 +++++++++++++++++--------- t/integration/simple_query.sh | 12 ++++++++++++ t/unit/core/store_lookup_test.c | 6 ++++++ 3 files changed, 35 insertions(+), 9 deletions(-) diff --git a/src/core/store_lookup.c b/src/core/store_lookup.c index d53bd58..9176592 100644 --- a/src/core/store_lookup.c +++ b/src/core/store_lookup.c @@ -136,19 +136,17 @@ obj_cmp(sdb_store_obj_t *obj, sdb_store_cond_t *cond, sdb_data_t value = SDB_DATA_INIT; int status; - if (sdb_store_get_field(obj, OBJ_C(cond)->field, &obj_value)) - return INT_MAX; if (sdb_store_expr_eval(OBJ_C(cond)->expr, obj, &value)) return INT_MAX; - if (obj_value.type != value.type) { - sdb_data_free_datum(&value); - return INT_MAX; - } - else if (OBJ_C(cond)->field == SDB_FIELD_BACKEND) { + if (OBJ_C(cond)->field == SDB_FIELD_BACKEND) { /* this implementation is not actually a conditional but rather checks * for equality (or rather, existence) only */ size_t i; + + if (value.type != SDB_TYPE_STRING) + return INT_MAX; + status = INT_MAX; for (i = 0; i < obj->backends_num; ++i) { if (! strcasecmp(obj->backends[i], value.data.string)) { @@ -156,10 +154,20 @@ obj_cmp(sdb_store_obj_t *obj, sdb_store_cond_t *cond, break; } } + sdb_data_free_datum(&value); + return status; } - else { - status = sdb_data_cmp(&obj_value, &value); + + if (sdb_store_get_field(obj, OBJ_C(cond)->field, &obj_value)) + return INT_MAX; + if (obj_value.type != value.type) { + sdb_data_free_datum(&obj_value); + sdb_data_free_datum(&value); + return INT_MAX; } + + status = sdb_data_cmp(&obj_value, &value); + sdb_data_free_datum(&obj_value); sdb_data_free_datum(&value); return status; } /* obj_cmp */ diff --git a/t/integration/simple_query.sh b/t/integration/simple_query.sh index b639443..35a55ce 100755 --- a/t/integration/simple_query.sh +++ b/t/integration/simple_query.sh @@ -152,6 +152,18 @@ output="$( run_sysdb -H "$SOCKET_FILE" \ FILTER :last_update < 2Y" )" echo $output | grep -E '^\[\]$' +output="$( run_sysdb -H "$SOCKET_FILE" \ + -c "LOOKUP hosts FILTER :backend = 'backend::mock_plugin'" )" +echo "$output" \ + | grep -F '"host1.example.com"' \ + | grep -F '"host2.example.com"' \ + | grep -F '"localhost"' \ + | grep -F '"other.host.name"' \ + | grep -F '"some.host.name"' +output="$( run_sysdb -H "$SOCKET_FILE" \ + -c "LOOKUP hosts FILTER :backend = 'invalid'" )" +echo $output | grep -E '^\[\]$' + output="$( run_sysdb -H "$SOCKET_FILE" \ -c "LOOKUP hosts MATCHING service = 'sysdbd'" )" echo "$output" | grep -F '"localhost"' diff --git a/t/unit/core/store_lookup_test.c b/t/unit/core/store_lookup_test.c index 754eb13..13861bb 100644 --- a/t/unit/core/store_lookup_test.c +++ b/t/unit/core/store_lookup_test.c @@ -394,6 +394,12 @@ START_TEST(test_obj_cond) { SDB_TYPE_INTEGER, { .integer = 0 } }, 0, 0, 0, 0, 0 }, { "a", SDB_FIELD_BACKEND, { SDB_TYPE_INTEGER, { .integer = 0 } }, 0, 0, 0, 0, 0 }, + { "a", SDB_FIELD_BACKEND, + { SDB_TYPE_INTEGER, { .integer = 0 } }, 0, 0, 0, 0, 0 }, + /* (64bit) integer value without zero-bytes */ + { "a", SDB_FIELD_BACKEND, + { SDB_TYPE_INTEGER, { .integer = 0xffffffffffffffffL } }, + 0, 0, 0, 0, 0 }, }; int status; -- 2.30.2