Skip to content

Commit da60fbe

Browse files
GustavoARSilvadavem330
authored andcommitted
NFC: 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] KSPP/linux#21 [3] commit 7649773 ("cxgb3/l2t: Fix undefined behaviour") Signed-off-by: Gustavo A. R. Silva <gustavo@embeddedor.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 6dd7f1a commit da60fbe

4 files changed

Lines changed: 11 additions & 11 deletions

File tree

‎drivers/nfc/fdp/fdp.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ static u8 nci_core_get_config_otp_ram_version[5] = {
7676
struct nci_core_get_config_rsp {
7777
u8 status;
7878
u8 count;
79-
u8 data[0];
79+
u8 data[];
8080
};
8181

8282
static int fdp_nci_create_conn(struct nci_dev *ndev)

‎drivers/nfc/st21nfca/dep.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ struct st21nfca_atr_req {
6666
u8 bsi;
6767
u8 bri;
6868
u8 ppi;
69-
u8 gbi[0];
69+
u8 gbi[];
7070
} __packed;
7171

7272
struct st21nfca_atr_res {
@@ -79,7 +79,7 @@ struct st21nfca_atr_res {
7979
u8 bri;
8080
u8 to;
8181
u8 ppi;
82-
u8 gbi[0];
82+
u8 gbi[];
8383
} __packed;
8484

8585
struct st21nfca_psl_req {

‎include/net/nfc/nci.h‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -244,13 +244,13 @@ struct dest_spec_params {
244244
struct core_conn_create_dest_spec_params {
245245
__u8 type;
246246
__u8 length;
247-
__u8 value[0];
247+
__u8 value[];
248248
} __packed;
249249

250250
struct nci_core_conn_create_cmd {
251251
__u8 destination_type;
252252
__u8 number_destination_params;
253-
struct core_conn_create_dest_spec_params params[0];
253+
struct core_conn_create_dest_spec_params params[];
254254
} __packed;
255255

256256
#define NCI_OP_CORE_CONN_CLOSE_CMD nci_opcode_pack(NCI_GID_CORE, 0x05)
@@ -321,7 +321,7 @@ struct nci_core_init_rsp_1 {
321321
__u8 status;
322322
__le32 nfcc_features;
323323
__u8 num_supported_rf_interfaces;
324-
__u8 supported_rf_interfaces[0]; /* variable size array */
324+
__u8 supported_rf_interfaces[]; /* variable size array */
325325
/* continuted in nci_core_init_rsp_2 */
326326
} __packed;
327327

@@ -338,7 +338,7 @@ struct nci_core_init_rsp_2 {
338338
struct nci_core_set_config_rsp {
339339
__u8 status;
340340
__u8 num_params;
341-
__u8 params_id[0]; /* variable size array */
341+
__u8 params_id[]; /* variable size array */
342342
} __packed;
343343

344344
#define NCI_OP_CORE_CONN_CREATE_RSP nci_opcode_pack(NCI_GID_CORE, 0x04)
@@ -501,18 +501,18 @@ struct nci_rf_nfcee_action_ntf {
501501
__u8 nfcee_id;
502502
__u8 trigger;
503503
__u8 supported_data_length;
504-
__u8 supported_data[0];
504+
__u8 supported_data[];
505505
} __packed;
506506

507507
#define NCI_OP_NFCEE_DISCOVER_NTF nci_opcode_pack(NCI_GID_NFCEE_MGMT, 0x00)
508508
struct nci_nfcee_supported_protocol {
509509
__u8 num_protocol;
510-
__u8 supported_protocol[0];
510+
__u8 supported_protocol[];
511511
} __packed;
512512

513513
struct nci_nfcee_information_tlv {
514514
__u8 num_tlv;
515-
__u8 information_tlv[0];
515+
__u8 information_tlv[];
516516
} __packed;
517517

518518
struct nci_nfcee_discover_ntf {

‎include/net/nfc/nfc.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ struct nfc_evt_transaction {
146146
u32 aid_len;
147147
u8 aid[NFC_MAX_AID_LENGTH];
148148
u8 params_len;
149-
u8 params[0];
149+
u8 params[];
150150
} __packed;
151151

152152
struct nfc_genl_data {

0 commit comments

Comments
 (0)