Skip to content

Commit

Permalink
treewide: Replace zero-length arrays 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
unadvertenly introduced[3] to the codebase from now on.

All these instances of code were found with the help of the following
Coccinelle script:

@@
identifier S, member, array;
type T1, T2;
@@

struct S {
  ...
  T1 member;
  T2 array[
- 0
  ];
};

[1] https://gcc.gnu.org/onlinedocs/gcc/Zero-Length.html
[2] KSPP#21
[3] commit 7649773 ("cxgb3/l2t: Fix undefined behaviour")

NOTE: I'll carry this in my -next tree for the v5.6 merge window.

Signed-off-by: Gustavo A. R. Silva <[email protected]>
  • Loading branch information
GustavoARSilva authored and intel-lab-lkp committed Feb 14, 2020
1 parent aa7619a commit e636fd7
Show file tree
Hide file tree
Showing 247 changed files with 297 additions and 297 deletions.
2 changes: 1 addition & 1 deletion arch/m68k/amiga/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -628,7 +628,7 @@ struct savekmsg {
unsigned long magic2; /* SAVEKMSG_MAGIC2 */
unsigned long magicptr; /* address of magic1 */
unsigned long size;
char data[0];
char data[];
};

static struct savekmsg *savekmsg;
Expand Down
2 changes: 1 addition & 1 deletion arch/m68k/tools/amiga/dmesg.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ struct savekmsg {
u_long magic2; /* SAVEKMSG_MAGIC2 */
u_long magicptr; /* address of magic1 */
u_long size;
char data[0];
char data[];
};


Expand Down
2 changes: 1 addition & 1 deletion arch/mips/kernel/signal.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ struct sigframe {
/* Matches struct ucontext from its uc_mcontext field onwards */
struct sigcontext sf_sc;
sigset_t sf_mask;
unsigned long long sf_extcontext[0];
unsigned long long sf_extcontext[];
};

struct rt_sigframe {
Expand Down
2 changes: 1 addition & 1 deletion arch/powerpc/mm/hugetlbpage.c
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ int __init alloc_bootmem_huge_page(struct hstate *h)
struct hugepd_freelist {
struct rcu_head rcu;
unsigned int index;
void *ptes[0];
void *ptes[];
};

static DEFINE_PER_CPU(struct hugepd_freelist *, hugepd_freelist_cur);
Expand Down
2 changes: 1 addition & 1 deletion arch/powerpc/platforms/powermac/nvram.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ struct chrp_header {
u8 cksum;
u16 len;
char name[12];
u8 data[0];
u8 data[];
};

struct core99_header {
Expand Down
2 changes: 1 addition & 1 deletion arch/s390/appldata/appldata_os.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ struct appldata_os_data {
(waiting for I/O) */

/* per cpu data */
struct appldata_os_per_cpu os_cpu[0];
struct appldata_os_per_cpu os_cpu[];
} __attribute__((packed));

static struct appldata_os_data *appldata_os_data;
Expand Down
2 changes: 1 addition & 1 deletion arch/sparc/kernel/cpumap.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ struct cpuinfo_tree {

/* Offsets into nodes[] for each level of the tree */
struct cpuinfo_level level[CPUINFO_LVL_MAX];
struct cpuinfo_node nodes[0];
struct cpuinfo_node nodes[];
};


Expand Down
8 changes: 4 additions & 4 deletions arch/sparc/kernel/ds.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ struct ds_reg_req {
__u64 handle;
__u16 major;
__u16 minor;
char svc_id[0];
char svc_id[];
};

struct ds_reg_ack {
Expand Down Expand Up @@ -701,12 +701,12 @@ struct ds_var_hdr {

struct ds_var_set_msg {
struct ds_var_hdr hdr;
char name_and_value[0];
char name_and_value[];
};

struct ds_var_delete_msg {
struct ds_var_hdr hdr;
char name[0];
char name[];
};

struct ds_var_resp {
Expand Down Expand Up @@ -989,7 +989,7 @@ struct ds_queue_entry {
struct ds_info *dp;
int req_len;
int __pad;
u64 req[0];
u64 req[];
};

static void process_ds_work(void)
Expand Down
2 changes: 1 addition & 1 deletion arch/x86/events/intel/bts.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ struct bts_buffer {
local_t head;
unsigned long end;
void **data_pages;
struct bts_phys buf[0];
struct bts_phys buf[];
};

static struct pmu bts_pmu;
Expand Down
2 changes: 1 addition & 1 deletion drivers/amba/tegra-ahb.c
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ static const u32 tegra_ahb_gizmo[] = {
struct tegra_ahb {
void __iomem *regs;
struct device *dev;
u32 ctx[0];
u32 ctx[];
};

static inline u32 gizmo_readl(struct tegra_ahb *ahb, u32 offset)
Expand Down
2 changes: 1 addition & 1 deletion drivers/auxdisplay/charlcd.c
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ struct charlcd_priv {
int len;
} esc_seq;

unsigned long long drvdata[0];
unsigned long long drvdata[];
};

#define charlcd_to_priv(p) container_of(p, struct charlcd_priv, lcd)
Expand Down
2 changes: 1 addition & 1 deletion drivers/block/rsxx/dma.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ struct dma_tracker {
struct dma_tracker_list {
spinlock_t lock;
int head;
struct dma_tracker list[0];
struct dma_tracker list[];
};


Expand Down
4 changes: 2 additions & 2 deletions drivers/bluetooth/btintel.c
Original file line number Diff line number Diff line change
Expand Up @@ -376,13 +376,13 @@ struct ibt_cp_reg_access {
__le32 addr;
__u8 mode;
__u8 len;
__u8 data[0];
__u8 data[];
} __packed;

struct ibt_rp_reg_access {
__u8 status;
__le32 addr;
__u8 data[0];
__u8 data[];
} __packed;

static int regmap_ibt_read(void *context, const void *addr, size_t reg_size,
Expand Down
2 changes: 1 addition & 1 deletion drivers/bluetooth/hci_ag6xx.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ struct ag6xx_data {
struct pbn_entry {
__le32 addr;
__le32 plen;
__u8 data[0];
__u8 data[];
} __packed;

static int ag6xx_open(struct hci_uart *hu)
Expand Down
2 changes: 1 addition & 1 deletion drivers/bluetooth/hci_intel.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
struct hci_lpm_pkt {
__u8 opcode;
__u8 dlen;
__u8 data[0];
__u8 data[];
} __packed;

struct intel_device {
Expand Down
2 changes: 1 addition & 1 deletion drivers/char/mspec.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ struct vma_data {
enum mspec_page_type type; /* Type of pages allocated. */
unsigned long vm_start; /* Original (unsplit) base. */
unsigned long vm_end; /* Original (unsplit) end. */
unsigned long maddr[0]; /* Array of MSPEC addresses. */
unsigned long maddr[]; /* Array of MSPEC addresses. */
};

/*
Expand Down
2 changes: 1 addition & 1 deletion drivers/char/virtio_console.c
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ struct port_buffer {
unsigned int sgpages;

/* sg is used if spages > 0. sg must be the last in is struct */
struct scatterlist sg[0];
struct scatterlist sg[];
};

/*
Expand Down
2 changes: 1 addition & 1 deletion drivers/crypto/caam/caamalg.c
Original file line number Diff line number Diff line change
Expand Up @@ -895,7 +895,7 @@ struct skcipher_edesc {
int sec4_sg_bytes;
dma_addr_t sec4_sg_dma;
struct sec4_sg_entry *sec4_sg;
u32 hw_desc[0];
u32 hw_desc[];
};

static void caam_unmap(struct device *dev, struct scatterlist *src,
Expand Down
4 changes: 2 additions & 2 deletions drivers/crypto/caam/caamalg_qi.c
Original file line number Diff line number Diff line change
Expand Up @@ -783,7 +783,7 @@ struct aead_edesc {
unsigned int assoclen;
dma_addr_t assoclen_dma;
struct caam_drv_req drv_req;
struct qm_sg_entry sgt[0];
struct qm_sg_entry sgt[];
};

/*
Expand All @@ -803,7 +803,7 @@ struct skcipher_edesc {
int qm_sg_bytes;
dma_addr_t qm_sg_dma;
struct caam_drv_req drv_req;
struct qm_sg_entry sgt[0];
struct qm_sg_entry sgt[];
};

static struct caam_drv_ctx *get_drv_ctx(struct caam_ctx *ctx,
Expand Down
2 changes: 1 addition & 1 deletion drivers/crypto/caam/caamhash.c
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,7 @@ struct ahash_edesc {
int src_nents;
int sec4_sg_bytes;
u32 hw_desc[DESC_JOB_IO_LEN_MAX / sizeof(u32)] ____cacheline_aligned;
struct sec4_sg_entry sec4_sg[0];
struct sec4_sg_entry sec4_sg[];
};

static inline void ahash_unmap(struct device *dev,
Expand Down
2 changes: 1 addition & 1 deletion drivers/crypto/cavium/nitrox/nitrox_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ struct ucode {
char version[VERSION_LEN - 1];
__be32 code_size;
u8 raz[12];
u64 code[0];
u64 code[];
};

/**
Expand Down
2 changes: 1 addition & 1 deletion drivers/crypto/img-hash.c
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ struct img_hash_request_ctx {
struct ahash_request fallback_req;

/* Zero length buffer must remain last member of struct */
u8 buffer[0] __aligned(sizeof(u32));
u8 buffer[] __aligned(sizeof(u32));
};

struct img_hash_ctx {
Expand Down
2 changes: 1 addition & 1 deletion drivers/crypto/mediatek/mtk-sha.c
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ struct mtk_sha_ctx {
u8 id;
u8 buf[SHA_BUF_SIZE] __aligned(sizeof(u32));

struct mtk_sha_hmac_ctx base[0];
struct mtk_sha_hmac_ctx base[];
};

struct mtk_sha_drv {
Expand Down
4 changes: 2 additions & 2 deletions drivers/crypto/omap-sham.c
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ struct omap_sham_reqctx {
int sg_len;
unsigned int total; /* total request */

u8 buffer[0] OMAP_ALIGNED;
u8 buffer[] OMAP_ALIGNED;
};

struct omap_sham_hmac_ctx {
Expand All @@ -176,7 +176,7 @@ struct omap_sham_ctx {
/* fallback stuff */
struct crypto_shash *fallback;

struct omap_sham_hmac_ctx base[0];
struct omap_sham_hmac_ctx base[];
};

#define OMAP_SHAM_QUEUE_LENGTH 10
Expand Down
2 changes: 1 addition & 1 deletion drivers/crypto/s5p-sss.c
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ struct s5p_hash_reqctx {
bool error;

u32 bufcnt;
u8 buffer[0];
u8 buffer[];
};

/**
Expand Down
2 changes: 1 addition & 1 deletion drivers/dma/at_xdmac.c
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ struct at_xdmac {
struct clk *clk;
u32 save_gim;
struct dma_pool *at_xdmac_desc_pool;
struct at_xdmac_chan chan[0];
struct at_xdmac_chan chan[];
};


Expand Down
2 changes: 1 addition & 1 deletion drivers/dma/bcm-sba-raid.c
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ struct sba_request {
struct brcm_message msg;
struct dma_async_tx_descriptor tx;
/* SBA commands */
struct brcm_sba_command cmds[0];
struct brcm_sba_command cmds[];
};

enum sba_version {
Expand Down
2 changes: 1 addition & 1 deletion drivers/dma/ioat/dca.c
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ struct ioat_dca_priv {
int max_requesters;
int requester_count;
u8 tag_map[IOAT_TAG_MAP_LEN];
struct ioat_dca_slot req_slots[0];
struct ioat_dca_slot req_slots[];
};

static int ioat_dca_dev_managed(struct dca_provider *dca,
Expand Down
2 changes: 1 addition & 1 deletion drivers/dma/moxart-dma.c
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ struct moxart_desc {
unsigned int dma_cycles;
struct virt_dma_desc vd;
uint8_t es;
struct moxart_sg sg[0];
struct moxart_sg sg[];
};

struct moxart_chan {
Expand Down
2 changes: 1 addition & 1 deletion drivers/dma/qcom/bam_dma.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ struct bam_async_desc {
struct list_head desc_node;
enum dma_transfer_direction dir;
size_t length;
struct bam_desc_hw desc[0];
struct bam_desc_hw desc[];
};

enum bam_reg {
Expand Down
2 changes: 1 addition & 1 deletion drivers/dma/sa11x0-dma.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ struct sa11x0_dma_desc {
bool cyclic;

unsigned sglen;
struct sa11x0_dma_sg sg[0];
struct sa11x0_dma_sg sg[];
};

struct sa11x0_dma_phy;
Expand Down
2 changes: 1 addition & 1 deletion drivers/dma/sprd-dma.c
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ struct sprd_dma_dev {
struct clk *ashb_clk;
int irq;
u32 total_chns;
struct sprd_dma_chn channels[0];
struct sprd_dma_chn channels[];
};

static void sprd_dma_free_desc(struct virt_dma_desc *vd);
Expand Down
2 changes: 1 addition & 1 deletion drivers/dma/tegra20-apb-dma.c
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ struct tegra_dma {
u32 reg_gen;

/* Last member of the structure */
struct tegra_dma_channel channels[0];
struct tegra_dma_channel channels[];
};

static inline void tdma_write(struct tegra_dma *tdma, u32 reg, u32 val)
Expand Down
2 changes: 1 addition & 1 deletion drivers/dma/tegra210-adma.c
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ struct tegra_adma {
const struct tegra_adma_chip_data *cdata;

/* Last member of the structure */
struct tegra_adma_chan channels[0];
struct tegra_adma_chan channels[];
};

static inline void tdma_write(struct tegra_adma *tdma, u32 reg, u32 val)
Expand Down
2 changes: 1 addition & 1 deletion drivers/dma/ti/edma.c
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ struct edma_desc {
u32 residue;
u32 residue_stat;

struct edma_pset pset[0];
struct edma_pset pset[];
};

struct edma_cc;
Expand Down
2 changes: 1 addition & 1 deletion drivers/dma/ti/omap-dma.c
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ struct omap_desc {
uint32_t csdp; /* CSDP value */

unsigned sglen;
struct omap_sg sg[0];
struct omap_sg sg[];
};

enum {
Expand Down
2 changes: 1 addition & 1 deletion drivers/dma/timb_dma.c
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ struct timb_dma {
struct dma_device dma;
void __iomem *membase;
struct tasklet_struct tasklet;
struct timb_dma_chan channels[0];
struct timb_dma_chan channels[];
};

static struct device *chan2dev(struct dma_chan *chan)
Expand Down
2 changes: 1 addition & 1 deletion drivers/dma/uniphier-mdmac.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ struct uniphier_mdmac_device {
struct dma_device ddev;
struct clk *clk;
void __iomem *reg_base;
struct uniphier_mdmac_chan channels[0];
struct uniphier_mdmac_chan channels[];
};

static struct uniphier_mdmac_chan *
Expand Down
2 changes: 1 addition & 1 deletion drivers/firewire/core-cdev.c
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ struct inbound_transaction_resource {
struct descriptor_resource {
struct client_resource resource;
struct fw_descriptor descriptor;
u32 data[0];
u32 data[];
};

struct iso_resource {
Expand Down
Loading

0 comments on commit e636fd7

Please sign in to comment.