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 69c67a584937ec621ef1f540787b26b8d597736c
Author: zhanghongyu <[email protected]>
AuthorDate: Mon Jun 29 20:19:49 2026 +0800

    mm/iob: fix stale qh_tail corrupting enqueued multi-IOB chains.
    
    iob_add_queue/iob_concat_queue linked the next chain directly onto
    iobq->qh_tail. But a chain may be extended (e.g. via iob_copyin) after
    it was enqueued: the IOB_CHAIN_END flag moves to the new last IOB while
    qh_tail still points at the original, now mid-chain, IOB. Linking onto
    that stale tail overwrites the chain's internal io_flink, truncating the
    chain and planting a false chain boundary.
    
    Advance from qh_tail to the real IOB_CHAIN_END before linking. In the
    normal (non-extended) case the flag is already on qh_tail, so the loop
    adds no cost.
    
    Signed-off-by: zhanghongyu <[email protected]>
---
 mm/iob/iob_add_queue.c | 38 ++++++++++++++++++++++++++++++++------
 1 file changed, 32 insertions(+), 6 deletions(-)

diff --git a/mm/iob/iob_add_queue.c b/mm/iob/iob_add_queue.c
index 4b2231e1249..44c8cdda160 100644
--- a/mm/iob/iob_add_queue.c
+++ b/mm/iob/iob_add_queue.c
@@ -73,10 +73,25 @@ int iob_add_queue(FAR struct iob_s *iob, FAR struct 
iob_queue_s *iobq)
     }
   else
     {
-      /* Link after current tail, setting the chain end flag */
+      /* Link after current tail, setting the chain end flag.
+       *
+       * The chain that is currently at the tail of the queue may have been
+       * extended (e.g. via iob_copyin()) after it was enqueued.  In that
+       * case the IOB_CHAIN_END flag has been carried forward to the new
+       * last IOB while qh_tail still points at the original (now mid-chain)
+       * IOB.  Advance to the real end of the tail chain so that we do not
+       * overwrite its io_flink and corrupt/truncate the chain.
+       */
+
+      FAR struct iob_s *tail = iobq->qh_tail;
+
+      DEBUGASSERT(tail != NULL);
+      while (!IOB_FLAG_TEST(tail, IOB_CHAIN_END))
+        {
+          tail = IOB_LINK_GET(tail);
+        }
 
-      DEBUGASSERT(iobq->qh_tail != NULL);
-      IOB_FLAG_SET(iobq->qh_tail, iob, IOB_CHAIN_END);
+      IOB_FLAG_SET(tail, iob, IOB_CHAIN_END);
       iobq->qh_tail = last;
     }
 
@@ -131,10 +146,21 @@ int iob_concat_queue(FAR struct iob_queue_s *dest,
         }
       else
         {
-          /* Link source to destination tail, preserving CHAIN_END flag */
+          /* Link source to destination tail, preserving CHAIN_END flag.
+           * The tail chain may have been extended after being enqueued, so
+           * advance to its real end (marked by IOB_CHAIN_END) before
+           * linking to avoid corrupting/truncating that chain.
+           */
+
+          FAR struct iob_s *dtail = dest->qh_tail;
+
+          DEBUGASSERT(dtail != NULL);
+          while (!IOB_FLAG_TEST(dtail, IOB_CHAIN_END))
+            {
+              dtail = IOB_LINK_GET(dtail);
+            }
 
-          DEBUGASSERT(dest->qh_tail != NULL);
-          IOB_FLAG_SET(dest->qh_tail, iob, IOB_CHAIN_END);
+          IOB_FLAG_SET(dtail, iob, IOB_CHAIN_END);
           dest->qh_tail = tail;
         }
     }

Reply via email to