[PATCH -next] selftests/bpf: Use ARRAY_SIZE for array length

Jiapeng Chong posted 1 patch 2 months ago
There is a newer version of this series
tools/testing/selftests/bpf/prog_tests/core_reloc_raw.c | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)
[PATCH -next] selftests/bpf: Use ARRAY_SIZE for array length
Posted by Jiapeng Chong 2 months ago
Use of macro ARRAY_SIZE to calculate array size minimizes
the redundant code and improves code reusability.

./tools/testing/selftests/bpf/prog_tests/core_reloc_raw.c:98:34-35: WARNING: Use ARRAY_SIZE.
./tools/testing/selftests/bpf/prog_tests/core_reloc_raw.c:93:29-30: WARNING: Use ARRAY_SIZE.
./tools/testing/selftests/bpf/prog_tests/core_reloc_raw.c:101:34-35: WARNING: Use ARRAY_SIZE.

Reported-by: Abaci Robot <abaci@linux.alibaba.com>
Closes: https://bugzilla.openanolis.cn/show_bug.cgi?id=11167
Signed-off-by: Jiapeng Chong <jiapeng.chong@linux.alibaba.com>
---
 tools/testing/selftests/bpf/prog_tests/core_reloc_raw.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/tools/testing/selftests/bpf/prog_tests/core_reloc_raw.c b/tools/testing/selftests/bpf/prog_tests/core_reloc_raw.c
index a18d3680fb16..5e576c6cecca 100644
--- a/tools/testing/selftests/bpf/prog_tests/core_reloc_raw.c
+++ b/tools/testing/selftests/bpf/prog_tests/core_reloc_raw.c
@@ -7,7 +7,7 @@
 #include <sys/syscall.h>
 #include <bpf/libbpf.h>
 #include <bpf/btf.h>
-
+#include "kselftest.h"
 #include "test_progs.h"
 #include "test_btf.h"
 #include "bpf/libbpf_internal.h"
@@ -90,15 +90,15 @@ static void test_bad_local_id(void)
 	attr.prog_type = BPF_TRACE_RAW_TP;
 	attr.license = (__u64)"GPL";
 	attr.insns = (__u64)&insns;
-	attr.insn_cnt = sizeof(insns) / sizeof(*insns);
+	attr.insn_cnt = ARRAY_SIZE(insns);
 	attr.log_buf = (__u64)log;
 	attr.log_size = sizeof(log);
 	attr.log_level = log_level;
 	attr.func_info = (__u64)funcs;
-	attr.func_info_cnt = sizeof(funcs) / sizeof(*funcs);
+	attr.func_info_cnt = ARRAY_SIZE(funcs);
 	attr.func_info_rec_size = sizeof(*funcs);
 	attr.core_relos = (__u64)relos;
-	attr.core_relo_cnt = sizeof(relos) / sizeof(*relos);
+	attr.core_relo_cnt = ARRAY_SIZE(relos);
 	attr.core_relo_rec_size = sizeof(*relos);
 	prog_fd = sys_bpf_prog_load(&attr, sizeof(attr), 1);
 	saved_errno = errno;
-- 
2.32.0.3.g01195cf9f
Re: [PATCH -next] selftests/bpf: Use ARRAY_SIZE for array length
Posted by Andrii Nakryiko 2 months ago
On Fri, Sep 27, 2024 at 1:23 AM Jiapeng Chong
<jiapeng.chong@linux.alibaba.com> wrote:
>
> Use of macro ARRAY_SIZE to calculate array size minimizes
> the redundant code and improves code reusability.
>
> ./tools/testing/selftests/bpf/prog_tests/core_reloc_raw.c:98:34-35: WARNING: Use ARRAY_SIZE.
> ./tools/testing/selftests/bpf/prog_tests/core_reloc_raw.c:93:29-30: WARNING: Use ARRAY_SIZE.
> ./tools/testing/selftests/bpf/prog_tests/core_reloc_raw.c:101:34-35: WARNING: Use ARRAY_SIZE.
>
> Reported-by: Abaci Robot <abaci@linux.alibaba.com>
> Closes: https://bugzilla.openanolis.cn/show_bug.cgi?id=11167
> Signed-off-by: Jiapeng Chong <jiapeng.chong@linux.alibaba.com>
> ---
>  tools/testing/selftests/bpf/prog_tests/core_reloc_raw.c | 8 ++++----
>  1 file changed, 4 insertions(+), 4 deletions(-)
>
> diff --git a/tools/testing/selftests/bpf/prog_tests/core_reloc_raw.c b/tools/testing/selftests/bpf/prog_tests/core_reloc_raw.c
> index a18d3680fb16..5e576c6cecca 100644
> --- a/tools/testing/selftests/bpf/prog_tests/core_reloc_raw.c
> +++ b/tools/testing/selftests/bpf/prog_tests/core_reloc_raw.c
> @@ -7,7 +7,7 @@
>  #include <sys/syscall.h>
>  #include <bpf/libbpf.h>
>  #include <bpf/btf.h>
> -
> +#include "kselftest.h"

this doesn't compile, and I don't think you need this include

pw-bot: cr

>  #include "test_progs.h"
>  #include "test_btf.h"
>  #include "bpf/libbpf_internal.h"
> @@ -90,15 +90,15 @@ static void test_bad_local_id(void)
>         attr.prog_type = BPF_TRACE_RAW_TP;
>         attr.license = (__u64)"GPL";
>         attr.insns = (__u64)&insns;
> -       attr.insn_cnt = sizeof(insns) / sizeof(*insns);
> +       attr.insn_cnt = ARRAY_SIZE(insns);
>         attr.log_buf = (__u64)log;
>         attr.log_size = sizeof(log);
>         attr.log_level = log_level;
>         attr.func_info = (__u64)funcs;
> -       attr.func_info_cnt = sizeof(funcs) / sizeof(*funcs);
> +       attr.func_info_cnt = ARRAY_SIZE(funcs);
>         attr.func_info_rec_size = sizeof(*funcs);
>         attr.core_relos = (__u64)relos;
> -       attr.core_relo_cnt = sizeof(relos) / sizeof(*relos);
> +       attr.core_relo_cnt = ARRAY_SIZE(relos);
>         attr.core_relo_rec_size = sizeof(*relos);
>         prog_fd = sys_bpf_prog_load(&attr, sizeof(attr), 1);
>         saved_errno = errno;
> --
> 2.32.0.3.g01195cf9f
>