Skip to content

Commit f859813

Browse files
mrpreanakryiko
authored andcommitted
selftests/bpf: Fix incorrect array size calculation
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 <[email protected]> Signed-off-by: Jiayuan Chen <[email protected]> Signed-off-by: Andrii Nakryiko <[email protected]> Link: https://lore.kernel.org/bpf/[email protected] Closes: https://lore.kernel.org/bpf/[email protected]/
1 parent 60ef541 commit f859813

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

tools/testing/selftests/bpf/benchs/bench_sockmap.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <argp.h>
1111
#include "bench.h"
1212
#include "bench_sockmap_prog.skel.h"
13+
#include "bpf_util.h"
1314

1415
#define FILE_SIZE (128 * 1024)
1516
#define DATA_REPEAT_SIZE 10
@@ -124,8 +125,8 @@ static void bench_sockmap_prog_destroy(void)
124125
{
125126
int i;
126127

127-
for (i = 0; i < sizeof(ctx.fds); i++) {
128-
if (ctx.fds[0] > 0)
128+
for (i = 0; i < ARRAY_SIZE(ctx.fds); i++) {
129+
if (ctx.fds[i] > 0)
129130
close(ctx.fds[i]);
130131
}
131132

0 commit comments

Comments
 (0)