dmitrystu / libusb_stm32

Lightweight USB device Stack for STM32 microcontrollers
Apache License 2.0
707 stars 160 forks source link

Packed struct alignment warning for usb_string_descriptor #41

Closed zbrozek closed 5 years ago

zbrozek commented 5 years ago

When compiling I was getting this warning:

usbd_stm32f429_otgfs.c:450:21: warning: taking address of packed member of 'struct usb_string_descriptor' may result in an unaligned pointer value [-Waddress-of-packed-member]
  450 |     uint16_t *str = dsc->wString;
      |                     ^~~

This can be fixed by changing the declaration of struct usb_string_descriptor in usb_std.h to include __attribute__ ((aligned (4)))

As such:

struct usb_string_descriptor {                                                     
    uint8_t  bLength;               /**<\brief Size of the descriptor, in bytes.*/
    uint8_t  bDescriptorType;       /**<\brief String descriptor type.*/           
    uint16_t wString[];             /**<\brief String data, as unicode characters or array of
                                     * \ref USB_STD_LANGID codes. */               
} __attribute__((packed)) __attribute__ ((aligned (4)));

Not sure if there are negative consequences to doing that.

dmitrystu commented 5 years ago

I see no negative consequences here. BTW __attribute__((aligned (2)) also must be ok.