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

Shawn Landden shawn at churchofgit.com
Wed Mar 4 20:07:29 PST 2015


On Wed, Mar 4, 2015 at 7:58 PM, Zbigniew Jędrzejewski-Szmek <
zbyszek at in.waw.pl> wrote:

> On Tue, Mar 03, 2015 at 04:21:30PM -0800, Shawn Landden wrote:
> > also switch to <inttypes.h>
> > ---
> >  src/udev/udev-builtin-usb_id.c | 35 ++++++++++++++++++-----------------
> >  1 file changed, 18 insertions(+), 17 deletions(-)
> >
> > diff --git a/src/udev/udev-builtin-usb_id.c
> b/src/udev/udev-builtin-usb_id.c
> > index ab0d96e..0223421 100644
> > --- a/src/udev/udev-builtin-usb_id.c
> > +++ b/src/udev/udev-builtin-usb_id.c
> > @@ -28,6 +28,7 @@
> >  #include <ctype.h>
> >  #include <fcntl.h>
> >  #include <errno.h>
> > +#include <inttypes.h>
> >
> >  #include "udev.h"
> >
> > @@ -153,15 +154,15 @@ static int dev_if_packed_info(struct udev_device
> *dev, char *ifs_str, size_t len
> >          int pos = 0;
> >          unsigned strpos = 0;
> >          struct usb_interface_descriptor {
> > -                u_int8_t        bLength;
> > -                u_int8_t        bDescriptorType;
> > -                u_int8_t        bInterfaceNumber;
> > -                u_int8_t        bAlternateSetting;
> > -                u_int8_t        bNumEndpoints;
> > -                u_int8_t        bInterfaceClass;
> > -                u_int8_t        bInterfaceSubClass;
> > -                u_int8_t        bInterfaceProtocol;
> > -                u_int8_t        iInterface;
> > +                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".
>
> Common misunderstanding about strict aliasing. if accessing as char * yes,
but not the other way around. See the C11 standard which makes it clear
(don't have page number off the top of my head...)

> [1]
> https://gcc.gnu.org/onlinedocs/gcc-4.6.1/gcc/Optimize-Options.html#index-fstrict_002daliasing-825
>
> Zbyszek
> _______________________________________________
> 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/20150304/e01cda2f/attachment.html>


More information about the systemd-devel mailing list