chelsio: Replace zero-length array with flexible-array member
Subsystems (4)
PATCH 1/1 chelsio: 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")
--- drivers/net/ethernet/chelsio/cxgb3/cxgb3_ioctl.h | 2 +- drivers/net/ethernet/chelsio/cxgb3/t3_cpl.h | 2 +- drivers/net/ethernet/chelsio/cxgb4/clip_tbl.h | 2 +- drivers/net/ethernet/chelsio/cxgb4/cudbg_entity.h | 8 ++++---- drivers/net/ethernet/chelsio/cxgb4/cxgb4_debugfs.c | 2 +- drivers/net/ethernet/chelsio/cxgb4/cxgb4_debugfs.h | 2 +- drivers/net/ethernet/chelsio/cxgb4/cxgb4_tc_u32_parse.h | 2 +- drivers/net/ethernet/chelsio/cxgb4/l2t.c | 2 +- drivers/net/ethernet/chelsio/cxgb4/sched.h | 2 +- drivers/net/ethernet/chelsio/cxgb4/smt.h | 2 +- drivers/net/ethernet/chelsio/cxgb4/t4_msg.h | 2 +- drivers/net/ethernet/chelsio/cxgb4/t4fw_api.h | 2 +- drivers/net/ethernet/chelsio/libcxgb/libcxgb_ppm.h | 4 ++-- 13 files changed, 17 insertions(+), 17 deletions(-)
drivers/net/ethernet/chelsio/cxgb3/cxgb3_ioctl.h +1 -1
@@ -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 {
drivers/net/ethernet/chelsio/cxgb3/t3_cpl.h +1 -1
@@ -1448,7 +1448,7 @@ struct cpl_rdma_terminate {#endif __be32 msn;__be32 mo; -__u8 data[0 ]; +__u8 data[]; };/* cpl_rdma_terminate.tid_len fields */
drivers/net/ethernet/chelsio/cxgb4/clip_tbl.h +1 -1
@@ -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 {
drivers/net/ethernet/chelsio/cxgb4/cudbg_entity.h +4 -4
@@ -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
drivers/net/ethernet/chelsio/cxgb4/cxgb4_debugfs.c +1 -1
@@ -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.
drivers/net/ethernet/chelsio/cxgb4/cxgb4_debugfs.h +1 -1
@@ -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)
drivers/net/ethernet/chelsio/cxgb4/cxgb4_tc_u32_parse.h +1 -1
@@ -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 */
drivers/net/ethernet/chelsio/cxgb4/l2t.c +1 -1
@@ -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)
drivers/net/ethernet/chelsio/cxgb4/sched.h +1 -1
@@ -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)
drivers/net/ethernet/chelsio/cxgb4/smt.h +1 -1
@@ -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 );
drivers/net/ethernet/chelsio/cxgb4/t4_msg.h +1 -1
@@ -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 {
drivers/net/ethernet/chelsio/cxgb4/t4fw_api.h +1 -1
@@ -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
drivers/net/ethernet/chelsio/libcxgb/libcxgb_ppm.h +2 -2
@@ -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
Signature · 1 line
2.25.0
From: "Gustavo A. R. Silva" <[email protected]>
Date: Mon, 24 Feb 2020 10:30:24 -0600
Quoted 22 lines
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]
Quoted 7 lines
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] commit76497732932f("cxgb3/l2t: Fix undefined behaviour")Signed-off-by Gustavo A. R. Silva <[email protected]>
Applied.
Quoted 9 lines · David Miller
Quoted 6 lines
[1] https://gcc.gnu.org/onlinedocs/gcc/Zero-Length.html
[2] https://github.com/KSPP/linux/issues/21
[3] commit76497732932f("cxgb3/l2t: Fix undefined behaviour")Signed-off-by Gustavo A. R. Silva <[email protected]>Applied.
Thanks, Dave.
--
Gustavo