Re: [PATCH] DBusMessage: specify dbus_message_iter_get_fixed_array() arg precisely

Simon McVittie <smcv-ZGY8ohtN/[email protected]>
Newsgroups gmane.comp.freedesktop.dbus
Message-ID <20190807154448.GA27181@horizon>
On Wed, 07 Aug 2019 at 15:11:39 +0200, Rafał Miłecki wrote:
> That function takes pointer to an array as the second argument. Specify
> its type precisely to help users avoid mistakes in calls.

Please use https://gitlab.freedesktop.org/dbus/dbus/issues
to report bugs and feature requests, and
https://gitlab.freedesktop.org/dbus/dbus/merge_requests to propose
changes.

However, I don't think we can apply the change you are proposing here.

>  void        dbus_message_iter_get_fixed_array  (DBusMessageIter *iter,
> -                                                void            *value,
> +                                                void            **value,
>                                                  int             *n_elements);

I don't think we can do this, because it's going to cause new compiler
warnings for legitimate code. The argument that people want to pass
to this function is (conceptually) a const dbus_int32_t **, or
const dbus_int64_t **, or whatever type corresponds to the type of the
array you're interested in, something like this:

    const dbus_int32_t *int32_array;
    int n, i;

    ...

    dbus_message_iter_get_fixed_array (&iter, &int32_array, &n);

    for (i = 0; i < n; i++)
      printf ("%d\n", int32_array[i];

But types like [const or not] dbus_int32_t ** are not considered to be
compatible with void ** by Standard C, which you can see by (for example)
making your proposed change to the header and recompiling the test/
directory:

> .../src/dbus/test/dbus-daemon.c: In function ‘test_creds’:
> .../src/dbus/test/dbus-daemon.c:496:59: error: passing argument 2 of ‘dbus_message_iter_get_fixed_array’ from incompatible pointer type [-Werror=incompatible-pointer-types]
>            dbus_message_iter_get_fixed_array (&array_iter, &groups, &len);
>                                                            ^~~~~~~
> In file included from .../src/dbus/dbus/dbus-connection.h:33,
>                  from .../src/dbus/dbus/dbus-bus.h:30,
>                  from .../src/dbus/dbus/dbus.h:31,
>                  from .../src/dbus/test/dbus-daemon.c:34:
> .../src/dbus/dbus/dbus-message.h:281:66: note: expected ‘void **’ but argument is of type ‘guint32 **’ {aka ‘unsigned int **’}
>                                                  void           **value,
>                                                  ~~~~~~~~~~~~~~~~~^~~~~

The only way this can work without warnings or casts is to go via a
generic pointer (void *), which is what the current code does.

    smcv
_______________________________________________
dbus mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dbus
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.