[systemd-devel] [PATCH] fix strict aliasing violations in src/udev/udev-builtin-usb_id.c

Shawn Landden shawn at churchofgit.com
Sun Mar 8 16:27:29 PDT 2015


On Sun, Mar 8, 2015 at 4:11 PM, Lennart Poettering <lennart at poettering.net>
wrote:

> On Thu, 05.03.15 04:58, Zbigniew Jędrzejewski-Szmek (zbyszek at in.waw.pl)
> wrote:
>
> > > +                uint8_t        bLength;
> > > +                uint8_t        bDescriptorType;
> > > +                uint8_t        bInterfaceNumber;
> > > +                uint8_t        bAlternateSetting;
> > > +                uint8_t        bNumEndpoints;
> > > +                uint8_t        bInterfaceClass;
> > > +                uint8_t        bInterfaceSubClass;
> > > +                uint8_t        bInterfaceProtocol;
> > > +                uint8_t        iInterface;
> > >          } __attribute__((packed));
> > >
> > >          if (asprintf(&filename, "%s/descriptors",
> udev_device_get_syspath(dev)) < 0)
> > > @@ -179,21 +180,21 @@ static int dev_if_packed_info(struct udev_device
> *dev, char *ifs_str, size_t len
> > >
> > >          ifs_str[0] = '\0';
> > >          while (pos < size && strpos+7 < len-2) {
> > > -                struct usb_interface_descriptor *desc;
> > > +                struct usb_interface_descriptor desc;
> > >                  char if_str[8];
> > >
> > > -                desc = (struct usb_interface_descriptor *) &buf[pos];
> > > -                if (desc->bLength < 3)
> > > +                memcpy(&desc, &buf[pos], sizeof(desc));
> > Copying it seems suboptimal. But is this actually an aliasing
> > violation? buf is a char array, and [1] says: "a character type
> > may alias any other type".
> >
> > [1]
> https://gcc.gnu.org/onlinedocs/gcc-4.6.1/gcc/Optimize-Options.html#index-fstrict_002daliasing-825
>
> Also, I greatly prefer using unions for these things, to make the
> aliasing explicit, rather than copying things.
>
> The other solution I had was to use offsetof() to basically make it an
enum, but that made the code was quite a bit more verbose.

> Lennart
>
> --
> Lennart Poettering, Red Hat
> _______________________________________________
> systemd-devel mailing list
> systemd-devel at lists.freedesktop.org
> http://lists.freedesktop.org/mailman/listinfo/systemd-devel
>



-- 
Shawn Landden
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freedesktop.org/archives/systemd-devel/attachments/20150308/16cd7597/attachment-0001.html>


More information about the systemd-devel mailing list