Commit f98d6dd1 authored by Guo Zhengkui's avatar Guo Zhengkui Committed by Daniel Borkmann
Browse files

selftests/bpf: Clean up array_size.cocci warnings



Clean up the array_size.cocci warnings under tools/testing/selftests/bpf/:

Use `ARRAY_SIZE(arr)` instead of forms like `sizeof(arr)/sizeof(arr[0])`.

tools/testing/selftests/bpf/test_cgroup_storage.c uses ARRAY_SIZE() defined
in tools/include/linux/kernel.h (sys/sysinfo.h -> linux/kernel.h), while
others use ARRAY_SIZE() in bpf_util.h.

Signed-off-by: default avatarGuo Zhengkui <guozhengkui@vivo.com>
Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
Link: https://lore.kernel.org/bpf/20220315130143.2403-1-guozhengkui@vivo.com
parent 8fa42d78
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -14,7 +14,7 @@ static int prog_load(void)
		BPF_MOV64_IMM(BPF_REG_0, 1), /* r0 = 1 */
		BPF_EXIT_INSN(),
	};
	size_t insns_cnt = sizeof(prog) / sizeof(struct bpf_insn);
	size_t insns_cnt = ARRAY_SIZE(prog);

	return bpf_test_load_program(BPF_PROG_TYPE_CGROUP_SKB,
			       prog, insns_cnt, "GPL", 0,
+1 −1
Original line number Diff line number Diff line
@@ -63,7 +63,7 @@ static int prog_load_cnt(int verdict, int val)
		BPF_MOV64_IMM(BPF_REG_0, verdict), /* r0 = verdict */
		BPF_EXIT_INSN(),
	};
	size_t insns_cnt = sizeof(prog) / sizeof(struct bpf_insn);
	size_t insns_cnt = ARRAY_SIZE(prog);
	int ret;

	ret = bpf_test_load_program(BPF_PROG_TYPE_CGROUP_SKB,
+1 −1
Original line number Diff line number Diff line
@@ -16,7 +16,7 @@ static int prog_load(int verdict)
		BPF_MOV64_IMM(BPF_REG_0, verdict), /* r0 = verdict */
		BPF_EXIT_INSN(),
	};
	size_t insns_cnt = sizeof(prog) / sizeof(struct bpf_insn);
	size_t insns_cnt = ARRAY_SIZE(prog);

	return bpf_test_load_program(BPF_PROG_TYPE_CGROUP_SKB,
			       prog, insns_cnt, "GPL", 0,
+3 −3
Original line number Diff line number Diff line
@@ -29,7 +29,7 @@ static void test_global_data_number(struct bpf_object *obj, __u32 duration)
		{ "relocate .rodata reference", 10, ~0 },
	};

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

	for (i = 0; i < sizeof(tests) / sizeof(tests[0]); i++) {
	for (i = 0; i < ARRAY_SIZE(tests); i++) {
		err = bpf_map_lookup_elem(map_fd, &tests[i].key, str);
		CHECK(err || memcmp(str, tests[i].str, sizeof(str)),
		      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)
		{ "relocate .data reference",   3, { 41, 0xeeeeefef, 0x2111111111111111ULL, } },
	};

	for (i = 0; i < sizeof(tests) / sizeof(tests[0]); i++) {
	for (i = 0; i < ARRAY_SIZE(tests); i++) {
		err = bpf_map_lookup_elem(map_fd, &tests[i].key, &val);
		CHECK(err || memcmp(&val, &tests[i].val, sizeof(val)),
		      tests[i].name, "err %d result { %u, %u, %llu } expected { %u, %u, %llu }\n",
+1 −1
Original line number Diff line number Diff line
@@ -20,7 +20,7 @@ void test_obj_name(void)
	__u32 duration = 0;
	int i;

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