Skip to content

Commit d08a17d

Browse files
Björn Töpelborkmann
authored andcommitted
selftests/bpf: Consistent malloc/calloc usage
Use calloc instead of malloc where it makes sense, and avoid C++-style void *-cast. Signed-off-by: Björn Töpel <bjorn.topel@intel.com> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net> Link: https://lore.kernel.org/bpf/20210122154725.22140-12-bjorn.topel@gmail.com
1 parent 93dd4a0 commit d08a17d

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

tools/testing/selftests/bpf/xdpxceiver.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,7 @@ static int validate_interfaces(void)
411411
if (strcmp(ifdict[i]->nsname, "")) {
412412
struct targs *targs;
413413

414-
targs = (struct targs *)malloc(sizeof(struct targs));
414+
targs = malloc(sizeof(*targs));
415415
if (!targs)
416416
exit_with_error(errno);
417417

@@ -578,7 +578,7 @@ static void rx_pkt(struct xsk_socket_info *xsk, struct pollfd *fds)
578578
if (!pkt_node_rx)
579579
exit_with_error(errno);
580580

581-
pkt_node_rx->pkt_frame = (char *)malloc(PKT_SIZE);
581+
pkt_node_rx->pkt_frame = malloc(PKT_SIZE);
582582
if (!pkt_node_rx->pkt_frame)
583583
exit_with_error(errno);
584584

@@ -739,8 +739,8 @@ static void worker_pkt_validate(void)
739739
if (iphdr->version == IP_PKT_VER && iphdr->tos == IP_PKT_TOS) {
740740
payloadseqnum = *((uint32_t *)(pkt_node_rx_q->pkt_frame + PKT_HDR_SIZE));
741741
if (debug_pkt_dump && payloadseqnum != EOT) {
742-
pkt_obj = (struct pkt_frame *)malloc(sizeof(struct pkt_frame));
743-
pkt_obj->payload = (char *)malloc(PKT_SIZE);
742+
pkt_obj = malloc(sizeof(*pkt_obj));
743+
pkt_obj->payload = malloc(PKT_SIZE);
744744
memcpy(pkt_obj->payload, pkt_node_rx_q->pkt_frame, PKT_SIZE);
745745
pkt_buf[payloadseqnum] = pkt_obj;
746746
}
@@ -865,7 +865,7 @@ static void *worker_testapp_validate(void *arg)
865865

866866
TAILQ_INIT(&head);
867867
if (debug_pkt_dump) {
868-
pkt_buf = malloc(sizeof(struct pkt_frame **) * num_frames);
868+
pkt_buf = calloc(num_frames, sizeof(*pkt_buf));
869869
if (!pkt_buf)
870870
exit_with_error(errno);
871871
}
@@ -1017,7 +1017,7 @@ int main(int argc, char **argv)
10171017
u16 UDP_DST_PORT = 2020;
10181018
u16 UDP_SRC_PORT = 2121;
10191019

1020-
ifaceconfig = (struct ifaceconfigobj *)malloc(sizeof(struct ifaceconfigobj));
1020+
ifaceconfig = malloc(sizeof(struct ifaceconfigobj));
10211021
memcpy(ifaceconfig->dst_mac, MAC1, ETH_ALEN);
10221022
memcpy(ifaceconfig->src_mac, MAC2, ETH_ALEN);
10231023
inet_aton(IP1, &ifaceconfig->dst_ip);
@@ -1026,7 +1026,7 @@ int main(int argc, char **argv)
10261026
ifaceconfig->src_port = UDP_SRC_PORT;
10271027

10281028
for (int i = 0; i < MAX_INTERFACES; i++) {
1029-
ifdict[i] = (struct ifobject *)malloc(sizeof(struct ifobject));
1029+
ifdict[i] = malloc(sizeof(struct ifobject));
10301030
if (!ifdict[i])
10311031
exit_with_error(errno);
10321032

0 commit comments

Comments
 (0)