Code

frontend, sysdb: Correctly handle empty queries.
authorSebastian Harl <sh@tokkee.org>
Tue, 16 Sep 2014 17:36:23 +0000 (19:36 +0200)
committerSebastian Harl <sh@tokkee.org>
Tue, 16 Sep 2014 17:36:23 +0000 (19:36 +0200)
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).

src/frontend/connection.c
src/frontend/query.c
src/include/frontend/proto.h
src/tools/sysdb/command.c

index a8dfeee86e93597b5239f9709487c867bb1a5670..6cad74788cbef796e42ef01a3fd6f334430fdabd 100644 (file)
@@ -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 */
 
index 52d27ec3c47ba7e7dc940ba552e76b43cf0b38a2..f55975edda8c55188410cfbfc68acab9d19dcd66 100644 (file)
@@ -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));
index 2fabad513b06e5c156c021617262606de628c7f2..91dd3609ce4307de6ff8ae54a01f68dfbbd2055b 100644 (file)
@@ -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
         * +---------------+---------------+
index d30ec6084179a614fcf386cf87e3a9dcad9e0079..81f679233d969ccb203f5f938c07e6d5f98fd693 100644 (file)
@@ -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;