Skip to content

Commit

Permalink
Merge tag 'flex-array-conversions-5.8-rc2' of git://git.kernel.org/pu…
Browse files Browse the repository at this point in the history
…b/scm/linux/kernel/git/gustavoars/linux

Pull flexible-array member conversions from Gustavo A. R. Silva:
 "Replace zero-length arrays with flexible-array members.

  Notice that all of these patches have been baking in linux-next for
  two development cycles now.

  There is a regular need in the kernel to provide a way to declare
  having a dynamically sized set of trailing elements in a structure.
  Kernel code should always use “flexible array members”[1] for these
  cases. The older style of one-element or zero-length arrays should no
  longer be used[2].

  C99 introduced “flexible array members”, which lacks a numeric size
  for the array declaration entirely:

        struct something {
                size_t count;
                struct foo items[];
        };

  This is the way the kernel expects dynamically sized trailing elements
  to be declared. It allows the compiler to generate errors when the
  flexible array does not occur last in the structure, which helps to
  prevent some kind of undefined behavior[3] bugs from being
  inadvertently introduced to the codebase.

  It also allows the compiler to correctly analyze array sizes (via
  sizeof(), CONFIG_FORTIFY_SOURCE, and CONFIG_UBSAN_BOUNDS). For
  instance, there is no mechanism that warns us that the following
  application of the sizeof() operator to a zero-length array always
  results in zero:

        struct something {
                size_t count;
                struct foo items[0];
        };

        struct something *instance;

        instance = kmalloc(struct_size(instance, items, count), GFP_KERNEL);
        instance->count = count;

        size = sizeof(instance->items) * instance->count;
        memcpy(instance->items, source, size);

  At the last line of code above, size turns out to be zero, when one
  might have thought it represents the total size in bytes of the
  dynamic memory recently allocated for the trailing array items. Here
  are a couple examples of this issue[4][5].

  Instead, flexible array members have incomplete type, and so the
  sizeof() operator may not be applied[6], so any misuse of such
  operators will be immediately noticed at build time.

  The cleanest and least error-prone way to implement this is through
  the use of a flexible array member:

        struct something {
                size_t count;
                struct foo items[];
        };

        struct something *instance;

        instance = kmalloc(struct_size(instance, items, count), GFP_KERNEL);
        instance->count = count;

        size = sizeof(instance->items[0]) * instance->count;
        memcpy(instance->items, source, size);

  instead"

[1] https://en.wikipedia.org/wiki/Flexible_array_member
[2] https://github.com/KSPP/linux/issues/21
[3] commit 7649773 ("cxgb3/l2t: Fix undefined behaviour")
[4] commit f2cd32a ("rndis_wlan: Remove logically dead code")
[5] commit ab91c2a ("tpm: eventlog: Replace zero-length array with flexible-array member")
[6] https://gcc.gnu.org/onlinedocs/gcc/Zero-Length.html

* tag 'flex-array-conversions-5.8-rc2' of git://git.kernel.org/pub/scm/linux/kernel/git/gustavoars/linux: (41 commits)
  w1: Replace zero-length array with flexible-array
  tracing/probe: Replace zero-length array with flexible-array
  soc: ti: Replace zero-length array with flexible-array
  tifm: Replace zero-length array with flexible-array
  dmaengine: tegra-apb: Replace zero-length array with flexible-array
  stm class: Replace zero-length array with flexible-array
  Squashfs: Replace zero-length array with flexible-array
  ASoC: SOF: Replace zero-length array with flexible-array
  ima: Replace zero-length array with flexible-array
  sctp: Replace zero-length array with flexible-array
  phy: samsung: Replace zero-length array with flexible-array
  RxRPC: Replace zero-length array with flexible-array
  rapidio: Replace zero-length array with flexible-array
  media: pwc: Replace zero-length array with flexible-array
  firmware: pcdp: Replace zero-length array with flexible-array
  oprofile: Replace zero-length array with flexible-array
  block: Replace zero-length array with flexible-array
  tools/testing/nvdimm: Replace zero-length array with flexible-array
  libata: Replace zero-length array with flexible-array
  kprobes: Replace zero-length array with flexible-array
  ...
  • Loading branch information
Linus Torvalds committed Jun 17, 2020
2 parents ff58155 + 76fafbf commit ffbc937
Showing 54 changed files with 96 additions and 96 deletions.
2 changes: 1 addition & 1 deletion arch/ia64/kernel/unwind_i.h
Original file line number Diff line number Diff line change
@@ -42,7 +42,7 @@ enum unw_register_index {

struct unw_info_block {
u64 header;
u64 desc[0]; /* unwind descriptors */
u64 desc[]; /* unwind descriptors */
/* personality routine and language-specific data follow behind descriptors */
};

2 changes: 1 addition & 1 deletion block/partitions/ldm.h
Original file line number Diff line number Diff line change
@@ -93,7 +93,7 @@ struct frag { /* VBLK Fragment handling */
u8 num; /* Total number of records */
u8 rec; /* This is record number n */
u8 map; /* Which portions are in use */
u8 data[0];
u8 data[];
};

/* In memory LDM database structures. */
2 changes: 1 addition & 1 deletion drivers/amba/tegra-ahb.c
Original file line number Diff line number Diff line change
@@ -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)
2 changes: 1 addition & 1 deletion drivers/block/drbd/drbd_int.h
Original file line number Diff line number Diff line change
@@ -620,7 +620,7 @@ struct fifo_buffer {
unsigned int head_index;
unsigned int size;
int total; /* sum of all values */
int values[0];
int values[];
};
extern struct fifo_buffer *fifo_alloc(unsigned int fifo_size);

8 changes: 4 additions & 4 deletions drivers/block/drbd/drbd_protocol.h
Original file line number Diff line number Diff line change
@@ -271,7 +271,7 @@ struct p_rs_param {
u32 resync_rate;

/* Since protocol version 88 and higher. */
char verify_alg[0];
char verify_alg[];
} __packed;

struct p_rs_param_89 {
@@ -305,7 +305,7 @@ struct p_protocol {
u32 two_primaries;

/* Since protocol version 87 and higher. */
char integrity_alg[0];
char integrity_alg[];

} __packed;

@@ -360,7 +360,7 @@ struct p_sizes {
u16 dds_flags; /* use enum dds_flags here. */

/* optional queue_limits if (agreed_features & DRBD_FF_WSAME) */
struct o_qlim qlim[0];
struct o_qlim qlim[];
} __packed;

struct p_state {
@@ -409,7 +409,7 @@ struct p_compressed_bm {
*/
u8 encoding;

u8 code[0];
u8 code[];
} __packed;

struct p_delay_probe93 {
8 changes: 4 additions & 4 deletions drivers/crypto/chelsio/chcr_crypto.h
Original file line number Diff line number Diff line change
@@ -223,7 +223,7 @@ struct chcr_authenc_ctx {

struct __aead_ctx {
struct chcr_gcm_ctx gcm[0];
struct chcr_authenc_ctx authenc[0];
struct chcr_authenc_ctx authenc[];
};

struct chcr_aead_ctx {
@@ -235,7 +235,7 @@ struct chcr_aead_ctx {
u8 nonce[4];
u16 hmac_ctrl;
u16 mayverify;
struct __aead_ctx ctx[0];
struct __aead_ctx ctx[];
};

struct hmac_ctx {
@@ -247,7 +247,7 @@ struct hmac_ctx {
struct __crypto_ctx {
struct hmac_ctx hmacctx[0];
struct ablk_ctx ablkctx[0];
struct chcr_aead_ctx aeadctx[0];
struct chcr_aead_ctx aeadctx[];
};

struct chcr_context {
@@ -257,7 +257,7 @@ struct chcr_context {
unsigned int ntxq;
unsigned int nrxq;
struct completion cbc_aes_aio_done;
struct __crypto_ctx crypto_ctx[0];
struct __crypto_ctx crypto_ctx[];
};

struct chcr_hctx_per_wr {
2 changes: 1 addition & 1 deletion drivers/dma/milbeaut-hdmac.c
Original file line number Diff line number Diff line change
@@ -77,7 +77,7 @@ struct milbeaut_hdmac_device {
struct dma_device ddev;
struct clk *clk;
void __iomem *reg_base;
struct milbeaut_hdmac_chan channels[0];
struct milbeaut_hdmac_chan channels[];
};

static struct milbeaut_hdmac_chan *
2 changes: 1 addition & 1 deletion drivers/dma/milbeaut-xdmac.c
Original file line number Diff line number Diff line change
@@ -74,7 +74,7 @@ struct milbeaut_xdmac_chan {
struct milbeaut_xdmac_device {
struct dma_device ddev;
void __iomem *reg_base;
struct milbeaut_xdmac_chan channels[0];
struct milbeaut_xdmac_chan channels[];
};

static struct milbeaut_xdmac_chan *
2 changes: 1 addition & 1 deletion drivers/dma/moxart-dma.c
Original file line number Diff line number Diff line change
@@ -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 {
2 changes: 1 addition & 1 deletion drivers/dma/tegra20-apb-dma.c
Original file line number Diff line number Diff line change
@@ -225,7 +225,7 @@ struct tegra_dma {
u32 global_pause_count;

/* 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)
2 changes: 1 addition & 1 deletion drivers/dma/ti/edma.c
Original file line number Diff line number Diff line change
@@ -211,7 +211,7 @@ struct edma_desc {
u32 residue;
u32 residue_stat;

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

struct edma_cc;
2 changes: 1 addition & 1 deletion drivers/dma/ti/k3-udma.c
Original file line number Diff line number Diff line change
@@ -170,7 +170,7 @@ struct udma_desc {
void *metadata; /* pointer to provided metadata buffer (EPIP, PSdata) */

unsigned int hwdesc_count;
struct udma_hwdesc hwdesc[0];
struct udma_hwdesc hwdesc[];
};

enum udma_chan_state {
2 changes: 1 addition & 1 deletion drivers/dma/timb_dma.c
Original file line number Diff line number Diff line change
@@ -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)
2 changes: 1 addition & 1 deletion drivers/firewire/core-cdev.c
Original file line number Diff line number Diff line change
@@ -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 {
2 changes: 1 addition & 1 deletion drivers/firewire/core-transaction.c
Original file line number Diff line number Diff line change
@@ -620,7 +620,7 @@ struct fw_request {
u32 request_header[4];
int ack;
u32 length;
u32 data[0];
u32 data[];
};

static void free_response_callback(struct fw_packet *packet,
2 changes: 1 addition & 1 deletion drivers/firewire/core.h
Original file line number Diff line number Diff line change
@@ -191,7 +191,7 @@ struct fw_node {
/* Upper layer specific data. */
void *data;

struct fw_node *ports[0];
struct fw_node *ports[];
};

static inline struct fw_node *fw_node_get(struct fw_node *node)
2 changes: 1 addition & 1 deletion drivers/firewire/nosy.c
Original file line number Diff line number Diff line change
@@ -52,7 +52,7 @@ struct pcl {

struct packet {
unsigned int length;
char data[0];
char data[];
};

struct packet_buffer {
2 changes: 1 addition & 1 deletion drivers/firewire/ohci.c
Original file line number Diff line number Diff line change
@@ -111,7 +111,7 @@ struct descriptor_buffer {
dma_addr_t buffer_bus;
size_t buffer_size;
size_t used;
struct descriptor buffer[0];
struct descriptor buffer[];
};

struct context {
2 changes: 1 addition & 1 deletion drivers/firmware/dmi-sysfs.c
Original file line number Diff line number Diff line change
@@ -262,7 +262,7 @@ struct dmi_system_event_log {
u8 header_format;
u8 type_descriptors_supported_count;
u8 per_log_type_descriptor_length;
u8 supported_log_type_descriptos[0];
u8 supported_log_type_descriptos[];
} __packed;

#define DMI_SYSFS_SEL_FIELD(_field) \
2 changes: 1 addition & 1 deletion drivers/firmware/google/memconsole-coreboot.c
Original file line number Diff line number Diff line change
@@ -21,7 +21,7 @@
struct cbmem_cons {
u32 size_dont_access_after_boot;
u32 cursor;
u8 body[0];
u8 body[];
} __packed;

#define CURSOR_MASK ((1 << 28) - 1)
2 changes: 1 addition & 1 deletion drivers/firmware/google/vpd.c
Original file line number Diff line number Diff line change
@@ -32,7 +32,7 @@ struct vpd_cbmem {
u32 version;
u32 ro_size;
u32 rw_size;
u8 blob[0];
u8 blob[];
};

struct vpd_section {
2 changes: 1 addition & 1 deletion drivers/firmware/iscsi_ibft.c
Original file line number Diff line number Diff line change
@@ -104,7 +104,7 @@ struct ibft_control {
u16 tgt0_off;
u16 nic1_off;
u16 tgt1_off;
u16 expansion[0];
u16 expansion[];
} __attribute__((__packed__));

struct ibft_initiator {
2 changes: 1 addition & 1 deletion drivers/firmware/pcdp.h
Original file line number Diff line number Diff line change
@@ -103,6 +103,6 @@ struct pcdp {
u8 creator_id[4];
u32 creator_rev;
u32 num_uarts;
struct pcdp_uart uart[0]; /* actual size is num_uarts */
struct pcdp_uart uart[]; /* actual size is num_uarts */
/* remainder of table is pcdp_device structures */
} __attribute__((packed));
2 changes: 1 addition & 1 deletion drivers/hwtracing/stm/policy.c
Original file line number Diff line number Diff line change
@@ -34,7 +34,7 @@ struct stp_policy_node {
unsigned int first_channel;
unsigned int last_channel;
/* this is the one that's exposed to the attributes */
unsigned char priv[0];
unsigned char priv[];
};

void *stp_policy_node_priv(struct stp_policy_node *pn)
4 changes: 2 additions & 2 deletions drivers/hwtracing/stm/stm.h
Original file line number Diff line number Diff line change
@@ -23,7 +23,7 @@ void *stp_policy_node_priv(struct stp_policy_node *pn);

struct stp_master {
unsigned int nr_free;
unsigned long chan_map[0];
unsigned long chan_map[];
};

struct stm_device {
@@ -42,7 +42,7 @@ struct stm_device {
const struct config_item_type *pdrv_node_type;
/* master allocation */
spinlock_t mc_lock;
struct stp_master *masters[0];
struct stp_master *masters[];
};

#define to_stm_device(_d) \
2 changes: 1 addition & 1 deletion drivers/media/usb/pwc/pwc.h
Original file line number Diff line number Diff line change
@@ -193,7 +193,7 @@ struct pwc_raw_frame {
decompressor) */
__u8 cmd[4]; /* the four byte of the command (in case of
nala, only the first 3 bytes is filled) */
__u8 rawframe[0]; /* frame_size = H / 4 * vbandlength */
__u8 rawframe[]; /* frame_size = H / 4 * vbandlength */
} __packed;

/* intermediate buffers with raw data from the USB cam */
4 changes: 2 additions & 2 deletions drivers/net/can/peak_canfd/peak_pciefd_main.c
Original file line number Diff line number Diff line change
@@ -146,7 +146,7 @@ struct pciefd_rx_dma {
__le32 irq_status;
__le32 sys_time_low;
__le32 sys_time_high;
struct pucan_rx_msg msg[0];
struct pucan_rx_msg msg[];
} __packed __aligned(4);

/* Tx Link record */
@@ -194,7 +194,7 @@ struct pciefd_board {
struct pci_dev *pci_dev;
int can_count;
spinlock_t cmd_lock; /* 64-bits cmds must be atomic */
struct pciefd_can *can[0]; /* array of network devices */
struct pciefd_can *can[]; /* array of network devices */
};

/* supported device ids. */
2 changes: 1 addition & 1 deletion drivers/oprofile/cpu_buffer.h
Original file line number Diff line number Diff line change
@@ -33,7 +33,7 @@ void flush_cpu_work(void);
struct op_sample {
unsigned long eip;
unsigned long event;
unsigned long data[0];
unsigned long data[];
};

struct op_entry;
2 changes: 1 addition & 1 deletion drivers/phy/samsung/phy-samsung-usb2.h
Original file line number Diff line number Diff line change
@@ -43,7 +43,7 @@ struct samsung_usb2_phy_driver {
struct regmap *reg_pmu;
struct regmap *reg_sys;
spinlock_t lock;
struct samsung_usb2_phy_instance instances[0];
struct samsung_usb2_phy_instance instances[];
};

struct samsung_usb2_common_phy {
2 changes: 1 addition & 1 deletion drivers/rapidio/rio-scan.c
Original file line number Diff line number Diff line change
@@ -39,7 +39,7 @@ struct rio_id_table {
u16 start; /* logical minimal id */
u32 max; /* max number of IDs in table */
spinlock_t lock;
unsigned long table[0];
unsigned long table[];
};

static int next_destid = 0;
2 changes: 1 addition & 1 deletion drivers/soc/ti/knav_qmss.h
Original file line number Diff line number Diff line change
@@ -67,7 +67,7 @@ struct knav_reg_config {
u32 link_ram_size0;
u32 link_ram_base1;
u32 __pad2[2];
u32 starvation[0];
u32 starvation[];
};

struct knav_reg_region {
4 changes: 2 additions & 2 deletions drivers/w1/w1_netlink.h
Original file line number Diff line number Diff line change
@@ -73,7 +73,7 @@ struct w1_netlink_msg
__u32 res;
} mst;
} id;
__u8 data[0];
__u8 data[];
};

/**
@@ -122,7 +122,7 @@ struct w1_netlink_cmd
__u8 cmd;
__u8 res;
__u16 len;
__u8 data[0];
__u8 data[];
};

#ifdef __KERNEL__
2 changes: 1 addition & 1 deletion fs/aio.c
Original file line number Diff line number Diff line change
@@ -67,7 +67,7 @@ struct aio_ring {
unsigned header_length; /* size of aio_ring */


struct io_event io_events[0];
struct io_event io_events[];
}; /* 128 bytes + ring size */

/*
2 changes: 1 addition & 1 deletion fs/jffs2/nodelist.h
Original file line number Diff line number Diff line change
@@ -259,7 +259,7 @@ struct jffs2_full_dirent
uint32_t ino; /* == zero for unlink */
unsigned int nhash;
unsigned char type;
unsigned char name[0];
unsigned char name[];
};

/*
Loading

0 comments on commit ffbc937

Please sign in to comment.