From: Sebastian Harl Date: Tue, 16 Sep 2014 17:36:23 +0000 (+0200) Subject: frontend, sysdb: Correctly handle empty queries. X-Git-Tag: sysdb-0.5.0~34 X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=a5673017588c548a6a8e4fcb1ec44ffc27816d06;p=sysdb.git frontend, sysdb: Correctly handle empty queries. Let the frontend send back a DATA message even on empty queries to make sure the client sees the expected reply. Let sysdb handle empty replies correctly and also not print any errors on empty replies (successful command not returning any data). --- diff --git a/src/frontend/connection.c b/src/frontend/connection.c index a8dfeee..6cad747 100644 --- a/src/frontend/connection.c +++ b/src/frontend/connection.c @@ -290,10 +290,13 @@ command_handle(sdb_conn_t *conn) status = -1; } - if (status) + if (status) { + if (! sdb_strbuf_len(conn->errbuf)) + sdb_strbuf_sprintf(conn->errbuf, "Failed to execute command"); sdb_connection_send(conn, CONNECTION_ERROR, (uint32_t)sdb_strbuf_len(conn->errbuf), sdb_strbuf_string(conn->errbuf)); + } return status; } /* command_handle */ diff --git a/src/frontend/query.c b/src/frontend/query.c index 52d27ec..f55975e 100644 --- a/src/frontend/query.c +++ b/src/frontend/query.c @@ -91,7 +91,8 @@ sdb_fe_query(sdb_conn_t *conn) switch (sdb_llist_len(parsetree)) { case 0: - /* skipping empty command */ + /* skipping empty command; send back an empty reply */ + sdb_connection_send(conn, CONNECTION_DATA, 0, NULL); break; case 1: node = SDB_CONN_NODE(sdb_llist_get(parsetree, 0)); diff --git a/src/include/frontend/proto.h b/src/include/frontend/proto.h index 2fabad5..91dd360 100644 --- a/src/include/frontend/proto.h +++ b/src/include/frontend/proto.h @@ -108,7 +108,9 @@ typedef enum { * contain the type of the data and the result encoded as a JSON string. * The type is the same as the command code of the respective command (see * below) and is stored as an unsigned 32bit integer in network - * byte-order. The result may be empty (but the type is still included). + * byte-order. The result may be empty (but the type is still included) if + * the query did not return any result. The type and the result message + * are empty on empty commands. * * 0 32 64 * +---------------+---------------+ diff --git a/src/tools/sysdb/command.c b/src/tools/sysdb/command.c index d30ec60..81f6792 100644 --- a/src/tools/sysdb/command.c +++ b/src/tools/sysdb/command.c @@ -65,7 +65,13 @@ log_printer(sdb_strbuf_t *buf) static void data_printer(sdb_strbuf_t *buf) { - if (sdb_strbuf_len(buf) <= sizeof(uint32_t)) { + size_t len = sdb_strbuf_len(buf); + + if ((! len) || (len == sizeof(uint32_t))) { + /* empty command or empty reply */ + return; + } + else if (len < sizeof(uint32_t)) { printf("ERROR: Received a DATA message with invalid " "or missing data-type\n"); return;