diff --git a/drivers/wifi/eswifi/eswifi_socket.c b/drivers/wifi/eswifi/eswifi_socket.c index 9ae607b2341a6..f4c546f0ff0a7 100644 --- a/drivers/wifi/eswifi/eswifi_socket.c +++ b/drivers/wifi/eswifi/eswifi_socket.c @@ -133,33 +133,40 @@ static void eswifi_off_read_work(struct k_work *work) __select_socket(eswifi, socket->index); + /* Verify if we can allocate a rx packet before reading data to prevent leaks */ + pkt = net_pkt_rx_alloc_with_buffer(eswifi->iface, 1460, + AF_UNSPEC, 0, K_NO_WAIT); + if (!pkt) { + LOG_ERR("Cannot allocate rx packet"); + goto done; + } + len = __read_data(eswifi, 1460, &data); /* 1460 is max size */ if (len < 0) { __stop_socket(eswifi, socket); if (socket->recv_cb) { /* send EOF (null pkt) */ + net_pkt_unref(pkt); + pkt = NULL; goto do_recv_cb; } } if (!len || !socket->recv_cb) { + net_pkt_unref(pkt); goto done; } LOG_DBG("payload sz = %d", len); - pkt = net_pkt_rx_alloc_with_buffer(eswifi->iface, len, - AF_UNSPEC, 0, K_NO_WAIT); - if (!pkt) { - LOG_ERR("Cannot allocate rx packet"); - goto done; - } - if (net_pkt_write(pkt, data, len) < 0) { LOG_WRN("Incomplete buffer copy"); } + /* Resize the packet */ + net_pkt_trim_buffer(pkt); + net_pkt_cursor_init(pkt); do_recv_cb: