This is an automated email from the ASF dual-hosted git repository.

xiaoxiang781216 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git

commit e2e9440439a4da3426dc692d34f8229be300f824
Author: Marco Casaroli <[email protected]>
AuthorDate: Sat Nov 21 17:06:53 2026 +0100

    net/tcp: Fix readv() and recvmsg() with more than one buffer.
    
    psock_tcp_recvfrom() does one receive for each buffer of the iovec
    array, and stops at the first result <= 0.
    
    1. A zero-length buffer gives a result of 0.  When the first buffer is
       empty, readv() returns 0 (end of file) while data waits, or EAGAIN
       for a non-blocking receive.  Skip an empty buffer.  The last buffer
       still does a receive, so that a receive of zero bytes keeps its
       result.
    
    2. When the data fill the first buffer exactly, the receive for the
       next buffer waits for new data (until SO_RCVTIMEO, or forever).
       After data, do the receive for the next buffers with MSG_DONTWAIT,
       unless MSG_WAITALL is set.  This is what local sockets do already.
    
    Assisted-by: Claude Code:claude-opus-5-5
    Signed-off-by: Marco Casaroli <[email protected]>
---
 net/tcp/tcp_recvfrom.c | 15 ++++++++++++++-
 1 file changed, 14 insertions(+), 1 deletion(-)

diff --git a/net/tcp/tcp_recvfrom.c b/net/tcp/tcp_recvfrom.c
index 7ec13d5c0c4..6b5eabe5333 100644
--- a/net/tcp/tcp_recvfrom.c
+++ b/net/tcp/tcp_recvfrom.c
@@ -908,7 +908,20 @@ ssize_t psock_tcp_recvfrom(FAR struct socket *psock, FAR 
struct msghdr *msg,
       FAR void *buf = msg->msg_iov[i].iov_base;
       size_t len = msg->msg_iov[i].iov_len;
 
-      ret = tcp_recvfrom_one(conn, buf, len, from, fromlen, flags);
+      /* An empty buffer takes no data.  The last one still does a receive,
+       * so that a receive of zero bytes reports the connection state.
+       */
+
+      if (len == 0 && i + 1 < msg->msg_iovlen)
+        {
+          continue;
+        }
+
+      /* After data, the next buffers take only the data that is there */
+
+      ret = tcp_recvfrom_one(conn, buf, len, from, fromlen,
+                             nrecv > 0 && (flags & MSG_WAITALL) == 0 ?
+                             flags | MSG_DONTWAIT : flags);
       if (ret <= 0)
         {
           break;

Reply via email to