extmod/modlwip: Fix crash when calling recv on listening socket.

Add check to prevent calling recv on a socket in the listening state.  This
prevents a crash/hard fault when user code mistakenly tries to recv on the
listening socket instead of on the accepted connection.

Add corresponding test case to demonstrate the bug.

Signed-off-by: Andrew Leech <andrew.leech@planetinnovation.com.au>
This commit is contained in:
Andrew Leech
2025-05-21 11:16:54 +10:00
committed by Damien George
parent 2a46759fe8
commit 2f04381aeb
2 changed files with 64 additions and 15 deletions

View File

@@ -825,6 +825,12 @@ static mp_uint_t lwip_tcp_receive(lwip_socket_obj_t *socket, byte *buf, mp_uint_
// Check for any pending errors
STREAM_ERROR_CHECK(socket);
if (socket->state == STATE_LISTENING) {
// original socket in listening state, not the accepted connection.
*_errno = MP_ENOTCONN;
return -1;
}
if (socket->incoming.tcp.pbuf == NULL) {
// Non-blocking socket or flag