Skip to content

Commit

Permalink
Bluetooth: Replace zero-length array with flexible-array member
Browse files Browse the repository at this point in the history
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 7649773 ("cxgb3/l2t: Fix undefined behaviour")

Signed-off-by: Gustavo A. R. Silva <gustavo@embeddedor.com>
Signed-off-by: Marcel Holtmann <marcel@holtmann.org>
  • Loading branch information
Gustavo A. R. Silva authored and Marcel Holtmann committed Feb 28, 2020
1 parent eed467b commit a7e4545
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 36 deletions.
6 changes: 3 additions & 3 deletions drivers/bluetooth/btqca.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ struct qca_fw_config {
struct edl_event_hdr {
__u8 cresp;
__u8 rtype;
__u8 data[0];
__u8 data[];
} __packed;

struct qca_btsoc_version {
Expand Down Expand Up @@ -112,12 +112,12 @@ struct tlv_type_nvm {
__le16 tag_len;
__le32 reserve1;
__le32 reserve2;
__u8 data[0];
__u8 data[];
} __packed;

struct tlv_type_hdr {
__le32 type_len;
__u8 data[0];
__u8 data[];
} __packed;

enum qca_btsoc_type {
Expand Down
4 changes: 2 additions & 2 deletions drivers/bluetooth/btrtl.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ struct rtl_epatch_header {
struct rtl_vendor_config_entry {
__le16 offset;
__u8 len;
__u8 data[0];
__u8 data[];
} __packed;

struct rtl_vendor_config {
__le32 signature;
__le16 total_len;
struct rtl_vendor_config_entry entry[0];
struct rtl_vendor_config_entry entry[];
} __packed;

#if IS_ENABLED(CONFIG_BT_RTL)
Expand Down
30 changes: 15 additions & 15 deletions include/net/bluetooth/hci.h
Original file line number Diff line number Diff line change
Expand Up @@ -935,7 +935,7 @@ struct hci_cp_sniff_subrate {
struct hci_cp_set_event_flt {
__u8 flt_type;
__u8 cond_type;
__u8 condition[0];
__u8 condition[];
} __packed;

/* Filter types */
Expand Down Expand Up @@ -1335,15 +1335,15 @@ struct hci_rp_read_local_amp_assoc {
__u8 status;
__u8 phy_handle;
__le16 rem_len;
__u8 frag[0];
__u8 frag[];
} __packed;

#define HCI_OP_WRITE_REMOTE_AMP_ASSOC 0x140b
struct hci_cp_write_remote_amp_assoc {
__u8 phy_handle;
__le16 len_so_far;
__le16 rem_len;
__u8 frag[0];
__u8 frag[];
} __packed;
struct hci_rp_write_remote_amp_assoc {
__u8 status;
Expand Down Expand Up @@ -1613,7 +1613,7 @@ struct hci_cp_le_set_ext_scan_params {
__u8 own_addr_type;
__u8 filter_policy;
__u8 scanning_phys;
__u8 data[0];
__u8 data[];
} __packed;

#define LE_SCAN_PHY_1M 0x01
Expand Down Expand Up @@ -1641,7 +1641,7 @@ struct hci_cp_le_ext_create_conn {
__u8 peer_addr_type;
bdaddr_t peer_addr;
__u8 phys;
__u8 data[0];
__u8 data[];
} __packed;

struct hci_cp_le_ext_conn_param {
Expand Down Expand Up @@ -1693,7 +1693,7 @@ struct hci_rp_le_set_ext_adv_params {
struct hci_cp_le_set_ext_adv_enable {
__u8 enable;
__u8 num_of_sets;
__u8 data[0];
__u8 data[];
} __packed;

struct hci_cp_ext_adv_set {
Expand Down Expand Up @@ -1775,14 +1775,14 @@ struct hci_cp_le_set_cig_params {
__le16 m_latency;
__le16 s_latency;
__u8 num_cis;
struct hci_cis_params cis[0];
struct hci_cis_params cis[];
} __packed;

struct hci_rp_le_set_cig_params {
__u8 status;
__u8 cig_id;
__u8 num_handles;
__le16 handle[0];
__le16 handle[];
} __packed;

#define HCI_OP_LE_CREATE_CIS 0x2064
Expand All @@ -1793,7 +1793,7 @@ struct hci_cis {

struct hci_cp_le_create_cis {
__u8 num_cis;
struct hci_cis cis[0];
struct hci_cis cis[];
} __packed;

#define HCI_OP_LE_REMOVE_CIG 0x2065
Expand Down Expand Up @@ -1937,7 +1937,7 @@ struct hci_comp_pkts_info {

struct hci_ev_num_comp_pkts {
__u8 num_hndl;
struct hci_comp_pkts_info handles[0];
struct hci_comp_pkts_info handles[];
} __packed;

#define HCI_EV_MODE_CHANGE 0x14
Expand Down Expand Up @@ -2170,7 +2170,7 @@ struct hci_comp_blocks_info {
struct hci_ev_num_comp_blocks {
__le16 num_blocks;
__u8 num_hndl;
struct hci_comp_blocks_info handles[0];
struct hci_comp_blocks_info handles[];
} __packed;

#define HCI_EV_SYNC_TRAIN_COMPLETE 0x4F
Expand Down Expand Up @@ -2226,7 +2226,7 @@ struct hci_ev_le_advertising_info {
__u8 bdaddr_type;
bdaddr_t bdaddr;
__u8 length;
__u8 data[0];
__u8 data[];
} __packed;

#define HCI_EV_LE_CONN_UPDATE_COMPLETE 0x03
Expand Down Expand Up @@ -2302,7 +2302,7 @@ struct hci_ev_le_ext_adv_report {
__u8 direct_addr_type;
bdaddr_t direct_addr;
__u8 length;
__u8 data[0];
__u8 data[];
} __packed;

#define HCI_EV_LE_ENHANCED_CONN_COMPLETE 0x0a
Expand Down Expand Up @@ -2362,7 +2362,7 @@ struct hci_evt_le_cis_req {
#define HCI_EV_STACK_INTERNAL 0xfd
struct hci_ev_stack_internal {
__u16 type;
__u8 data[0];
__u8 data[];
} __packed;

#define HCI_EV_SI_DEVICE 0x01
Expand Down Expand Up @@ -2409,7 +2409,7 @@ struct hci_sco_hdr {
struct hci_iso_hdr {
__le16 handle;
__le16 dlen;
__u8 data[0];
__u8 data[];
} __packed;

/* ISO data packet status flags */
Expand Down
6 changes: 3 additions & 3 deletions include/net/bluetooth/hci_sock.h
Original file line number Diff line number Diff line change
Expand Up @@ -144,19 +144,19 @@ struct hci_dev_req {

struct hci_dev_list_req {
__u16 dev_num;
struct hci_dev_req dev_req[0]; /* hci_dev_req structures */
struct hci_dev_req dev_req[]; /* hci_dev_req structures */
};

struct hci_conn_list_req {
__u16 dev_id;
__u16 conn_num;
struct hci_conn_info conn_info[0];
struct hci_conn_info conn_info[];
};

struct hci_conn_info_req {
bdaddr_t bdaddr;
__u8 type;
struct hci_conn_info conn_info[0];
struct hci_conn_info conn_info[];
};

struct hci_auth_info_req {
Expand Down
8 changes: 4 additions & 4 deletions include/net/bluetooth/l2cap.h
Original file line number Diff line number Diff line change
Expand Up @@ -299,14 +299,14 @@ struct l2cap_conn_rsp {
struct l2cap_conf_req {
__le16 dcid;
__le16 flags;
__u8 data[0];
__u8 data[];
} __packed;

struct l2cap_conf_rsp {
__le16 scid;
__le16 flags;
__le16 result;
__u8 data[0];
__u8 data[];
} __packed;

#define L2CAP_CONF_SUCCESS 0x0000
Expand All @@ -322,7 +322,7 @@ struct l2cap_conf_rsp {
struct l2cap_conf_opt {
__u8 type;
__u8 len;
__u8 val[0];
__u8 val[];
} __packed;
#define L2CAP_CONF_OPT_SIZE 2

Expand Down Expand Up @@ -392,7 +392,7 @@ struct l2cap_info_req {
struct l2cap_info_rsp {
__le16 type;
__le16 result;
__u8 data[0];
__u8 data[];
} __packed;

struct l2cap_create_chan_req {
Expand Down
2 changes: 1 addition & 1 deletion include/net/bluetooth/rfcomm.h
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ struct rfcomm_dev_info {

struct rfcomm_dev_list_req {
u16 dev_num;
struct rfcomm_dev_info dev_info[0];
struct rfcomm_dev_info dev_info[];
};

int rfcomm_dev_ioctl(struct sock *sk, unsigned int cmd, void __user *arg);
Expand Down
10 changes: 5 additions & 5 deletions net/bluetooth/a2mp.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ struct a2mp_cmd {
__u8 code;
__u8 ident;
__le16 len;
__u8 data[0];
__u8 data[];
} __packed;

/* A2MP command codes */
#define A2MP_COMMAND_REJ 0x01
struct a2mp_cmd_rej {
__le16 reason;
__u8 data[0];
__u8 data[];
} __packed;

#define A2MP_DISCOVER_REQ 0x02
Expand All @@ -62,7 +62,7 @@ struct a2mp_cl {
struct a2mp_discov_rsp {
__le16 mtu;
__le16 ext_feat;
struct a2mp_cl cl[0];
struct a2mp_cl cl[];
} __packed;

#define A2MP_CHANGE_NOTIFY 0x04
Expand Down Expand Up @@ -93,15 +93,15 @@ struct a2mp_amp_assoc_req {
struct a2mp_amp_assoc_rsp {
__u8 id;
__u8 status;
__u8 amp_assoc[0];
__u8 amp_assoc[];
} __packed;

#define A2MP_CREATEPHYSLINK_REQ 0x0A
#define A2MP_DISCONNPHYSLINK_REQ 0x0C
struct a2mp_physlink_req {
__u8 local_id;
__u8 remote_id;
__u8 amp_assoc[0];
__u8 amp_assoc[];
} __packed;

#define A2MP_CREATEPHYSLINK_RSP 0x0B
Expand Down
6 changes: 3 additions & 3 deletions net/bluetooth/bnep/bnep.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,14 @@ struct bnep_setup_conn_req {
__u8 type;
__u8 ctrl;
__u8 uuid_size;
__u8 service[0];
__u8 service[];
} __packed;

struct bnep_set_filter_req {
__u8 type;
__u8 ctrl;
__be16 len;
__u8 list[0];
__u8 list[];
} __packed;

struct bnep_control_rsp {
Expand All @@ -93,7 +93,7 @@ struct bnep_control_rsp {
struct bnep_ext_hdr {
__u8 type;
__u8 len;
__u8 data[0];
__u8 data[];
} __packed;

/* BNEP ioctl defines */
Expand Down

0 comments on commit a7e4545

Please sign in to comment.