sd-bus: extend sd_bus_message_read_strv() to paths and signatures

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.
This commit is contained in:
Zbigniew Jędrzejewski-Szmek 2021-02-05 15:22:42 +01:00
parent fa7924db0b
commit 63ab06c4d2
2 changed files with 26 additions and 8 deletions

View file

@ -36,11 +36,13 @@
<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>
@ -73,6 +75,13 @@
<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>

View 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;