On 3/8/22 10:17 AM, Guo Zhengkui wrote:
> 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 <[email protected]>
[...]
> diff --git a/tools/testing/selftests/bpf/progs/syscall.c b/tools/testing/selftests/bpf/progs/syscall.c
> index e550f728962d..85c6e7849463 100644
> --- a/tools/testing/selftests/bpf/progs/syscall.c
> +++ b/tools/testing/selftests/bpf/progs/syscall.c
> @@ -6,6 +6,7 @@
> #include <bpf/bpf_tracing.h>
> #include <../../../tools/include/linux/filter.h>
> #include <linux/btf.h>
> +#include <linux/kernel.h>
>
> char _license[] SEC("license") = "GPL";
>
> @@ -82,7 +83,7 @@ int bpf_prog(struct args *ctx)
> static __u64 value = 34;
> static union bpf_attr prog_load_attr = {
> .prog_type = BPF_PROG_TYPE_XDP,
> - .insn_cnt = sizeof(insns) / sizeof(insns[0]),
> + .insn_cnt = ARRAY_SIZE(insns),
> };
> int ret;
>
> diff --git a/tools/testing/selftests/bpf/progs/test_rdonly_maps.c b/tools/testing/selftests/bpf/progs/test_rdonly_maps.c
> index fc8e8a34a3db..ca75aac745a4 100644
> --- a/tools/testing/selftests/bpf/progs/test_rdonly_maps.c
> +++ b/tools/testing/selftests/bpf/progs/test_rdonly_maps.c
> @@ -3,6 +3,7 @@
>
> #include <linux/ptrace.h>
> #include <linux/bpf.h>
> +#include <linux/kernel.h>
> #include <bpf/bpf_helpers.h>
>
> const struct {
> @@ -64,7 +65,7 @@ int full_loop(struct pt_regs *ctx)
> {
> /* prevent compiler to optimize everything out */
> unsigned * volatile p = (void *)&rdonly_values.a;
> - int i = sizeof(rdonly_values.a) / sizeof(rdonly_values.a[0]);
> + int i = ARRAY_SIZE(rdonly_values.a);
> unsigned iters = 0, sum = 0;
>
There's bpf_util.h with ARRAY_SIZE definition which is used in selftests, pls change to
reuse that one.
Thanks,
Daniel