]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
selftests/bpf: Fix incorrect array size calculation
authorJiayuan Chen <jiayuan.chen@linux.dev>
Tue, 9 Sep 2025 12:47:04 +0000 (20:47 +0800)
committerAndrii Nakryiko <andrii@kernel.org>
Tue, 9 Sep 2025 16:23:47 +0000 (09:23 -0700)
The loop in bench_sockmap_prog_destroy() has two issues:

1. Using 'sizeof(ctx.fds)' as the loop bound results in the number of
   bytes, not the number of file descriptors, causing the loop to iterate
   far more times than intended.

2. The condition 'ctx.fds[0] > 0' incorrectly checks only the first fd for
   all iterations, potentially leaving file descriptors unclosed. Change
   it to 'ctx.fds[i] > 0' to check each fd properly.

These fixes ensure correct cleanup of all file descriptors when the
benchmark exits.

Reported-by: Dan Carpenter <dan.carpenter@linaro.org>
Signed-off-by: Jiayuan Chen <jiayuan.chen@linux.dev>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/bpf/20250909124721.191555-1-jiayuan.chen@linux.dev
Closes: https://lore.kernel.org/bpf/aLqfWuRR9R_KTe5e@stanley.mountain/
tools/testing/selftests/bpf/benchs/bench_sockmap.c

index 8ebf563a67a2b5c649d90f0d6595909b3bba4a67..cfc072aa7fff7e835391210edacb4713b6befe6f 100644 (file)
@@ -10,6 +10,7 @@
 #include <argp.h>
 #include "bench.h"
 #include "bench_sockmap_prog.skel.h"
+#include "bpf_util.h"
 
 #define FILE_SIZE (128 * 1024)
 #define DATA_REPEAT_SIZE 10
@@ -124,8 +125,8 @@ static void bench_sockmap_prog_destroy(void)
 {
        int i;
 
-       for (i = 0; i < sizeof(ctx.fds); i++) {
-               if (ctx.fds[0] > 0)
+       for (i = 0; i < ARRAY_SIZE(ctx.fds); i++) {
+               if (ctx.fds[i] > 0)
                        close(ctx.fds[i]);
        }