usb: composite: fix possible alignment issues 33/221633/1
authorSimon Goldschmidt <simon.k.r.goldschmidt@gmail.com>
Thu, 21 Nov 2019 21:15:22 +0000 (22:15 +0100)
committerSeung-Woo Kim <sw0312.kim@samsung.com>
Mon, 6 Jan 2020 10:53:54 +0000 (19:53 +0900)
Since upgrading to gcc9, warnings are issued:
"taking address of packed member of ‘...’ may result in an unaligned
pointer value"

Fix this by converting two functions to use unaligned access since packed
structures may be on an unaligned address, depending on USB hardware.

Signed-off-by: Simon Goldschmidt <simon.k.r.goldschmidt@gmail.com>
[sw0312.kim: backport mainline commit 616ebd8b9cb4 to remove gcc 9 build warning]
Signed-off-by: Seung-Woo Kim <sw0312.kim@samsung.com>
Change-Id: I94fe77fc246a9e9e862bf922b5f3afe39b697968

drivers/usb/gadget/composite.c

index d8db6c08e0a7a3b6331c23df41836c1daef28894..57d6bcfbbdeec3b713878d75ba9b7df224075de8 100644 (file)
 
 #define USB_BUFSIZ     4096
 
+/* Helper type for accessing packed u16 pointers */
+typedef struct { __le16 val; } __packed __le16_packed;
+
 static struct usb_composite_driver *composite;
 
+static inline void le16_add_cpu_packed(__le16_packed *var, u16 val)
+{
+       var->val = cpu_to_le16(le16_to_cpu(var->val) + val);
+}
+
 /**
  * usb_add_function() - add a function to a configuration
  * @config: the configuration
@@ -481,20 +489,21 @@ done:
  * the host side.
  */
 
-static void collect_langs(struct usb_gadget_strings **sp, __le16 *buf)
+static void collect_langs(struct usb_gadget_strings **sp, void *buf)
 {
        const struct usb_gadget_strings *s;
        u16                             language;
-       __le16                          *tmp;
+       __le16_packed                   *tmp;
+       __le16_packed                   *end = (buf + 252);
 
        while (*sp) {
                s = *sp;
                language = cpu_to_le16(s->language);
-               for (tmp = buf; *tmp && tmp < &buf[126]; tmp++) {
-                       if (*tmp == language)
+               for (tmp = buf; tmp->val && tmp < end; tmp++) {
+                       if (tmp->val == language)
                                goto repeat;
                }
-               *tmp++ = language;
+               tmp->val = language;
 repeat:
                sp++;
        }
@@ -706,7 +715,8 @@ static int bos_desc(struct usb_composite_dev *cdev)
         */
        usb_ext = cdev->req->buf + le16_to_cpu(bos->wTotalLength);
        bos->bNumDeviceCaps++;
-       le16_add_cpu(&bos->wTotalLength, USB_DT_USB_EXT_CAP_SIZE);
+       le16_add_cpu_packed((__le16_packed *)&bos->wTotalLength,
+                           USB_DT_USB_EXT_CAP_SIZE);
        usb_ext->bLength = USB_DT_USB_EXT_CAP_SIZE;
        usb_ext->bDescriptorType = USB_DT_DEVICE_CAPABILITY;
        usb_ext->bDevCapabilityType = USB_CAP_TYPE_EXT;
@@ -722,7 +732,8 @@ static int bos_desc(struct usb_composite_dev *cdev)
 
                ss_cap = cdev->req->buf + le16_to_cpu(bos->wTotalLength);
                bos->bNumDeviceCaps++;
-               le16_add_cpu(&bos->wTotalLength, USB_DT_USB_SS_CAP_SIZE);
+               le16_add_cpu_packed((__le16_packed *)&bos->wTotalLength,
+                                   USB_DT_USB_SS_CAP_SIZE);
                ss_cap->bLength = USB_DT_USB_SS_CAP_SIZE;
                ss_cap->bDescriptorType = USB_DT_DEVICE_CAPABILITY;
                ss_cap->bDevCapabilityType = USB_SS_CAP_TYPE;