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 302c358ef38bfbf4dc33196930e2449365f91f9d
Author: zhanghongyu <[email protected]>
AuthorDate: Wed Dec 31 17:37:12 2025 +0800

    net/pkt: add recv size limit when NET_RECV_BUFSIZE > 0
    
    Avoid the iob from being fully occupied
    
    Signed-off-by: zhanghongyu <[email protected]>
---
 net/pkt/pkt.h        | 3 +++
 net/pkt/pkt_input.c  | 9 +++++++++
 net/pkt/pkt_sockif.c | 3 +++
 3 files changed, 15 insertions(+)

diff --git a/net/pkt/pkt.h b/net/pkt/pkt.h
index 9c7ad1b3591..29db4661f93 100644
--- a/net/pkt/pkt.h
+++ b/net/pkt/pkt.h
@@ -76,6 +76,9 @@ struct pkt_conn_s
   uint8_t    crefs;    /* Reference counts on this instance */
   uint16_t   type;     /* The Ethernet type of the packet */
 
+#if CONFIG_NET_RECV_BUFSIZE > 0
+  int32_t    rcvbufs;  /* Maximum amount of bytes queued in recv */
+#endif
 #ifdef CONFIG_NET_PKT_WRITE_BUFFERS
   /* Write buffering
    *
diff --git a/net/pkt/pkt_input.c b/net/pkt/pkt_input.c
index 638befdbee1..e64550900e9 100644
--- a/net/pkt/pkt_input.c
+++ b/net/pkt/pkt_input.c
@@ -215,6 +215,15 @@ static int pkt_in(FAR struct net_driver_s *dev)
         {
           /* Add the PKT to the socket read-ahead buffer. */
 
+#if CONFIG_NET_RECV_BUFSIZE > 0
+          if ((iob_get_queue_size(&conn->readahead) + dev->d_len) >
+              conn->rcvbufs)
+            {
+              nwarn("WARNING: Full read-ahead buffer, dropping packet\n");
+              continue;
+            }
+#endif
+
           if (pkt_datahandler(dev, conn, &conn->readahead) == 0)
             {
               /* No.. the packet was not processed now.  Return -EAGAIN so
diff --git a/net/pkt/pkt_sockif.c b/net/pkt/pkt_sockif.c
index 8c582a41f23..824ba194f3a 100644
--- a/net/pkt/pkt_sockif.c
+++ b/net/pkt/pkt_sockif.c
@@ -128,6 +128,9 @@ static int pkt_sockif_alloc(FAR struct socket *psock)
 
   conn->type = psock->s_proto;
 
+#if CONFIG_NET_RECV_BUFSIZE > 0
+  conn->rcvbufs = CONFIG_NET_RECV_BUFSIZE;
+#endif
 #ifdef CONFIG_NET_PKT_WRITE_BUFFERS
 #  if CONFIG_NET_SEND_BUFSIZE > 0
   conn->sndbufs = CONFIG_NET_SEND_BUFSIZE;

Reply via email to