Address review: add TCP type check and null optval guard

- Check NETCONNTYPE_GROUP is NETCONN_TCP before accessing tcp_pcb
- Add optval != nullptr guard before dereferencing in fast path
This commit is contained in:
J. Nick Koston
2026-03-10 22:28:08 -10:00
parent c530e52669
commit 426251f705
4 changed files with 5 additions and 3 deletions
+1 -1
View File
@@ -59,7 +59,7 @@ class BSDSocketImpl {
#if defined(USE_LWIP_FAST_SELECT) && defined(CONFIG_LWIP_TCPIP_CORE_LOCKING)
// Fast path for TCP_NODELAY: directly set the pcb flag under the TCPIP core lock,
// bypassing lwip_setsockopt overhead (socket lookups, hook, switch cascade, refcounting).
if (level == IPPROTO_TCP && optname == TCP_NODELAY && optlen == sizeof(int)) {
if (level == IPPROTO_TCP && optname == TCP_NODELAY && optlen == sizeof(int) && optval != nullptr) {
LwIPLock lock;
if (esphome_lwip_set_nodelay(this->cached_sock_, *reinterpret_cast<const int *>(optval) != 0))
return 0;
@@ -55,7 +55,7 @@ class LwIPSocketImpl {
#if defined(USE_LWIP_FAST_SELECT) && defined(CONFIG_LWIP_TCPIP_CORE_LOCKING)
// Fast path for TCP_NODELAY: directly set the pcb flag under the TCPIP core lock,
// bypassing lwip_setsockopt overhead (socket lookups, hook, switch cascade, refcounting).
if (level == IPPROTO_TCP && optname == TCP_NODELAY && optlen == sizeof(int)) {
if (level == IPPROTO_TCP && optname == TCP_NODELAY && optlen == sizeof(int) && optval != nullptr) {
LwIPLock lock;
if (esphome_lwip_set_nodelay(this->cached_sock_, *reinterpret_cast<const int *>(optval) != 0))
return 0;
+2
View File
@@ -220,6 +220,8 @@ void esphome_lwip_hook_socket(struct lwip_sock *sock) {
bool esphome_lwip_set_nodelay(struct lwip_sock *sock, bool enable) {
if (sock == NULL || sock->conn == NULL || sock->conn->pcb.tcp == NULL)
return false;
if (NETCONNTYPE_GROUP(sock->conn->type) != NETCONN_TCP)
return false;
if (enable) {
tcp_nagle_disable(sock->conn->pcb.tcp);
} else {
+1 -1
View File
@@ -70,7 +70,7 @@ void esphome_lwip_wake_main_loop_from_isr(int *px_higher_priority_task_woken);
/// Must be called with the TCPIP core lock held (LwIPLock in C++).
/// This bypasses lwip_setsockopt() overhead (socket lookups, switch cascade,
/// hooks, refcounting) — just a direct pcb->flags bit set/clear.
/// Returns true if successful, false if sock/conn/pcb is NULL.
/// Returns true if successful, false if sock/conn/pcb is NULL or the socket is not TCP.
bool esphome_lwip_set_nodelay(struct lwip_sock *sock, bool enable);
/// Wake the main loop task from any context (ISR, thread, or main loop).