Skip to content

Commit 3467178

Browse files
Guo ZhengkuiNobody
authored andcommitted
selftests/bpf: fix array_size.cocci warning
Fix the array_size.cocci warning in tools/testing/selftests/bpf/ Use `ARRAY_SIZE(arr)` instead of forms like `sizeof(arr)/sizeof(arr[0])`. syscall.c and test_rdonly_maps.c don't contain header files which implement ARRAY_SIZE() macro. So I add `#include <linux/kernel.h>`, in which ARRAY_SIZE(arr) not only calculates the size of `arr`, but also checks that `arr` is really an array (using __must_be_array(arr)). Signed-off-by: Guo Zhengkui <guozhengkui@vivo.com>
1 parent 7525cc3 commit 3467178

File tree

11 files changed

+19
-17
lines changed

11 files changed

+19
-17
lines changed

tools/testing/selftests/bpf/prog_tests/cgroup_attach_autodetach.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ static int prog_load(void)
1414
BPF_MOV64_IMM(BPF_REG_0, 1), /* r0 = 1 */
1515
BPF_EXIT_INSN(),
1616
};
17-
size_t insns_cnt = sizeof(prog) / sizeof(struct bpf_insn);
17+
size_t insns_cnt = ARRAY_SIZE(prog);
1818

1919
return bpf_test_load_program(BPF_PROG_TYPE_CGROUP_SKB,
2020
prog, insns_cnt, "GPL", 0,

tools/testing/selftests/bpf/prog_tests/cgroup_attach_multi.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ static int prog_load_cnt(int verdict, int val)
6363
BPF_MOV64_IMM(BPF_REG_0, verdict), /* r0 = verdict */
6464
BPF_EXIT_INSN(),
6565
};
66-
size_t insns_cnt = sizeof(prog) / sizeof(struct bpf_insn);
66+
size_t insns_cnt = ARRAY_SIZE(prog);
6767
int ret;
6868

6969
ret = bpf_test_load_program(BPF_PROG_TYPE_CGROUP_SKB,

tools/testing/selftests/bpf/prog_tests/cgroup_attach_override.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ static int prog_load(int verdict)
1616
BPF_MOV64_IMM(BPF_REG_0, verdict), /* r0 = verdict */
1717
BPF_EXIT_INSN(),
1818
};
19-
size_t insns_cnt = sizeof(prog) / sizeof(struct bpf_insn);
19+
size_t insns_cnt = ARRAY_SIZE(prog);
2020

2121
return bpf_test_load_program(BPF_PROG_TYPE_CGROUP_SKB,
2222
prog, insns_cnt, "GPL", 0,

tools/testing/selftests/bpf/prog_tests/global_data.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ static void test_global_data_number(struct bpf_object *obj, __u32 duration)
2929
{ "relocate .rodata reference", 10, ~0 },
3030
};
3131

32-
for (i = 0; i < sizeof(tests) / sizeof(tests[0]); i++) {
32+
for (i = 0; i < ARRAY_SIZE(tests); i++) {
3333
err = bpf_map_lookup_elem(map_fd, &tests[i].key, &num);
3434
CHECK(err || num != tests[i].num, tests[i].name,
3535
"err %d result %llx expected %llx\n",
@@ -58,7 +58,7 @@ static void test_global_data_string(struct bpf_object *obj, __u32 duration)
5858
{ "relocate .bss reference", 4, "\0\0hello" },
5959
};
6060

61-
for (i = 0; i < sizeof(tests) / sizeof(tests[0]); i++) {
61+
for (i = 0; i < ARRAY_SIZE(tests); i++) {
6262
err = bpf_map_lookup_elem(map_fd, &tests[i].key, str);
6363
CHECK(err || memcmp(str, tests[i].str, sizeof(str)),
6464
tests[i].name, "err %d result \'%s\' expected \'%s\'\n",
@@ -92,7 +92,7 @@ static void test_global_data_struct(struct bpf_object *obj, __u32 duration)
9292
{ "relocate .data reference", 3, { 41, 0xeeeeefef, 0x2111111111111111ULL, } },
9393
};
9494

95-
for (i = 0; i < sizeof(tests) / sizeof(tests[0]); i++) {
95+
for (i = 0; i < ARRAY_SIZE(tests); i++) {
9696
err = bpf_map_lookup_elem(map_fd, &tests[i].key, &val);
9797
CHECK(err || memcmp(&val, &tests[i].val, sizeof(val)),
9898
tests[i].name, "err %d result { %u, %u, %llu } expected { %u, %u, %llu }\n",

tools/testing/selftests/bpf/prog_tests/obj_name.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ void test_obj_name(void)
2020
__u32 duration = 0;
2121
int i;
2222

23-
for (i = 0; i < sizeof(tests) / sizeof(tests[0]); i++) {
23+
for (i = 0; i < ARRAY_SIZE(tests); i++) {
2424
size_t name_len = strlen(tests[i].name) + 1;
2525
union bpf_attr attr;
2626
size_t ncopy;

tools/testing/selftests/bpf/progs/syscall.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <bpf/bpf_tracing.h>
77
#include <../../../tools/include/linux/filter.h>
88
#include <linux/btf.h>
9+
#include <linux/kernel.h>
910

1011
char _license[] SEC("license") = "GPL";
1112

@@ -82,7 +83,7 @@ int bpf_prog(struct args *ctx)
8283
static __u64 value = 34;
8384
static union bpf_attr prog_load_attr = {
8485
.prog_type = BPF_PROG_TYPE_XDP,
85-
.insn_cnt = sizeof(insns) / sizeof(insns[0]),
86+
.insn_cnt = ARRAY_SIZE(insns),
8687
};
8788
int ret;
8889

tools/testing/selftests/bpf/progs/test_rdonly_maps.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#include <linux/ptrace.h>
55
#include <linux/bpf.h>
6+
#include <linux/kernel.h>
67
#include <bpf/bpf_helpers.h>
78

89
const struct {
@@ -64,7 +65,7 @@ int full_loop(struct pt_regs *ctx)
6465
{
6566
/* prevent compiler to optimize everything out */
6667
unsigned * volatile p = (void *)&rdonly_values.a;
67-
int i = sizeof(rdonly_values.a) / sizeof(rdonly_values.a[0]);
68+
int i = ARRAY_SIZE(rdonly_values.a);
6869
unsigned iters = 0, sum = 0;
6970

7071
/* validate verifier can allow full loop as well */

tools/testing/selftests/bpf/test_cgroup_storage.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ int main(int argc, char **argv)
3636
BPF_MOV64_REG(BPF_REG_0, BPF_REG_1),
3737
BPF_EXIT_INSN(),
3838
};
39-
size_t insns_cnt = sizeof(prog) / sizeof(struct bpf_insn);
39+
size_t insns_cnt = ARRAY_SIZE(prog);
4040
int error = EXIT_FAILURE;
4141
int map_fd, percpu_map_fd, prog_fd, cgroup_fd;
4242
struct bpf_cgroup_storage_key key;

tools/testing/selftests/bpf/test_lru_map.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -878,11 +878,11 @@ int main(int argc, char **argv)
878878
assert(nr_cpus != -1);
879879
printf("nr_cpus:%d\n\n", nr_cpus);
880880

881-
for (f = 0; f < sizeof(map_flags) / sizeof(*map_flags); f++) {
881+
for (f = 0; f < ARRAY_SIZE(map_flags); f++) {
882882
unsigned int tgt_free = (map_flags[f] & BPF_F_NO_COMMON_LRU) ?
883883
PERCPU_FREE_TARGET : LOCAL_FREE_TARGET;
884884

885-
for (t = 0; t < sizeof(map_types) / sizeof(*map_types); t++) {
885+
for (t = 0; t < ARRAY_SIZE(map_types); t++) {
886886
test_lru_sanity0(map_types[t], map_flags[f]);
887887
test_lru_sanity1(map_types[t], map_flags[f], tgt_free);
888888
test_lru_sanity2(map_types[t], map_flags[f], tgt_free);

tools/testing/selftests/bpf/test_sock_addr.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -723,7 +723,7 @@ static int xmsg_ret_only_prog_load(const struct sock_addr_test *test,
723723
BPF_MOV64_IMM(BPF_REG_0, rc),
724724
BPF_EXIT_INSN(),
725725
};
726-
return load_insns(test, insns, sizeof(insns) / sizeof(struct bpf_insn));
726+
return load_insns(test, insns, ARRAY_SIZE(insns));
727727
}
728728

729729
static int sendmsg_allow_prog_load(const struct sock_addr_test *test)
@@ -795,7 +795,7 @@ static int sendmsg4_rw_asm_prog_load(const struct sock_addr_test *test)
795795
BPF_EXIT_INSN(),
796796
};
797797

798-
return load_insns(test, insns, sizeof(insns) / sizeof(struct bpf_insn));
798+
return load_insns(test, insns, ARRAY_SIZE(insns));
799799
}
800800

801801
static int recvmsg4_rw_c_prog_load(const struct sock_addr_test *test)
@@ -858,7 +858,7 @@ static int sendmsg6_rw_dst_asm_prog_load(const struct sock_addr_test *test,
858858
BPF_EXIT_INSN(),
859859
};
860860

861-
return load_insns(test, insns, sizeof(insns) / sizeof(struct bpf_insn));
861+
return load_insns(test, insns, ARRAY_SIZE(insns));
862862
}
863863

864864
static int sendmsg6_rw_asm_prog_load(const struct sock_addr_test *test)

0 commit comments

Comments
 (0)