From 8422392998f8174389eef28fe7f3edf04025ac27 Mon Sep 17 00:00:00 2001 From: Chaitanya Tata Date: Tue, 2 Jan 2024 14:37:32 +0530 Subject: [PATCH] [nrf noup] zephyr: Fix infinite loop in select fixup! [nrf noup] zephyr: Add support for WPA CLI zephyr In case of low-memory conditions e.g., in stress tests, the allocation failure causes the callback to skip recv, there by leaving data on the socket and causing select to trigger the callback in an infinite loop. This infinite loop causes threads starvation as WPA supplicant thread runs at a higher priority, and is manifested as shell freeze. Fix this by discarding the data on socket in case of memory allocation failure. Fixes SHEL-1967. Signed-off-by: Chaitanya Tata --- wpa_supplicant/ctrl_iface_zephyr.c | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/wpa_supplicant/ctrl_iface_zephyr.c b/wpa_supplicant/ctrl_iface_zephyr.c index 5e17860de..f4a0d6d76 100644 --- a/wpa_supplicant/ctrl_iface_zephyr.c +++ b/wpa_supplicant/ctrl_iface_zephyr.c @@ -126,8 +126,20 @@ static void wpa_supplicant_ctrl_iface_receive(int sock, void *eloop_ctx, size_t reply_len = 0; buf = os_zalloc(CTRL_IFACE_MAX_LEN + 1); - if (!buf) + if (!buf) { + /* Do a dummy read to drain the data from the socket */ + static unsigned char dummy[512]; + + /* This is expected in OOM conditions, so, do not spam the log */ + wpa_printf(MSG_DEBUG, "Failed to allocate memory for ctrl_iface receive buffer"); + + do { + res = recv(sock, dummy, sizeof(dummy), + MSG_TRUNC | MSG_DONTWAIT); + } while (res > 0); return; + } + res = recv(sock, buf, CTRL_IFACE_MAX_LEN, 0); if (res < 0) { wpa_printf(MSG_ERROR, "recvfrom(ctrl_iface): %s",