ubus: fix legacy empty reply format
authorJo-Philipp Wich <jo@mein.io>
Thu, 1 Oct 2020 17:58:27 +0000 (19:58 +0200)
committerJo-Philipp Wich <jo@mein.io>
Sun, 4 Oct 2020 20:59:11 +0000 (22:59 +0200)
The legacy ubus protocol must not include an empty object in the result
array if the invoked ubus procedure yielded no response.

This fixes compatibility with existing legacy ubus api clients that expect
this behaviour, LuCI's fs.js in particular.

Signed-off-by: Jo-Philipp Wich <jo@mein.io>
ubus.c

diff --git a/ubus.c b/ubus.c
index 39b38b2c9043df329de501a50d6f2c5cc573727c..cc7447e8f48bd3c6f661464adef8dfd76af4b229 100644 (file)
--- a/ubus.c
+++ b/ubus.c
@@ -465,10 +465,14 @@ uh_ubus_request_cb(struct ubus_request *req, int ret)
                uh_ubus_init_json_rpc_response(cl, &buf);
                r = blobmsg_open_array(&buf, "result");
                blobmsg_add_u32(&buf, "", ret);
-               c = blobmsg_open_table(&buf, NULL);
-               blob_for_each_attr(cur, du->buf.head, rem)
-                       blobmsg_add_blob(&buf, cur);
-               blobmsg_close_table(&buf, c);
+
+               if (blob_len(du->buf.head)) {
+                       c = blobmsg_open_table(&buf, NULL);
+                       blob_for_each_attr(cur, du->buf.head, rem)
+                               blobmsg_add_blob(&buf, cur);
+                       blobmsg_close_table(&buf, c);
+               }
+
                blobmsg_close_array(&buf, r);
                uh_ubus_send_response(cl, &buf);
                return;