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

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


The following commit(s) were added to refs/heads/master by this push:
     new 3cbd3bf92c7 net/local: Give the whole address of a datagram sender.
3cbd3bf92c7 is described below

commit 3cbd3bf92c78041794a9d0ba493fd48ce570f7d4
Author: Marco Casaroli <[email protected]>
AuthorDate: Fri Nov 6 11:30:49 2026 +0100

    net/local: Give the whole address of a datagram sender.
    
    recvmsg() of a local datagram socket set msg_namelen to the length of the 
name
    without the family, so it was two bytes short.  A kernel build copies only
    that length back to the program, so the program lost the last two bytes of 
the
    name: a server could not answer the client.  Also, the name and its NUL 
could
    go two bytes past the buffer of the caller, because the limit did not count
    the family.
    
    Count the family and the NUL in both places.  The address of an unbound
    sender has the family only.  The behavior is the same in the flat, protected
    and kernel builds, apart from the two bytes that a kernel build lost.
    
    Assisted-by: Claude Code:claude-opus-5-5
    Signed-off-by: Marco Casaroli <[email protected]>
---
 net/local/local_recvmsg.c | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)

diff --git a/net/local/local_recvmsg.c b/net/local/local_recvmsg.c
index c8c51f6a7c7..686270fc10e 100644
--- a/net/local/local_recvmsg.c
+++ b/net/local/local_recvmsg.c
@@ -560,9 +560,13 @@ psock_dgram_recvfrom(FAR struct socket *psock, FAR void 
*buf, size_t len,
   readlen = addrlen;
   offset += sizeof(pktlen);
 
-  if (from && fromlen && *fromlen)
+  /* The address has the family, the name and a NUL.  The address of an
+   * unbound sender has the family only.
+   */
+
+  if (from && fromlen && *fromlen > sizeof(sa_family_t))
     {
-      pathlen = MIN(*fromlen - 1, readlen);
+      pathlen = MIN(*fromlen - sizeof(sa_family_t) - 1, readlen);
       ret = psock_fifo_read(psock, from->sa_data, offset,
                             &pathlen, flags, false);
       if (ret < 0)
@@ -573,7 +577,12 @@ psock_dgram_recvfrom(FAR struct socket *psock, FAR void 
*buf, size_t len,
 
       from->sa_family = AF_LOCAL;
       from->sa_data[pathlen] = '\0';
-      *fromlen = pathlen;
+      *fromlen = sizeof(sa_family_t);
+      if (addrlen > 0)
+        {
+          *fromlen += pathlen + 1;
+        }
+
       readlen -= pathlen;
     }
 

Reply via email to