From f6e436c34ced465f9c950dff27f6c3f9692c7964 Mon Sep 17 00:00:00 2001 From: Fabio Alessandrelli Date: Thu, 13 Jul 2023 16:32:40 +0200 Subject: [PATCH] [Net] Explicitly handle buffer errors in send/recv (cherry picked from commit 28001b9ef642d82df2fc36989b1d7b52c7b7ccba) --- drivers/unix/net_socket_posix.cpp | 20 ++++++++++++++++++++ drivers/unix/net_socket_posix.h | 1 + 2 files changed, 21 insertions(+) diff --git a/drivers/unix/net_socket_posix.cpp b/drivers/unix/net_socket_posix.cpp index b46af012f11..a8074aa3f67 100644 --- a/drivers/unix/net_socket_posix.cpp +++ b/drivers/unix/net_socket_posix.cpp @@ -204,6 +204,9 @@ NetSocketPosix::NetError NetSocketPosix::_get_socket_error() const { if (err == WSAEACCES) { return ERR_NET_UNAUTHORIZED; } + if (err == WSAEMSGSIZE || err == WSAENOBUFS) { + return ERR_NET_BUFFER_TOO_SMALL; + } print_verbose("Socket error: " + itos(err)); return ERR_NET_OTHER; #else @@ -222,6 +225,9 @@ NetSocketPosix::NetError NetSocketPosix::_get_socket_error() const { if (errno == EACCES) { return ERR_NET_UNAUTHORIZED; } + if (errno == ENOBUFS) { + return ERR_NET_BUFFER_TOO_SMALL; + } print_verbose("Socket error: " + itos(errno)); return ERR_NET_OTHER; #endif @@ -550,6 +556,10 @@ Error NetSocketPosix::recv(uint8_t *p_buffer, int p_len, int &r_read) { return ERR_BUSY; } + if (err == ERR_NET_BUFFER_TOO_SMALL) { + return ERR_OUT_OF_MEMORY; + } + return FAILED; } @@ -571,6 +581,10 @@ Error NetSocketPosix::recvfrom(uint8_t *p_buffer, int p_len, int &r_read, IPAddr return ERR_BUSY; } + if (err == ERR_NET_BUFFER_TOO_SMALL) { + return ERR_OUT_OF_MEMORY; + } + return FAILED; } @@ -606,6 +620,9 @@ Error NetSocketPosix::send(const uint8_t *p_buffer, int p_len, int &r_sent) { if (err == ERR_NET_WOULD_BLOCK) { return ERR_BUSY; } + if (err == ERR_NET_BUFFER_TOO_SMALL) { + return ERR_OUT_OF_MEMORY; + } return FAILED; } @@ -625,6 +642,9 @@ Error NetSocketPosix::sendto(const uint8_t *p_buffer, int p_len, int &r_sent, IP if (err == ERR_NET_WOULD_BLOCK) { return ERR_BUSY; } + if (err == ERR_NET_BUFFER_TOO_SMALL) { + return ERR_OUT_OF_MEMORY; + } return FAILED; } diff --git a/drivers/unix/net_socket_posix.h b/drivers/unix/net_socket_posix.h index bd2088b4f95..2682530e151 100644 --- a/drivers/unix/net_socket_posix.h +++ b/drivers/unix/net_socket_posix.h @@ -56,6 +56,7 @@ private: ERR_NET_IN_PROGRESS, ERR_NET_ADDRESS_INVALID_OR_UNAVAILABLE, ERR_NET_UNAUTHORIZED, + ERR_NET_BUFFER_TOO_SMALL, ERR_NET_OTHER, };