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

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

Signed-off-by: Gustavo A. R. Silva <gustavo@embeddedor.com>
Link: https://lore.kernel.org/r/20200220132908.GA30501@embeddedor
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

authored by

Gustavo A. R. Silva and committed by
Greg Kroah-Hartman
5979afa2 cf7e44da

+52 -52
+1 -1
drivers/staging/gdm724x/gdm_mux.h
··· 29 29 __le32 seq_num; 30 30 __le32 payload_size; 31 31 __le16 packet_type; 32 - unsigned char data[0]; 32 + unsigned char data[]; 33 33 }; 34 34 35 35 struct mux_tx {
+3 -3
drivers/staging/gdm724x/hci_packet.h
··· 28 28 struct hci_packet { 29 29 __dev16 cmd_evt; 30 30 __dev16 len; 31 - u8 data[0]; 31 + u8 data[]; 32 32 } __packed; 33 33 34 34 struct tlv { ··· 51 51 __dev32 dft_eps_ID; 52 52 __dev32 bearer_ID; 53 53 __dev32 nic_type; 54 - u8 data[0]; 54 + u8 data[]; 55 55 } __packed; 56 56 57 57 struct multi_sdu { ··· 59 59 __dev16 len; 60 60 __dev16 num_packet; 61 61 __dev16 reserved; 62 - u8 data[0]; 62 + u8 data[]; 63 63 } __packed; 64 64 65 65 struct hci_pdn_table_ind {
+1 -1
drivers/staging/greybus/audio_apbridgea.h
··· 65 65 struct audio_apbridgea_hdr { 66 66 __u8 type; 67 67 __le16 i2s_port; 68 - __u8 data[0]; 68 + __u8 data[]; 69 69 } __packed; 70 70 71 71 struct audio_apbridgea_set_config_request {
+2 -2
drivers/staging/ks7010/ks_hostif.h
··· 70 70 #define TYPE_DATA 0x0000 71 71 #define TYPE_AUTH 0x0001 72 72 __le16 reserved; 73 - u8 data[0]; 73 + u8 data[]; 74 74 } __packed; 75 75 76 76 #define TYPE_PMK1 0x0001 ··· 194 194 struct hostif_mib_value { 195 195 __le16 size; 196 196 __le16 type; 197 - u8 body[0]; 197 + u8 body[]; 198 198 } __packed; 199 199 200 200 struct hostif_mib_get_confirm_t {
+1 -1
drivers/staging/media/allegro-dvt/allegro-core.c
··· 434 434 struct mcu_msg_push_buffers_internal { 435 435 struct mcu_msg_header header; 436 436 u32 channel_id; 437 - struct mcu_msg_push_buffers_internal_buffer buffer[0]; 437 + struct mcu_msg_push_buffers_internal_buffer buffer[]; 438 438 } __attribute__ ((__packed__)); 439 439 440 440 struct mcu_msg_put_stream_buffer {
+1 -1
drivers/staging/octeon-usb/octeon-hcd.c
··· 406 406 */ 407 407 struct octeon_temp_buffer { 408 408 void *orig_buffer; 409 - u8 data[0]; 409 + u8 data[]; 410 410 }; 411 411 412 412 static inline struct usb_hcd *octeon_to_hcd(struct octeon_hcd *p)
+15 -15
drivers/staging/rtl8192e/rtllib.h
··· 728 728 struct rtllib_hdr { 729 729 __le16 frame_ctl; 730 730 __le16 duration_id; 731 - u8 payload[0]; 731 + u8 payload[]; 732 732 } __packed; 733 733 734 734 struct rtllib_hdr_1addr { 735 735 __le16 frame_ctl; 736 736 __le16 duration_id; 737 737 u8 addr1[ETH_ALEN]; 738 - u8 payload[0]; 738 + u8 payload[]; 739 739 } __packed; 740 740 741 741 struct rtllib_hdr_2addr { ··· 743 743 __le16 duration_id; 744 744 u8 addr1[ETH_ALEN]; 745 745 u8 addr2[ETH_ALEN]; 746 - u8 payload[0]; 746 + u8 payload[]; 747 747 } __packed; 748 748 749 749 struct rtllib_hdr_3addr { ··· 753 753 u8 addr2[ETH_ALEN]; 754 754 u8 addr3[ETH_ALEN]; 755 755 __le16 seq_ctl; 756 - u8 payload[0]; 756 + u8 payload[]; 757 757 } __packed; 758 758 759 759 struct rtllib_hdr_4addr { ··· 764 764 u8 addr3[ETH_ALEN]; 765 765 __le16 seq_ctl; 766 766 u8 addr4[ETH_ALEN]; 767 - u8 payload[0]; 767 + u8 payload[]; 768 768 } __packed; 769 769 770 770 struct rtllib_hdr_3addrqos { ··· 775 775 u8 addr3[ETH_ALEN]; 776 776 __le16 seq_ctl; 777 777 __le16 qos_ctl; 778 - u8 payload[0]; 778 + u8 payload[]; 779 779 } __packed; 780 780 781 781 struct rtllib_hdr_4addrqos { ··· 787 787 __le16 seq_ctl; 788 788 u8 addr4[ETH_ALEN]; 789 789 __le16 qos_ctl; 790 - u8 payload[0]; 790 + u8 payload[]; 791 791 } __packed; 792 792 793 793 struct rtllib_info_element { 794 794 u8 id; 795 795 u8 len; 796 - u8 data[0]; 796 + u8 data[]; 797 797 } __packed; 798 798 799 799 struct rtllib_authentication { ··· 802 802 __le16 transaction; 803 803 __le16 status; 804 804 /*challenge*/ 805 - struct rtllib_info_element info_element[0]; 805 + struct rtllib_info_element info_element[]; 806 806 } __packed; 807 807 808 808 struct rtllib_disauth { ··· 818 818 struct rtllib_probe_request { 819 819 struct rtllib_hdr_3addr header; 820 820 /* SSID, supported rates */ 821 - struct rtllib_info_element info_element[0]; 821 + struct rtllib_info_element info_element[]; 822 822 } __packed; 823 823 824 824 struct rtllib_probe_response { ··· 829 829 /* SSID, supported rates, FH params, DS params, 830 830 * CF params, IBSS params, TIM (if beacon), RSN 831 831 */ 832 - struct rtllib_info_element info_element[0]; 832 + struct rtllib_info_element info_element[]; 833 833 } __packed; 834 834 835 835 /* Alias beacon for probe_response */ ··· 840 840 __le16 capability; 841 841 __le16 listen_interval; 842 842 /* SSID, supported rates, RSN */ 843 - struct rtllib_info_element info_element[0]; 843 + struct rtllib_info_element info_element[]; 844 844 } __packed; 845 845 846 846 struct rtllib_assoc_response_frame { ··· 848 848 __le16 capability; 849 849 __le16 status; 850 850 __le16 aid; 851 - struct rtllib_info_element info_element[0]; /* supported rates */ 851 + struct rtllib_info_element info_element[]; /* supported rates */ 852 852 } __packed; 853 853 854 854 struct rtllib_txb { ··· 859 859 u16 reserved; 860 860 __le16 frag_size; 861 861 __le16 payload_size; 862 - struct sk_buff *fragments[0]; 862 + struct sk_buff *fragments[]; 863 863 }; 864 864 865 865 #define MAX_SUBFRAME_COUNT 64 ··· 1792 1792 /* This must be the last item so that it points to the data 1793 1793 * allocated beyond this structure by alloc_rtllib 1794 1794 */ 1795 - u8 priv[0]; 1795 + u8 priv[]; 1796 1796 }; 1797 1797 1798 1798 #define IEEE_A (1<<0)
+14 -14
drivers/staging/rtl8192u/ieee80211/ieee80211.h
··· 886 886 struct rtl_80211_hdr { 887 887 __le16 frame_ctl; 888 888 __le16 duration_id; 889 - u8 payload[0]; 889 + u8 payload[]; 890 890 } __packed; 891 891 892 892 struct rtl_80211_hdr_1addr { 893 893 __le16 frame_ctl; 894 894 __le16 duration_id; 895 895 u8 addr1[ETH_ALEN]; 896 - u8 payload[0]; 896 + u8 payload[]; 897 897 } __packed; 898 898 899 899 struct rtl_80211_hdr_2addr { ··· 901 901 __le16 duration_id; 902 902 u8 addr1[ETH_ALEN]; 903 903 u8 addr2[ETH_ALEN]; 904 - u8 payload[0]; 904 + u8 payload[]; 905 905 } __packed; 906 906 907 907 struct rtl_80211_hdr_3addr { ··· 911 911 u8 addr2[ETH_ALEN]; 912 912 u8 addr3[ETH_ALEN]; 913 913 __le16 seq_ctl; 914 - u8 payload[0]; 914 + u8 payload[]; 915 915 } __packed; 916 916 917 917 struct rtl_80211_hdr_4addr { ··· 922 922 u8 addr3[ETH_ALEN]; 923 923 __le16 seq_ctl; 924 924 u8 addr4[ETH_ALEN]; 925 - u8 payload[0]; 925 + u8 payload[]; 926 926 } __packed; 927 927 928 928 struct rtl_80211_hdr_3addrqos { ··· 951 951 struct ieee80211_info_element { 952 952 u8 id; 953 953 u8 len; 954 - u8 data[0]; 954 + u8 data[]; 955 955 } __packed; 956 956 957 957 struct ieee80211_authentication { ··· 960 960 __le16 transaction; 961 961 __le16 status; 962 962 /*challenge*/ 963 - struct ieee80211_info_element info_element[0]; 963 + struct ieee80211_info_element info_element[]; 964 964 } __packed; 965 965 966 966 struct ieee80211_disassoc { ··· 971 971 struct ieee80211_probe_request { 972 972 struct rtl_80211_hdr_3addr header; 973 973 /* SSID, supported rates */ 974 - struct ieee80211_info_element info_element[0]; 974 + struct ieee80211_info_element info_element[]; 975 975 } __packed; 976 976 977 977 struct ieee80211_probe_response { ··· 982 982 /* SSID, supported rates, FH params, DS params, 983 983 * CF params, IBSS params, TIM (if beacon), RSN 984 984 */ 985 - struct ieee80211_info_element info_element[0]; 985 + struct ieee80211_info_element info_element[]; 986 986 } __packed; 987 987 988 988 /* Alias beacon for probe_response */ ··· 993 993 __le16 capability; 994 994 __le16 listen_interval; 995 995 /* SSID, supported rates, RSN */ 996 - struct ieee80211_info_element info_element[0]; 996 + struct ieee80211_info_element info_element[]; 997 997 } __packed; 998 998 999 999 struct ieee80211_reassoc_request_frame { ··· 1002 1002 __le16 listen_interval; 1003 1003 u8 current_ap[ETH_ALEN]; 1004 1004 /* SSID, supported rates, RSN */ 1005 - struct ieee80211_info_element info_element[0]; 1005 + struct ieee80211_info_element info_element[]; 1006 1006 } __packed; 1007 1007 1008 1008 struct ieee80211_assoc_response_frame { ··· 1010 1010 __le16 capability; 1011 1011 __le16 status; 1012 1012 __le16 aid; 1013 - struct ieee80211_info_element info_element[0]; /* supported rates */ 1013 + struct ieee80211_info_element info_element[]; /* supported rates */ 1014 1014 } __packed; 1015 1015 1016 1016 struct ieee80211_txb { ··· 1021 1021 u16 reserved; 1022 1022 __le16 frag_size; 1023 1023 __le16 payload_size; 1024 - struct sk_buff *fragments[0]; 1024 + struct sk_buff *fragments[]; 1025 1025 }; 1026 1026 1027 1027 #define MAX_TX_AGG_COUNT 16 ··· 2007 2007 /* This must be the last item so that it points to the data 2008 2008 * allocated beyond this structure by alloc_ieee80211 2009 2009 */ 2010 - u8 priv[0]; 2010 + u8 priv[]; 2011 2011 }; 2012 2012 2013 2013 #define IEEE_A (1<<0)
+2 -2
drivers/staging/rtl8712/ieee80211.h
··· 535 535 struct ieee80211_info_element { 536 536 u8 id; 537 537 u8 len; 538 - u8 data[0]; 538 + u8 data[]; 539 539 } __packed; 540 540 541 541 /* ··· 597 597 u16 reserved; 598 598 u16 frag_size; 599 599 u16 payload_size; 600 - struct sk_buff *fragments[0]; 600 + struct sk_buff *fragments[]; 601 601 }; 602 602 603 603 /* SWEEP TABLE ENTRIES NUMBER*/
+2 -2
drivers/staging/rtl8712/rtl871x_mp_ioctl.h
··· 48 48 struct EFUSE_ACCESS_STRUCT { 49 49 u16 start_addr; 50 50 u16 cnts; 51 - u8 data[0]; 51 + u8 data[]; 52 52 }; 53 53 54 54 struct burst_rw_reg { ··· 324 324 struct mp_ioctl_param { 325 325 unsigned int subcode; 326 326 unsigned int len; 327 - unsigned char data[0]; 327 + unsigned char data[]; 328 328 }; 329 329 330 330 #define GEN_MP_IOCTL_SUBCODE(code) _MP_IOCTL_ ## code ## _CMD_
+5 -5
drivers/staging/wilc1000/cfg80211.c
··· 62 62 u8 oui_type; 63 63 u8 oui_subtype; 64 64 u8 dialog_token; 65 - u8 elem[0]; 65 + u8 elem[]; 66 66 } __packed; 67 67 68 68 struct wilc_vendor_specific_ie { ··· 70 70 u8 tag_len; 71 71 u8 oui[3]; 72 72 u8 oui_type; 73 - u8 attr[0]; 73 + u8 attr[]; 74 74 } __packed; 75 75 76 76 struct wilc_attr_entry { 77 77 u8 attr_type; 78 78 __le16 attr_len; 79 - u8 val[0]; 79 + u8 val[]; 80 80 } __packed; 81 81 82 82 struct wilc_attr_oper_ch { ··· 91 91 u8 attr_type; 92 92 __le16 attr_len; 93 93 u8 country_code[IEEE80211_COUNTRY_STRING_LEN]; 94 - u8 elem[0]; 94 + u8 elem[]; 95 95 } __packed; 96 96 97 97 struct wilc_ch_list_elem { 98 98 u8 op_class; 99 99 u8 no_of_channels; 100 - u8 ch_list[0]; 100 + u8 ch_list[]; 101 101 } __packed; 102 102 103 103 static void cfg_scan_result(enum scan_event scan_event,
+1 -1
drivers/staging/wilc1000/spi.c
··· 139 139 u8 status; 140 140 u8 resp_header; 141 141 u8 resp_data[4]; 142 - u8 crc[0]; 142 + u8 crc[]; 143 143 } __packed; 144 144 145 145 struct wilc_spi_rsp_data {
+2 -2
drivers/staging/wlan-ng/hfa384x.h
··· 355 355 /* Commonly used basic types */ 356 356 struct hfa384x_bytestr { 357 357 __le16 len; 358 - u8 data[0]; 358 + u8 data[]; 359 359 } __packed; 360 360 361 361 struct hfa384x_bytestr32 { ··· 421 421 /*-- Configuration Record: WPAData (data portion only) --*/ 422 422 struct hfa384x_wpa_data { 423 423 __le16 datalen; 424 - u8 data[0]; /* max 80 */ 424 + u8 data[]; /* max 80 */ 425 425 } __packed; 426 426 427 427 /*--------------------------------------------------------------------
+2 -2
drivers/staging/wlan-ng/p80211types.h
··· 204 204 205 205 struct p80211pstrd { 206 206 u8 len; 207 - u8 data[0]; 207 + u8 data[]; 208 208 } __packed; 209 209 210 210 /* Maximum pascal string */ ··· 249 249 u32 did; 250 250 u16 status; 251 251 u16 len; 252 - u8 data[0]; 252 + u8 data[]; 253 253 } __packed; 254 254 255 255 /* message data item for int, BOUNDEDINT, ENUMINT */