chiark / gitweb /
libsystemd-bus: use assert_return
[elogind.git] / src / libsystemd-bus / bus-message.c
index 811374422717ff0cdbab7b5c3d52ceea88b63f6c..5e355127b0cb119f460738c1c9c346a5a3114b97 100644 (file)
@@ -161,9 +161,7 @@ static void *message_extend_fields(sd_bus_message *m, size_t align, size_t sz, b
         size_t old_size, new_size, start;
 
         assert(m);
-
-        if (m->poisoned)
-                return NULL;
+        assert_return(!m->poisoned, NULL);
 
         old_size = sizeof(struct bus_header) + m->header->fields_size;
         start = ALIGN_TO(old_size, align);
@@ -989,9 +987,7 @@ struct bus_body_part *message_append_part(sd_bus_message *m) {
         struct bus_body_part *part;
 
         assert(m);
-
-        if (m->poisoned)
-                return NULL;
+        assert_return(!m->poisoned, NULL);
 
         if (m->n_body_parts <= 0) {
                 part = &m->body;
@@ -1138,9 +1134,7 @@ static void *message_extend_body(sd_bus_message *m, size_t align, size_t sz, boo
         assert(m);
         assert(align > 0);
         assert(!m->sealed);
-
-        if (m->poisoned)
-                return NULL;
+        assert_return(!m->poisoned, NULL);
 
         start_body = ALIGN_TO((size_t) m->header->body_size, align);
         end_body = start_body + sz;
@@ -1894,9 +1888,7 @@ _public_ int sd_bus_message_open_container(
 }
 
 static size_t determine_word_size(size_t sz, size_t extra) {
-        if (sz <= 0 && extra == 0)
-                return 0;
-        else if (sz + extra <= 0xFF)
+        if (sz + extra <= 0xFF)
                 return 1;
         else if (sz + extra*2 <= 0xFFFF)
                 return 2;
@@ -2026,7 +2018,7 @@ static int bus_message_close_struct(sd_bus_message *m, struct bus_container *c,
         if (!BUS_MESSAGE_IS_GVARIANT(m))
                 return 0;
 
-        p = c->signature;
+        p = strempty(c->signature);
         while (*p != 0) {
                 size_t n;
 
@@ -2074,7 +2066,7 @@ static int bus_message_close_struct(sd_bus_message *m, struct bus_container *c,
                 if (!a)
                         return -ENOMEM;
 
-                p = c->signature;
+                p = strempty(c->signature);
                 for (i = 0, j = 0; i < c->n_offsets; i++) {
                         unsigned k;
                         size_t n;
@@ -2404,10 +2396,12 @@ _public_ int sd_bus_message_append_array_space(
 
         assert_return(m, -EINVAL);
         assert_return(!m->sealed, -EPERM);
-        assert_return(bus_type_is_trivial(type), -EINVAL);
+        assert_return(bus_type_is_trivial(type) && type != SD_BUS_TYPE_BOOLEAN, -EINVAL);
         assert_return(ptr || size == 0, -EINVAL);
         assert_return(!m->poisoned, -ESTALE);
 
+        /* alignment and size of the trivial types (except bool) is
+         * identical for gvariant and dbus1 marshalling */
         align = bus_type_get_alignment(type);
         sz = bus_type_get_size(type);
 
@@ -2554,8 +2548,8 @@ _public_ int sd_bus_message_append_array_memfd(sd_bus_message *m,
         part->size = size;
         copy_fd = -1;
 
-        message_extend_containers(m, size);
         m->header->body_size += size;
+        message_extend_containers(m, size);
 
         return sd_bus_message_close_container(m);
 }
@@ -2969,8 +2963,12 @@ static int container_next_item(sd_bus_message *m, struct bus_container *c, size_
                         *rindex = ALIGN_TO(c->offsets[c->offset_index], alignment);
                         c->item_size = c->offsets[c->offset_index+1] - *rindex;
                 } else {
+
+                        if (c->offset_index+1 >= (c->end-c->begin)/sz)
+                                goto end;
+
                         /* Fixed-size array */
-                        *rindex += sz;
+                        *rindex = c->begin + (c->offset_index+1) * sz;
                         c->item_size = sz;
                 }
 
@@ -3057,7 +3055,7 @@ static int message_peek_body(
         }
 
         part = find_part(m, start, nbytes, (void**) &q);
-        if (!part || !q)
+        if (!part || (nbytes > 0 && !q))
                 return -EBADMSG;
 
         *rindex = end;
@@ -3554,12 +3552,20 @@ static int build_struct_offsets(
         int r;
 
         assert(m);
-        assert(signature);
         assert(item_size);
         assert(offsets);
         assert(n_offsets);
 
+        if (isempty(signature)) {
+                *item_size = 0;
+                *offsets = NULL;
+                *n_offsets = 0;
+                return 0;
+        }
+
         sz = determine_word_size(size, 0);
+        if (sz <= 0)
+                return -EBADMSG;
 
         /* First, loop over signature and count variable elements and
          * elements in general. We use this to know how large the
@@ -3897,6 +3903,7 @@ _public_ int sd_bus_message_enter_container(sd_bus_message *m,
 
 _public_ int sd_bus_message_exit_container(sd_bus_message *m) {
         struct bus_container *c;
+        unsigned saved;
         int r;
 
         assert_return(m, -EINVAL);
@@ -3928,7 +3935,10 @@ _public_ int sd_bus_message_exit_container(sd_bus_message *m) {
 
         c = message_get_container(m);
 
+        saved = c->index;
+        c->index = c->saved_index;
         r = container_next_item(m, c, &m->rindex);
+        c->index = saved;
         if (r < 0)
                 return r;
 
@@ -4132,6 +4142,9 @@ _public_ int sd_bus_message_rewind(sd_bus_message *m, int complete) {
                 m->rindex = c->begin;
         }
 
+        c->offset_index = 0;
+        c->item_size = c->n_offsets > 0 ? c->offsets[0] : c->end;
+
         return !isempty(c->signature);
 }
 
@@ -4830,7 +4843,7 @@ int bus_message_parse_fields(sd_bus_message *m) {
         uint32_t unix_fds = 0;
         void *offsets = NULL;
         unsigned n_offsets = 0;
-        size_t sz;
+        size_t sz = 0;
         unsigned i = 0;
 
         assert(m);
@@ -5064,9 +5077,6 @@ int bus_message_parse_fields(sd_bus_message *m) {
         if (m->n_fds != unix_fds)
                 return -EBADMSG;
 
-        if (isempty(m->root_container.signature) != (BUS_MESSAGE_BODY_SIZE(m) == 0))
-                return -EBADMSG;
-
         switch (m->header->type) {
 
         case SD_BUS_MESSAGE_SIGNAL:
@@ -5281,7 +5291,7 @@ _public_ const char* sd_bus_message_get_signature(sd_bus_message *m, int complet
         assert_return(m, NULL);
 
         c = complete ? &m->root_container : message_get_container(m);
-        return c->signature ?: "";
+        return strempty(c->signature);
 }
 
 _public_ int sd_bus_message_copy(sd_bus_message *m, sd_bus_message *source, int all) {