From cce95ff58c706bdd670fa17c24526815bf734555 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sun, 1 Mar 2026 07:22:27 -1000 Subject: [PATCH] [socket] Fix pre-existing bugs in LWIP raw TCP socket Fix two pre-existing bugs found during review of #14398: 1. bind() returns 0 on nullptr name instead of -1, inconsistent with POSIX bind() semantics and can mask invalid calls. 2. listen() doesn't re-register tcp_err after tcp_listen_with_backlog reallocates the PCB. The error callback was set during init() on the original PCB which is now freed, leaving the new listen PCB without an error handler. --- esphome/components/socket/lwip_raw_tcp_impl.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/esphome/components/socket/lwip_raw_tcp_impl.cpp b/esphome/components/socket/lwip_raw_tcp_impl.cpp index 6556979fe0..402ce86fd7 100644 --- a/esphome/components/socket/lwip_raw_tcp_impl.cpp +++ b/esphome/components/socket/lwip_raw_tcp_impl.cpp @@ -68,7 +68,7 @@ int LWIPRawCommon::bind(const struct sockaddr *name, socklen_t addrlen) { } if (name == nullptr) { errno = EINVAL; - return 0; + return -1; } ip_addr_t ip; in_port_t port; @@ -609,6 +609,7 @@ int LWIPRawListenImpl::listen(int backlog) { LWIP_LOG("tcp_arg(%p)", this->pcb_); tcp_arg(this->pcb_, this); tcp_accept(this->pcb_, LWIPRawListenImpl::s_accept_fn); + tcp_err(this->pcb_, LWIPRawListenImpl::s_err_fn); return 0; }