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

Zbigniew Jędrzejewski-Szmek zbyszek at in.waw.pl
Sun Mar 15 12:35:55 PDT 2015


On Wed, Mar 11, 2015 at 08:13:45AM -0700, Shawn Landden wrote:
> ---
>  src/udev/udev-builtin-usb_id.c | 16 ++++++++--------
>  1 file changed, 8 insertions(+), 8 deletions(-)
> 
> diff --git a/src/udev/udev-builtin-usb_id.c b/src/udev/udev-builtin-usb_id.c
> index 6516d93..3c15b2f 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"
>  
> @@ -179,21 +180,20 @@ 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;
> +                unsigned char *desc = &buf[pos];
>                  char if_str[8];
>  
> -                desc = (struct usb_interface_descriptor *) &buf[pos];
> -                if (desc->bLength < 3)
> +                if (desc[offsetof(struct usb_interface_descriptor, bLength)] < 3)
This makes the code really ugly. What about doing it the other way:

  #define BUF_SIZE (18 + 65535)
  struct usb_interface_descriptor *buf;
  buf = alloca(BUF_SIZE);
  ...
  read(fd, (char*) buf, BUF_SIZE);

If I understand the aliasing rules, this is OK, because:
C99 6.5 7:
An object shall have its stored value accessed only by an lvalue expression that has one of the following types:
  ...
  — a character type.

?

Zbyszek


More information about the systemd-devel mailing list