From 2f5f0df276ec5812083fb0c9e7e3d97c848eb42c Mon Sep 17 00:00:00 2001 From: Jukka Rissanen Date: Fri, 5 Sep 2025 10:24:25 +0300 Subject: [PATCH] net: context: Make sure TCP allocation is not leaked If we have allocated a TCP connection, and if after that we get an error like EADDRINUSE, then we must de-allocate the TCP connection otherwise there is a buffer leak. Signed-off-by: Jukka Rissanen (cherry picked from commit 909710b598cf1d8836c53e65ae168e21ba5f22bd) --- subsys/net/ip/net_context.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/subsys/net/ip/net_context.c b/subsys/net/ip/net_context.c index 26937bd0b9997..3126da3dcc329 100644 --- a/subsys/net/ip/net_context.c +++ b/subsys/net/ip/net_context.c @@ -553,6 +553,13 @@ int net_context_get(sa_family_t family, enum net_sock_type type, uint16_t proto, k_sem_give(&contexts_lock); if (ret < 0) { + if (ret == -EADDRINUSE && + !net_if_is_ip_offloaded(net_if_get_default()) && + proto == IPPROTO_TCP) { + /* Free the TCP context that we allocated earlier */ + net_tcp_put(&contexts[i]); + } + return ret; }