chiark / gitweb /
sd-bus: fix marshaling of unary type
authorDavid Herrmann <dh.herrmann@gmail.com>
Tue, 28 Jul 2015 16:16:16 +0000 (18:16 +0200)
committerSven Eden <yamakuzure@gmx.net>
Tue, 14 Mar 2017 09:07:19 +0000 (10:07 +0100)
The unary type has a fixed size of 1 in gvariant. Make sure we properly
encode it as such. Right now, we encode/decode it as empty sequence.

src/libelogind/sd-bus/bus-gvariant.c
src/libelogind/sd-bus/bus-message.c
src/libelogind/sd-bus/test-bus-gvariant.c

index 2d18a4e..402d43d 100644 (file)
@@ -75,14 +75,19 @@ int bus_gvariant_get_size(const char *signature) {
 
                 case SD_BUS_TYPE_STRUCT_BEGIN:
                 case SD_BUS_TYPE_DICT_ENTRY_BEGIN: {
-                        char t[n-1];
-
-                        memcpy(t, p + 1, n - 2);
-                        t[n - 2] = 0;
-
-                        r = bus_gvariant_get_size(t);
-                        if (r < 0)
-                                return r;
+                        if (n == 2) {
+                                /* unary type () has fixed size of 1 */
+                                r = 1;
+                        } else {
+                                char t[n-1];
+
+                                memcpy(t, p + 1, n - 2);
+                                t[n - 2] = 0;
+
+                                r = bus_gvariant_get_size(t);
+                                if (r < 0)
+                                        return r;
+                        }
 
                         sum += r;
                         break;
index c8806d5..c37a444 100644 (file)
@@ -2209,7 +2209,14 @@ static int bus_message_close_struct(sd_bus_message *m, struct bus_container *c,
         assert(!c->need_offsets || i == c->n_offsets);
         assert(c->need_offsets || n_variable == 0);
 
-        if (n_variable <= 0) {
+        if (isempty(c->signature)) {
+                /* The unary type is encoded as fixed 1 byte padding */
+                a = message_extend_body(m, 1, 1, add_offset, false);
+                if (!a)
+                        return -ENOMEM;
+
+                *a = 0;
+        } else if (n_variable <= 0) {
                 int alignment = 1;
 
                 /* Structures with fixed-size members only have to be
@@ -3814,6 +3821,14 @@ static int build_struct_offsets(
         assert(n_offsets);
 
         if (isempty(signature)) {
+                /* Unary type is encoded as *fixed* 1 byte padding */
+                r = message_peek_body(m, &m->rindex, 1, 1, &q);
+                if (r < 0)
+                        return r;
+
+                if (*(uint8_t *) q != 0)
+                        return -EBADMSG;
+
                 *item_size = 0;
                 *offsets = NULL;
                 *n_offsets = 0;
@@ -4140,7 +4155,14 @@ _public_ int sd_bus_message_enter_container(sd_bus_message *m,
 
         w->before = before;
         w->begin = m->rindex;
-        w->end = m->rindex + c->item_size;
+
+        /* Unary type has fixed size of 1, but virtual size of 0 */
+        if (BUS_MESSAGE_IS_GVARIANT(m) &&
+            type == SD_BUS_TYPE_STRUCT &&
+            isempty(signature))
+                w->end = m->rindex + 0;
+        else
+                w->end = m->rindex + c->item_size;
 
         w->array_size = array_size;
         w->item_size = item_size;
index 9b7dd2e..b078bdc 100644 (file)
@@ -59,7 +59,7 @@ static void test_bus_gvariant_is_fixed_size(void) {
 
 static void test_bus_gvariant_get_size(void) {
         assert_se(bus_gvariant_get_size("") == 0);
-        assert_se(bus_gvariant_get_size("()") == 0);
+        assert_se(bus_gvariant_get_size("()") == 1);
         assert_se(bus_gvariant_get_size("y") == 1);
         assert_se(bus_gvariant_get_size("u") == 4);
         assert_se(bus_gvariant_get_size("b") == 1);