[PATCH:xdm 1/2] Add a cast to avoid a compiler error.

Geert Uytterhoeven geert at linux-m68k.org
Sat Jun 29 07:12:40 PDT 2013


On Sat, Jun 29, 2013 at 3:50 PM, Thomas Klausner <wiz at netbsd.org> wrote:
> On Fri, Jun 28, 2013 at 09:00:51PM +0200, Geert Uytterhoeven wrote:
>> On Fri, Jun 28, 2013 at 7:35 PM, Thomas Klausner <wiz at netbsd.org> wrote:
>> > -       if (atomicio(write, fd, msg, sizeof(msg)) != sizeof(msg)) {
>> > +       if (atomicio((ssize_t (*)(int, void *, size_t))write, fd, msg, sizeof(msg)) != sizeof(msg)) {
>>
>> This cast looks really gross to me...
>>
>> What about wrapping write() in a function that takes care of casting the second
>> parameter of the function?
>
> Sure, that's also possible. New version of the patch attached.

> diff --git a/xdm/prngc.c b/xdm/prngc.c
> index f0db8eb..535a59e 100644
> --- a/xdm/prngc.c
> +++ b/xdm/prngc.c
> @@ -44,6 +44,7 @@
>  #endif
>
>  static ssize_t atomicio(ssize_t (*)(int, void *, size_t), int, void *, size_t);
> +static ssize_t voidwrite(int, void *, size_t);

If you put the body here (is that allowed by xorg coding style?), you don't
even need a forward declaration.

>
> #ifndef offsetof
> # define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
> @@ -188,3 +189,9 @@ atomicio(ssize_t (*f)(int, void *, size_t), int fd, void *_s, size_t n)
>   }
>   return (pos);
>  }
> +
> +static ssize_t
> +voidwrite(int d, void *buf, size_t nbytes)
> +{
> + return write(d, (const char *)buf, nbytes);

And now even the cast can be removed, as a "void *" can be passed where a
"const void *" is expected.

> +}

Gr{oetje,eeting}s,

                        Geert

--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- geert at linux-m68k.org

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
                                -- Linus Torvalds


More information about the xorg-devel mailing list