History

USB: Replace zero-length array with flexible-array member

The current codebase makes use of the zero-length array language extension to the C90 standard, but the preferred mechanism to declare variable-length types such as these ones is a flexible array member[1][2], introduced in C99: struct foo { int stuff; struct boo array[]; }; By making use of the mechanism above, we will get a compiler warning in case the flexible array does not occur last in the structure, which will help us prevent some kind of undefined behavior bugs from being inadvertently introduced[3] to the codebase from now on. Also, notice that, dynamic memory allocations won't be affected by this change: "Flexible array members have incomplete type, and so the sizeof operator may not be applied. As a quirk of the original implementation of zero-length arrays, sizeof evaluates to zero."[1] This issue was found with the help of Coccinelle. [1] https://gcc.gnu.org/onlinedocs/gcc/Zero-Length.html [2] https://github.com/KSPP/linux/issues/21 [3] commit 76497732932f ("cxgb3/l2t: Fix undefined behaviour")

Gustavo A. R. Silva committed 6 years ago

Commit 6bc3f391 parent 2f41c8a16 files · +22 −22Browse files

Showing the changes to include/linux (6 of 16 files). Show all

Unified · Split

6 files
  1. include/linux/usb.h +2 −2
  2. include/linux/usb/audio-v2.h +1 −1
  3. include/linux/usb/audio-v3.h +1 −1
  4. include/linux/usb/gadget.h +1 −1
  5. include/linux/usb/hcd.h +1 −1
  6. include/linux/usbdevice_fs.h +1 −1
include/linux/usb.h+2−2
@@ -325,7 +325,7 @@ struct usb_interface_cache { · 324 unmodified lines
325325
326326 /* variable-length array of alternate settings for this interface,
327327 * stored in no particular order */
328− struct usb_host_interface altsetting[0];
328+ struct usb_host_interface altsetting[];
329329 };
330330 #define ref_to_usb_interface_cache(r) \
331331 container_of(r, struct usb_interface_cache, ref)
@@ -1589,7 +1589,7 @@ struct urb { · 1257 unmodified lines
15891589 int error_count; /* (return) number of ISO errors */
15901590 void *context; /* (in) context for completion */
15911591 usb_complete_t complete; /* (in) completion routine */
1592− struct usb_iso_packet_descriptor iso_frame_desc[0];
1592+ struct usb_iso_packet_descriptor iso_frame_desc[];
15931593 /* (in) ISO ONLY */
15941594 };
15951595
include/linux/usb/audio-v2.h+1−1
@@ -153,7 +153,7 @@ struct uac2_feature_unit_descriptor { · 152 unmodified lines
153153 __u8 bSourceID;
154154 /* bmaControls is actually u32,
155155 * but u8 is needed for the hybrid parser */
156− __u8 bmaControls[0]; /* variable length */
156+ __u8 bmaControls[]; /* variable length */
157157 } __attribute__((packed));
158158
159159 /* 4.9.2 Class-Specific AS Interface Descriptor */
include/linux/usb/audio-v3.h+1−1
@@ -109,7 +109,7 @@ struct uac3_feature_unit_descriptor { · 108 unmodified lines
109109 __u8 bSourceID;
110110 /* bmaControls is actually u32,
111111 * but u8 is needed for the hybrid parser */
112− __u8 bmaControls[0]; /* variable length */
112+ __u8 bmaControls[]; /* variable length */
113113 /* wFeatureDescrStr omitted */
114114 } __attribute__((packed));
115115
include/linux/usb/gadget.h+1−1
@@ -767,7 +767,7 @@ struct usb_gadget_strings { · 766 unmodified lines
767767
768768 struct usb_gadget_string_container {
769769 struct list_head list;
770− u8 *stash[0];
770+ u8 *stash[];
771771 };
772772
773773 /* put descriptor for string with that id into buf (buflen >= 256) */
include/linux/usb/hcd.h+1−1
@@ -228,7 +228,7 @@ struct usb_hcd { · 227 unmodified lines
228228 /* The HC driver's private data is stored at the end of
229229 * this structure.
230230 */
231− unsigned long hcd_priv[0]
231+ unsigned long hcd_priv[]
232232 __attribute__ ((aligned(sizeof(s64))));
233233 };
234234
include/linux/usbdevice_fs.h+1−1
@@ -69,7 +69,7 @@ struct usbdevfs_urb32 { · 68 unmodified lines
6969 compat_int_t error_count;
7070 compat_uint_t signr;
7171 compat_caddr_t usercontext; /* unused */
72− struct usbdevfs_iso_packet_desc iso_frame_desc[0];
72+ struct usbdevfs_iso_packet_desc iso_frame_desc[];
7373 };
7474
7575 struct usbdevfs_ioctl32 {

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t