Skip to content

Commit

Permalink
selftests: net: fix array_size.cocci warning
Browse files Browse the repository at this point in the history
Fix array_size.cocci warning in tools/testing/selftests/net.

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

It has been tested with gcc (Debian 8.3.0-6) 8.3.0.

Signed-off-by: Guo Zhengkui <guozhengkui@vivo.com>
Link: https://lore.kernel.org/r/20220316092858.9398-1-guozhengkui@vivo.com
Signed-off-by: Paolo Abeni <pabeni@redhat.com>
  • Loading branch information
Guo Zhengkui authored and Paolo Abeni committed Mar 17, 2022
1 parent 58e06d0 commit 1abea24
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 5 deletions.
4 changes: 3 additions & 1 deletion tools/testing/selftests/net/cmsg_sender.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
#include <linux/udp.h>
#include <sys/socket.h>

#include "../kselftest.h"

enum {
ERN_SUCCESS = 0,
/* Well defined errors, callers may depend on these */
Expand Down Expand Up @@ -318,7 +320,7 @@ static const char *cs_ts_info2str(unsigned int info)
[SCM_TSTAMP_ACK] = "ACK",
};

if (info < sizeof(names) / sizeof(names[0]))
if (info < ARRAY_SIZE(names))
return names[info];
return "unknown";
}
Expand Down
5 changes: 3 additions & 2 deletions tools/testing/selftests/net/psock_fanout.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
#include <unistd.h>

#include "psock_lib.h"
#include "../kselftest.h"

#define RING_NUM_FRAMES 20

Expand Down Expand Up @@ -117,7 +118,7 @@ static void sock_fanout_set_cbpf(int fd)
struct sock_fprog bpf_prog;

bpf_prog.filter = bpf_filter;
bpf_prog.len = sizeof(bpf_filter) / sizeof(struct sock_filter);
bpf_prog.len = ARRAY_SIZE(bpf_filter);

if (setsockopt(fd, SOL_PACKET, PACKET_FANOUT_DATA, &bpf_prog,
sizeof(bpf_prog))) {
Expand Down Expand Up @@ -162,7 +163,7 @@ static void sock_fanout_set_ebpf(int fd)
memset(&attr, 0, sizeof(attr));
attr.prog_type = BPF_PROG_TYPE_SOCKET_FILTER;
attr.insns = (unsigned long) prog;
attr.insn_cnt = sizeof(prog) / sizeof(prog[0]);
attr.insn_cnt = ARRAY_SIZE(prog);
attr.license = (unsigned long) "GPL";
attr.log_buf = (unsigned long) log_buf,
attr.log_size = sizeof(log_buf),
Expand Down
6 changes: 4 additions & 2 deletions tools/testing/selftests/net/toeplitz.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@
#include <sys/types.h>
#include <unistd.h>

#include "../kselftest.h"

#define TOEPLITZ_KEY_MIN_LEN 40
#define TOEPLITZ_KEY_MAX_LEN 60

Expand Down Expand Up @@ -295,7 +297,7 @@ static void __set_filter(int fd, int off_proto, uint8_t proto, int off_dport)
struct sock_fprog prog = {};

prog.filter = filter;
prog.len = sizeof(filter) / sizeof(struct sock_filter);
prog.len = ARRAY_SIZE(filter);
if (setsockopt(fd, SOL_SOCKET, SO_ATTACH_FILTER, &prog, sizeof(prog)))
error(1, errno, "setsockopt filter");
}
Expand Down Expand Up @@ -324,7 +326,7 @@ static void set_filter_null(int fd)
struct sock_fprog prog = {};

prog.filter = filter;
prog.len = sizeof(filter) / sizeof(struct sock_filter);
prog.len = ARRAY_SIZE(filter);
if (setsockopt(fd, SOL_SOCKET, SO_ATTACH_FILTER, &prog, sizeof(prog)))
error(1, errno, "setsockopt filter");
}
Expand Down

0 comments on commit 1abea24

Please sign in to comment.