Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

greybus: Replace zero-length array with flexible-array

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]

sizeof(flexible-array-member) triggers a warning because flexible array
members have incomplete type[1]. There are some instances of code in
which the sizeof operator is being incorrectly/erroneously applied to
zero-length arrays and the result is zero. Such instances may be hiding
some bugs. So, this work (flexible-array member conversions) will also
help to get completely rid of those sorts of issues.

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")

Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org>
Link: https://lore.kernel.org/r/20200507185318.GA14393@embeddedor
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

authored by

Gustavo A. R. Silva and committed by
Greg Kroah-Hartman
84c1e51d dd92b013

+23 -23
+1 -1
drivers/greybus/arpc.h
··· 21 21 __le16 id; /* RPC unique id */ 22 22 __le16 size; /* Size in bytes of header + payload */ 23 23 __u8 type; /* RPC type */ 24 - __u8 data[0]; /* ARPC data */ 24 + __u8 data[]; /* ARPC data */ 25 25 } __packed; 26 26 27 27 struct arpc_response_message {
+22 -22
include/linux/greybus/greybus_protocols.h
··· 345 345 346 346 struct gb_cap_get_ims_certificate_response { 347 347 __u8 result_code; 348 - __u8 certificate[0]; 348 + __u8 certificate[]; 349 349 } __packed; 350 350 351 351 /* CAP authenticate request/response */ ··· 358 358 struct gb_cap_authenticate_response { 359 359 __u8 result_code; 360 360 __u8 response[64]; 361 - __u8 signature[0]; 361 + __u8 signature[]; 362 362 } __packed; 363 363 364 364 ··· 642 642 struct gb_hid_set_report_request { 643 643 __u8 report_type; 644 644 __u8 report_id; 645 - __u8 report[0]; 645 + __u8 report[]; 646 646 } __packed; 647 647 648 648 /* HID input report request, via interrupt pipe */ ··· 680 680 681 681 struct gb_i2c_transfer_request { 682 682 __le16 op_count; 683 - struct gb_i2c_transfer_op ops[0]; /* op_count of these */ 683 + struct gb_i2c_transfer_op ops[]; /* op_count of these */ 684 684 } __packed; 685 685 struct gb_i2c_transfer_response { 686 686 __u8 data[0]; /* inbound data */ ··· 908 908 __u8 chip_select; /* of the spi device */ 909 909 __u8 mode; /* of the spi device */ 910 910 __le16 count; 911 - struct gb_spi_transfer transfers[0]; /* count of these */ 911 + struct gb_spi_transfer transfers[]; /* count of these */ 912 912 } __packed; 913 913 914 914 struct gb_spi_transfer_response { ··· 1188 1188 1189 1189 struct gb_svc_pwrmon_rail_names_get_response { 1190 1190 __u8 status; 1191 - __u8 name[0][GB_SVC_PWRMON_RAIL_NAME_BUFSIZE]; 1191 + __u8 name[][GB_SVC_PWRMON_RAIL_NAME_BUFSIZE]; 1192 1192 } __packed; 1193 1193 1194 1194 #define GB_SVC_PWRMON_TYPE_CURR 0x01 ··· 1281 1281 1282 1282 struct gb_raw_send_request { 1283 1283 __le32 len; 1284 - __u8 data[0]; 1284 + __u8 data[]; 1285 1285 } __packed; 1286 1286 1287 1287 ··· 1300 1300 /* Represents data from AP -> Module */ 1301 1301 struct gb_uart_send_data_request { 1302 1302 __le16 size; 1303 - __u8 data[0]; 1303 + __u8 data[]; 1304 1304 } __packed; 1305 1305 1306 1306 /* recv-data-request flags */ ··· 1313 1313 struct gb_uart_recv_data_request { 1314 1314 __le16 size; 1315 1315 __u8 flags; 1316 - __u8 data[0]; 1316 + __u8 data[]; 1317 1317 } __packed; 1318 1318 1319 1319 struct gb_uart_receive_credits_request { ··· 1382 1382 __le32 len; 1383 1383 __le32 reserved0; 1384 1384 __le32 reserved1; 1385 - __u8 data[0]; 1385 + __u8 data[]; 1386 1386 } __packed; 1387 1387 1388 1388 struct gb_loopback_transfer_response { 1389 1389 __le32 len; 1390 1390 __le32 reserved0; 1391 1391 __le32 reserved1; 1392 - __u8 data[0]; 1392 + __u8 data[]; 1393 1393 } __packed; 1394 1394 1395 1395 /* SDIO */ ··· 1530 1530 1531 1531 __le16 data_blocks; 1532 1532 __le16 data_blksz; 1533 - __u8 data[0]; 1533 + __u8 data[]; 1534 1534 } __packed; 1535 1535 1536 1536 struct gb_sdio_transfer_response { 1537 1537 __le16 data_blocks; 1538 1538 __le16 data_blksz; 1539 - __u8 data[0]; 1539 + __u8 data[]; 1540 1540 } __packed; 1541 1541 1542 1542 /* event request: generated by module and is defined as unidirectional */ ··· 1572 1572 __u8 flags; 1573 1573 #define GB_CAMERA_CONFIGURE_STREAMS_TEST_ONLY 0x01 1574 1574 __le16 padding; 1575 - struct gb_camera_stream_config_request config[0]; 1575 + struct gb_camera_stream_config_request config[]; 1576 1576 } __packed; 1577 1577 1578 1578 /* Greybus Camera Configure Streams response payload */ ··· 1593 1593 __u8 flags; 1594 1594 __u8 padding[2]; 1595 1595 __le32 data_rate; 1596 - struct gb_camera_stream_config_response config[0]; 1596 + struct gb_camera_stream_config_response config[]; 1597 1597 }; 1598 1598 1599 1599 /* Greybus Camera Capture request payload - response has no payload */ ··· 1602 1602 __u8 streams; 1603 1603 __u8 padding; 1604 1604 __le16 num_frames; 1605 - __u8 settings[0]; 1605 + __u8 settings[]; 1606 1606 } __packed; 1607 1607 1608 1608 /* Greybus Camera Flush response payload - request has no payload */ ··· 1616 1616 __le16 frame_number; 1617 1617 __u8 stream; 1618 1618 __u8 padding; 1619 - __u8 metadata[0]; 1619 + __u8 metadata[]; 1620 1620 } __packed; 1621 1621 1622 1622 /* Lights */ ··· 1993 1993 struct gb_audio_enumerated { 1994 1994 __le32 items; 1995 1995 __le16 names_length; 1996 - __u8 names[0]; 1996 + __u8 names[]; 1997 1997 } __packed; 1998 1998 1999 1999 struct gb_audio_ctl_elem_info { /* See snd_ctl_elem_info in Linux source */ ··· 2033 2033 __u8 type; /* GB_AUDIO_WIDGET_TYPE_* */ 2034 2034 __u8 state; /* GB_AUDIO_WIDGET_STATE_* */ 2035 2035 __u8 ncontrols; 2036 - struct gb_audio_control ctl[0]; /* 'ncontrols' entries */ 2036 + struct gb_audio_control ctl[]; /* 'ncontrols' entries */ 2037 2037 } __packed; 2038 2038 2039 2039 struct gb_audio_route { ··· 2059 2059 * struct gb_audio_widget widgets[num_widgets]; 2060 2060 * struct gb_audio_route routes[num_routes]; 2061 2061 */ 2062 - __u8 data[0]; 2062 + __u8 data[]; 2063 2063 } __packed; 2064 2064 2065 2065 struct gb_audio_get_topology_size_response { ··· 2157 2157 2158 2158 struct gb_audio_send_data_request { 2159 2159 __le64 timestamp; 2160 - __u8 data[0]; 2160 + __u8 data[]; 2161 2161 } __packed; 2162 2162 2163 2163 ··· 2171 2171 2172 2172 struct gb_log_send_log_request { 2173 2173 __le16 len; 2174 - __u8 msg[0]; 2174 + __u8 msg[]; 2175 2175 } __packed; 2176 2176 2177 2177 #endif /* __GREYBUS_PROTOCOLS_H */