Skip to contentSkip to conversation

selftests/bpf: Use ARRAY_SIZE for array length

PATCH In review Jiapeng Chong 2 participants 2 messages Feb 2, 2024

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 selftests/bpf: Use ARRAY_SIZE for array length

Use of macro ARRAY_SIZE to calculate array size minimizes the redundant code and improves code reusability.

./tools/testing/selftests/bpf/progs/syscall.c:122:26-27: WARNING: Use ARRAY_SIZE.

---
 tools/testing/selftests/bpf/progs/syscall.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/testing/selftests/bpf/progs/syscall.c +1 -1
index 3d3cafdebe72..297a34f224c3 100644
@@ -119,7 +119,7 @@ int load_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;
 
Signature · 1 line
2.20.1.7.g153144c
#1 lore raw
Quoted 17 lines · Jiapeng Chong

Use of macro ARRAY_SIZE to calculate array size minimizes the redundant code and improves code reusability.

./tools/testing/selftests/bpf/progs/syscall.c:122:26-27: WARNING: Use ARRAY_SIZE.

---
  tools/testing/selftests/bpf/progs/syscall.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
tools/testing/selftests/bpf/progs/syscall.c +0 -0
index 3d3cafdebe72..297a34f224c3 100644

tools/testing/selftests/bpf/progs/syscall.c int load_prog(struct args *ctx) · line 122

Quoted 5 lines
tools/testing/selftests/bpf/progs/syscall.c +1 -1
@@ -119,7 +119,7 @@ int load_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)

This does not even compile: https://github.com/kernel-patches/bpf/actions/runs/7761734279/job/21170796228

The existing code here is fine.

Quoted 3 lines
tools/testing/selftests/bpf/progs/syscall.c +0 -0
  	};
  	int ret;