chelsio: Replace zero-length array with flexible-array member
authorGustavo A. R. Silva <gustavo@embeddedor.com>
Mon, 24 Feb 2020 16:30:24 +0000 (10:30 -0600)
committerDavid S. Miller <davem@davemloft.net>
Mon, 24 Feb 2020 23:26:17 +0000 (15:26 -0800)
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>
Signed-off-by: David S. Miller <davem@davemloft.net>
13 files changed:
drivers/net/ethernet/chelsio/cxgb3/cxgb3_ioctl.h
drivers/net/ethernet/chelsio/cxgb3/t3_cpl.h
drivers/net/ethernet/chelsio/cxgb4/clip_tbl.h
drivers/net/ethernet/chelsio/cxgb4/cudbg_entity.h
drivers/net/ethernet/chelsio/cxgb4/cxgb4_debugfs.c
drivers/net/ethernet/chelsio/cxgb4/cxgb4_debugfs.h
drivers/net/ethernet/chelsio/cxgb4/cxgb4_tc_u32_parse.h
drivers/net/ethernet/chelsio/cxgb4/l2t.c
drivers/net/ethernet/chelsio/cxgb4/sched.h
drivers/net/ethernet/chelsio/cxgb4/smt.h
drivers/net/ethernet/chelsio/cxgb4/t4_msg.h
drivers/net/ethernet/chelsio/cxgb4/t4fw_api.h
drivers/net/ethernet/chelsio/libcxgb/libcxgb_ppm.h

index b19e437..401827b 100644 (file)
@@ -79,7 +79,7 @@ struct ch_mem_range {
        uint32_t addr;
        uint32_t len;
        uint32_t version;
-       uint8_t buf[0];
+       uint8_t buf[];
 };
 
 struct ch_qset_params {
index 852c399..68bb5f3 100644 (file)
@@ -1448,7 +1448,7 @@ struct cpl_rdma_terminate {
 #endif
        __be32 msn;
        __be32 mo;
-       __u8 data[0];
+       __u8 data[];
 };
 
 /* cpl_rdma_terminate.tid_len fields */
index a0e0ae1..290c105 100644 (file)
@@ -29,7 +29,7 @@ struct clip_tbl {
        atomic_t nfree;
        struct list_head ce_free_head;
        void *cl_list;
-       struct list_head hash_list[0];
+       struct list_head hash_list[];
 };
 
 enum {
index f5be3ee..dcab94c 100644 (file)
@@ -82,7 +82,7 @@ struct cudbg_ulprx_la {
 struct cudbg_tp_la {
        u32 size;
        u32 mode;
-       u8 data[0];
+       u8 data[];
 };
 
 static const char * const cudbg_region[] = {
@@ -134,7 +134,7 @@ struct cudbg_meminfo {
 
 struct cudbg_cim_pif_la {
        int size;
-       u8 data[0];
+       u8 data[];
 };
 
 struct cudbg_clk_info {
@@ -339,13 +339,13 @@ struct cudbg_qdesc_entry {
        u32 qid;
        u32 desc_size;
        u32 num_desc;
-       u8 data[0]; /* Must be last */
+       u8 data[]; /* Must be last */
 };
 
 struct cudbg_qdesc_info {
        u32 qdesc_entry_size;
        u32 num_queues;
-       u8 data[0]; /* Must be last */
+       u8 data[]; /* Must be last */
 };
 
 #define IREG_NUM_ELEM 4
index de30d61..fe883cb 100644 (file)
@@ -984,7 +984,7 @@ static const char * const devlog_facility_strings[] = {
 struct devlog_info {
        unsigned int nentries;          /* number of entries in log[] */
        unsigned int first;             /* first [temporal] entry in log[] */
-       struct fw_devlog_e log[0];      /* Firmware Device Log */
+       struct fw_devlog_e log[];       /* Firmware Device Log */
 };
 
 /* Dump a Firmaware Device Log entry.
index ba95e13..1471cf0 100644 (file)
@@ -49,7 +49,7 @@ struct seq_tab {
        unsigned int rows;        /* # of entries */
        unsigned char width;      /* size in bytes of each entry */
        unsigned char skip_first; /* whether the first line is a header */
-       char data[0];             /* the table data */
+       char data[];             /* the table data */
 };
 
 static inline unsigned int hex2val(char c)
index a4b99ed..125868c 100644 (file)
@@ -289,6 +289,6 @@ struct cxgb4_link {
 
 struct cxgb4_tc_u32_table {
        unsigned int size;          /* number of entries in table */
-       struct cxgb4_link table[0]; /* Jump table */
+       struct cxgb4_link table[]; /* Jump table */
 };
 #endif /* __CXGB4_TC_U32_PARSE_H */
index 1a16449..12c3354 100644 (file)
@@ -59,7 +59,7 @@ struct l2t_data {
        rwlock_t lock;
        atomic_t nfree;             /* number of free entries */
        struct l2t_entry *rover;    /* starting point for next allocation */
-       struct l2t_entry l2tab[0];  /* MUST BE LAST */
+       struct l2t_entry l2tab[];  /* MUST BE LAST */
 };
 
 static inline unsigned int vlan_prio(const struct l2t_entry *e)
index 5cc74a5..5f8b871 100644 (file)
@@ -82,7 +82,7 @@ struct sched_class {
 
 struct sched_table {      /* per port scheduling table */
        u8 sched_size;
-       struct sched_class tab[0];
+       struct sched_class tab[];
 };
 
 static inline bool can_sched(struct net_device *dev)
index 1268d6e..541249d 100644 (file)
@@ -66,7 +66,7 @@ struct smt_entry {
 struct smt_data {
        unsigned int smt_size;
        rwlock_t lock;
-       struct smt_entry smtab[0];
+       struct smt_entry smtab[];
 };
 
 struct smt_data *t4_init_smt(void);
index 575c6ab..7d874f0 100644 (file)
@@ -1511,7 +1511,7 @@ struct ulptx_sgl {
        __be32 cmd_nsge;
        __be32 len0;
        __be64 addr0;
-       struct ulptx_sge_pair sge[0];
+       struct ulptx_sge_pair sge[];
 };
 
 struct ulptx_idata {
index accad11..703effc 100644 (file)
@@ -737,7 +737,7 @@ struct fw_flowc_mnemval {
 struct fw_flowc_wr {
        __be32 op_to_nparams;
        __be32 flowid_len16;
-       struct fw_flowc_mnemval mnemval[0];
+       struct fw_flowc_mnemval mnemval[];
 };
 
 #define FW_FLOWC_WR_NPARAMS_S           0
index 7b02c20..1b41564 100644 (file)
@@ -122,7 +122,7 @@ struct cxgbi_ppm_pool {
        unsigned int base;              /* base index */
        unsigned int next;              /* next possible free index */
        spinlock_t lock;                /* ppm pool lock */
-       unsigned long bmap[0];
+       unsigned long bmap[];
 } ____cacheline_aligned_in_smp;
 
 struct cxgbi_ppm {
@@ -145,7 +145,7 @@ struct cxgbi_ppm {
        unsigned int next;
        unsigned int max_index_in_edram;
        unsigned long *ppod_bmap;
-       struct cxgbi_ppod_data ppod_data[0];
+       struct cxgbi_ppod_data ppod_data[];
 };
 
 #define DDP_THRESHOLD          512