Skip to content

Commit

Permalink
treewide: uapi: Replace zero-length arrays with flexible-array members
Browse files Browse the repository at this point in the history
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].

This code was transformed with the help of Coccinelle:
(linux-5.19-rc2$ spatch --jobs $(getconf _NPROCESSORS_ONLN) --sp-file script.cocci --include-headers --dir . > output.patch)

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

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

-fstrict-flex-arrays=3 is coming and we need to land these changes
to prevent issues like these in the short future:

../fs/minix/dir.c:337:3: warning: 'strcpy' will always overflow; destination buffer has size 0,
but the source string has length 2 (including NUL byte) [-Wfortify-source]
		strcpy(de3->name, ".");
		^

Since these are all [0] to [] changes, the risk to UAPI is nearly zero. If
this breaks anything, we can use a union with a new member name.

[1] https://en.wikipedia.org/wiki/Flexible_array_member
[2] https://www.kernel.org/doc/html/v5.16/process/deprecated.html#zero-length-and-one-element-arrays

Link: KSPP#78
Build-tested-by: kernel test robot <lkp@intel.com>
Link: https://lore.kernel.org/lkml/62b675ec.wKX6AOZ6cbE71vtF%25lkp@intel.com/
Acked-by: Dan Williams <dan.j.williams@intel.com> # For ndctl.h
Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org>
  • Loading branch information
GustavoARSilva committed Jun 28, 2022
1 parent b13bacc commit 94dfc73
Show file tree
Hide file tree
Showing 82 changed files with 216 additions and 216 deletions.
4 changes: 2 additions & 2 deletions arch/m68k/include/uapi/asm/bootinfo.h
Expand Up @@ -34,7 +34,7 @@
struct bi_record {
__be16 tag; /* tag ID */
__be16 size; /* size of record (in bytes) */
__be32 data[0]; /* data */
__be32 data[]; /* data */
};


Expand Down Expand Up @@ -168,7 +168,7 @@ struct bootversion {
struct {
__be32 machtype;
__be32 version;
} machversions[0];
} machversions[];
} __packed;

#endif /* __ASSEMBLY__ */
Expand Down
2 changes: 1 addition & 1 deletion arch/mips/include/uapi/asm/ucontext.h
Expand Up @@ -60,7 +60,7 @@ struct ucontext {
sigset_t uc_sigmask;

/* Extended context structures may follow ucontext */
unsigned long long uc_extcontext[0];
unsigned long long uc_extcontext[];
};

#endif /* __MIPS_UAPI_ASM_UCONTEXT_H */
6 changes: 3 additions & 3 deletions arch/s390/include/uapi/asm/hwctrset.h
Expand Up @@ -30,18 +30,18 @@ struct s390_ctrset_start { /* Set CPUs to operate on */
struct s390_ctrset_setdata { /* Counter set data */
__u32 set; /* Counter set number */
__u32 no_cnts; /* # of counters stored in cv[] */
__u64 cv[0]; /* Counter values (variable length) */
__u64 cv[]; /* Counter values (variable length) */
};

struct s390_ctrset_cpudata { /* Counter set data per CPU */
__u32 cpu_nr; /* CPU number */
__u32 no_sets; /* # of counters sets in data[] */
struct s390_ctrset_setdata data[0];
struct s390_ctrset_setdata data[];
};

struct s390_ctrset_read { /* Structure to get all ctr sets */
__u64 no_cpus; /* Total # of CPUs data taken from */
struct s390_ctrset_cpudata data[0];
struct s390_ctrset_cpudata data[];
};

#define S390_HWCTR_MAGIC 'C' /* Random magic # for ioctls */
Expand Down
2 changes: 1 addition & 1 deletion arch/x86/include/uapi/asm/bootparam.h
Expand Up @@ -52,7 +52,7 @@ struct setup_data {
__u64 next;
__u32 type;
__u32 len;
__u8 data[0];
__u8 data[];
};

/* extensible setup indirect data node */
Expand Down
12 changes: 6 additions & 6 deletions arch/x86/include/uapi/asm/kvm.h
Expand Up @@ -198,13 +198,13 @@ struct kvm_msrs {
__u32 nmsrs; /* number of msrs in entries */
__u32 pad;

struct kvm_msr_entry entries[0];
struct kvm_msr_entry entries[];
};

/* for KVM_GET_MSR_INDEX_LIST */
struct kvm_msr_list {
__u32 nmsrs; /* number of msrs in entries */
__u32 indices[0];
__u32 indices[];
};

/* Maximum size of any access bitmap in bytes */
Expand Down Expand Up @@ -241,7 +241,7 @@ struct kvm_cpuid_entry {
struct kvm_cpuid {
__u32 nent;
__u32 padding;
struct kvm_cpuid_entry entries[0];
struct kvm_cpuid_entry entries[];
};

struct kvm_cpuid_entry2 {
Expand All @@ -263,7 +263,7 @@ struct kvm_cpuid_entry2 {
struct kvm_cpuid2 {
__u32 nent;
__u32 padding;
struct kvm_cpuid_entry2 entries[0];
struct kvm_cpuid_entry2 entries[];
};

/* for KVM_GET_PIT and KVM_SET_PIT */
Expand Down Expand Up @@ -389,7 +389,7 @@ struct kvm_xsave {
* the contents of CPUID leaf 0xD on the host.
*/
__u32 region[1024];
__u32 extra[0];
__u32 extra[];
};

#define KVM_MAX_XCRS 16
Expand Down Expand Up @@ -516,7 +516,7 @@ struct kvm_pmu_event_filter {
__u32 fixed_counter_bitmap;
__u32 flags;
__u32 pad[4];
__u64 events[0];
__u64 events[];
};

#define KVM_PMU_EVENT_ALLOW 0
Expand Down
6 changes: 3 additions & 3 deletions include/uapi/drm/i915_drm.h
Expand Up @@ -2123,7 +2123,7 @@ struct i915_context_engines_load_balance {

__u64 mbz64; /* reserved for future use; must be zero */

struct i915_engine_class_instance engines[0];
struct i915_engine_class_instance engines[];
} __attribute__((packed));

#define I915_DEFINE_CONTEXT_ENGINES_LOAD_BALANCE(name__, N__) struct { \
Expand Down Expand Up @@ -2161,7 +2161,7 @@ struct i915_context_engines_bond {
__u64 flags; /* all undefined flags must be zero */
__u64 mbz64[4]; /* reserved for future use; must be zero */

struct i915_engine_class_instance engines[0];
struct i915_engine_class_instance engines[];
} __attribute__((packed));

#define I915_DEFINE_CONTEXT_ENGINES_BOND(name__, N__) struct { \
Expand Down Expand Up @@ -2288,7 +2288,7 @@ struct i915_context_engines_parallel_submit {
* length = width (i) * num_siblings (j)
* index = j + i * num_siblings
*/
struct i915_engine_class_instance engines[0];
struct i915_engine_class_instance engines[];

} __packed;

Expand Down
2 changes: 1 addition & 1 deletion include/uapi/linux/blkzoned.h
Expand Up @@ -130,7 +130,7 @@ struct blk_zone_report {
__u64 sector;
__u32 nr_zones;
__u32 flags;
struct blk_zone zones[0];
struct blk_zone zones[];
};

/**
Expand Down
2 changes: 1 addition & 1 deletion include/uapi/linux/bpf.h
Expand Up @@ -79,7 +79,7 @@ struct bpf_insn {
/* Key of an a BPF_MAP_TYPE_LPM_TRIE entry */
struct bpf_lpm_trie_key {
__u32 prefixlen; /* up to 32 for AF_INET, 128 for AF_INET6 */
__u8 data[0]; /* Arbitrary size */
__u8 data[]; /* Arbitrary size */
};

struct bpf_cgroup_storage_key {
Expand Down
10 changes: 5 additions & 5 deletions include/uapi/linux/btrfs.h
Expand Up @@ -93,7 +93,7 @@ struct btrfs_qgroup_inherit {
__u64 num_ref_copies;
__u64 num_excl_copies;
struct btrfs_qgroup_limit lim;
__u64 qgroups[0];
__u64 qgroups[];
};

struct btrfs_ioctl_qgroup_limit_args {
Expand Down Expand Up @@ -561,7 +561,7 @@ struct btrfs_ioctl_search_args_v2 {
__u64 buf_size; /* in - size of buffer
* out - on EOVERFLOW: needed size
* to store item */
__u64 buf[0]; /* out - found items */
__u64 buf[]; /* out - found items */
};

struct btrfs_ioctl_clone_range_args {
Expand Down Expand Up @@ -632,7 +632,7 @@ struct btrfs_ioctl_same_args {
__u16 dest_count; /* in - total elements in info array */
__u16 reserved1;
__u32 reserved2;
struct btrfs_ioctl_same_extent_info info[0];
struct btrfs_ioctl_same_extent_info info[];
};

struct btrfs_ioctl_space_info {
Expand All @@ -644,15 +644,15 @@ struct btrfs_ioctl_space_info {
struct btrfs_ioctl_space_args {
__u64 space_slots;
__u64 total_spaces;
struct btrfs_ioctl_space_info spaces[0];
struct btrfs_ioctl_space_info spaces[];
};

struct btrfs_data_container {
__u32 bytes_left; /* out -- bytes not needed to deliver output */
__u32 bytes_missing; /* out -- additional bytes needed for result */
__u32 elem_cnt; /* out */
__u32 elem_missed; /* out */
__u64 val[0]; /* out */
__u64 val[]; /* out */
};

struct btrfs_ioctl_ino_path_args {
Expand Down
2 changes: 1 addition & 1 deletion include/uapi/linux/btrfs_tree.h
Expand Up @@ -575,7 +575,7 @@ struct btrfs_inode_extref {
__le64 parent_objectid;
__le64 index;
__le16 name_len;
__u8 name[0];
__u8 name[];
/* name goes here */
} __attribute__ ((__packed__));

Expand Down
2 changes: 1 addition & 1 deletion include/uapi/linux/can/bcm.h
Expand Up @@ -71,7 +71,7 @@ struct bcm_msg_head {
struct bcm_timeval ival1, ival2;
canid_t can_id;
__u32 nframes;
struct can_frame frames[0];
struct can_frame frames[];
};

enum {
Expand Down
2 changes: 1 addition & 1 deletion include/uapi/linux/connector.h
Expand Up @@ -75,7 +75,7 @@ struct cn_msg {

__u16 len; /* Length of the following data */
__u16 flags;
__u8 data[0];
__u8 data[];
};

#endif /* _UAPI__CONNECTOR_H */
2 changes: 1 addition & 1 deletion include/uapi/linux/cycx_cfm.h
Expand Up @@ -91,7 +91,7 @@ struct cycx_firmware {
unsigned short reserved[6];
char descr[CFM_DESCR_LEN];
struct cycx_fw_info info;
unsigned char image[0];
unsigned char image[];
};

struct cycx_fw_header {
Expand Down
8 changes: 4 additions & 4 deletions include/uapi/linux/dm-ioctl.h
Expand Up @@ -182,7 +182,7 @@ struct dm_target_spec {
struct dm_target_deps {
__u32 count; /* Array size */
__u32 padding; /* unused */
__u64 dev[0]; /* out */
__u64 dev[]; /* out */
};

/*
Expand All @@ -192,7 +192,7 @@ struct dm_name_list {
__u64 dev;
__u32 next; /* offset to the next record from
the _start_ of this */
char name[0];
char name[];

/*
* The following members can be accessed by taking a pointer that
Expand All @@ -216,7 +216,7 @@ struct dm_target_versions {
__u32 next;
__u32 version[3];

char name[0];
char name[];
};

/*
Expand All @@ -225,7 +225,7 @@ struct dm_target_versions {
struct dm_target_msg {
__u64 sector; /* Device sector */

char message[0];
char message[];
};

/*
Expand Down
2 changes: 1 addition & 1 deletion include/uapi/linux/dm-log-userspace.h
Expand Up @@ -426,7 +426,7 @@ struct dm_ulog_request {
__u32 request_type; /* DM_ULOG_* defined above */
__u32 data_size; /* How much data (not including this struct) */

char data[0];
char data[];
};

#endif /* __DM_LOG_USERSPACE_H__ */

0 comments on commit 94dfc73

Please sign in to comment.