Skip to content

Commit d7f10df

Browse files
GustavoARSilvaborkmann
authored andcommitted
bpf: 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] KSPP/linux#21 [3] commit 7649773 ("cxgb3/l2t: Fix undefined behaviour") Signed-off-by: Gustavo A. R. Silva <gustavo@embeddedor.com> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net> Acked-by: Song Liu <songliubraving@fb.com> Link: https://lore.kernel.org/bpf/20200227001744.GA3317@embeddedor
1 parent 4bc9884 commit d7f10df

File tree

6 files changed

+6
-6
lines changed

6 files changed

+6
-6
lines changed

include/linux/bpf-cgroup.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ struct bpf_cgroup_storage_map;
3636

3737
struct bpf_storage_buffer {
3838
struct rcu_head rcu;
39-
char data[0];
39+
char data[];
4040
};
4141

4242
struct bpf_cgroup_storage {

include/linux/bpf.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -859,7 +859,7 @@ struct bpf_prog_array_item {
859859

860860
struct bpf_prog_array {
861861
struct rcu_head rcu;
862-
struct bpf_prog_array_item items[0];
862+
struct bpf_prog_array_item items[];
863863
};
864864

865865
struct bpf_prog_array *bpf_prog_array_alloc(u32 prog_cnt, gfp_t flags);

include/uapi/linux/bpf.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ struct bpf_insn {
7373
/* Key of an a BPF_MAP_TYPE_LPM_TRIE entry */
7474
struct bpf_lpm_trie_key {
7575
__u32 prefixlen; /* up to 32 for AF_INET, 128 for AF_INET6 */
76-
__u8 data[0]; /* Arbitrary size */
76+
__u8 data[]; /* Arbitrary size */
7777
};
7878

7979
struct bpf_cgroup_storage_key {

kernel/bpf/bpf_struct_ops.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ enum bpf_struct_ops_state {
2323

2424
struct bpf_struct_ops_value {
2525
BPF_STRUCT_OPS_COMMON_VALUE;
26-
char data[0] ____cacheline_aligned_in_smp;
26+
char data[] ____cacheline_aligned_in_smp;
2727
};
2828

2929
struct bpf_struct_ops_map {

kernel/bpf/hashtab.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ struct htab_elem {
118118
struct bpf_lru_node lru_node;
119119
};
120120
u32 hash;
121-
char key[0] __aligned(8);
121+
char key[] __aligned(8);
122122
};
123123

124124
static inline bool htab_is_prealloc(const struct bpf_htab *htab)

kernel/bpf/lpm_trie.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ struct lpm_trie_node {
2525
struct lpm_trie_node __rcu *child[2];
2626
u32 prefixlen;
2727
u32 flags;
28-
u8 data[0];
28+
u8 data[];
2929
};
3030

3131
struct lpm_trie {

0 commit comments

Comments
 (0)