]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
sd-bus: extend sd_bus_message_read_strv() to paths and signatures
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Fri, 5 Feb 2021 14:22:42 +0000 (15:22 +0100)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Fri, 12 Feb 2021 10:36:24 +0000 (11:36 +0100)
It's rather convenient to be able to read all three types with this function.
Strictly speaking this change is not fully compatible, in case someone was
relying on sd_bus_message_read_strv() returning an error for anything except
"as", but I hope nobody was doing that.

man/sd_bus_message_read_strv.xml
src/libsystemd/sd-bus/bus-message.c

index a90ae84098395122209936ea8094155bf02cac1a..50580d86bcc5cd27bb6c7c68ebc202ef6d3e410f 100644 (file)
   <refsect1>
     <title>Description</title>
 
-    <para><function>sd_bus_message_read_strv()</function> gives access to an array of strings in message
-    <parameter>m</parameter>. The "read pointer" in the message must be right before an array of strings. On
-    success, a pointer to the <constant>NULL</constant>-terminated array of strings is returned in the output
-    parameter <parameter>l</parameter>. Note that ownership of this array is transferred to the caller.
-    Hence, the caller is responsible for freeing this array and its contents.</para>
+    <para><function>sd_bus_message_read_strv()</function> gives access to an array of string-like items in
+    message <parameter>m</parameter>. The "read pointer" in the message must be right before an array of
+    strings (D-Bus type <literal>as</literal>), object paths (D-Bus type <literal>ao</literal>), or
+    signatures (D-Bus type <literal>ag</literal>). On success, a pointer to a
+    <constant>NULL</constant>-terminated array of strings is returned in the output parameter
+    <parameter>l</parameter>. Note that ownership of this array is transferred to the caller. Hence, the
+    caller is responsible for freeing this array and its contents.</para>
   </refsect1>
 
   <refsect1>
 
           <listitem><para>The message cannot be parsed.</para></listitem>
         </varlistentry>
+
+        <varlistentry>
+          <term><constant>-ENXIO</constant></term>
+
+          <listitem><para>The message "read pointer" is not right before an array of the appropriate type.
+          </para></listitem>
+        </varlistentry>
       </variablelist>
     </refsect2>
   </refsect1>
index f358a8699e8de7dc8005ef4df2886943900d55d9..894d681260f3c233d011577cff02353191f20f5e 100644 (file)
@@ -5587,17 +5587,26 @@ int bus_message_get_blob(sd_bus_message *m, void **buffer, size_t *sz) {
 }
 
 int bus_message_read_strv_extend(sd_bus_message *m, char ***l) {
-        const char *s;
+        char type;
+        const char *contents, *s;
         int r;
 
         assert(m);
         assert(l);
 
-        r = sd_bus_message_enter_container(m, 'a', "s");
+        r = sd_bus_message_peek_type(m, &type, &contents);
+        if (r < 0)
+                return r;
+
+        if (type != SD_BUS_TYPE_ARRAY || !STR_IN_SET(contents, "s", "o", "g"))
+                return -ENXIO;
+
+        r = sd_bus_message_enter_container(m, 'a', NULL);
         if (r <= 0)
                 return r;
 
-        while ((r = sd_bus_message_read_basic(m, 's', &s)) > 0) {
+        /* sd_bus_message_read_basic() does content validation for us. */
+        while ((r = sd_bus_message_read_basic(m, *contents, &s)) > 0) {
                 r = strv_extend(l, s);
                 if (r < 0)
                         return r;