[PATCH rtems-docs v2] c-user/chains: Correct iteration example code

Kinsey Moore kinsey.moore at oarcorp.com
Thu Mar 9 02:33:02 UTC 2023


Casting the node returned by rtems_chain_head is incorrect. That node is
owned by the control structure and use of it post-cast could cause
memory corruption.
---
 c-user/chains.rst | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/c-user/chains.rst b/c-user/chains.rst
index f518ef4..2044966 100644
--- a/c-user/chains.rst
+++ b/c-user/chains.rst
@@ -193,10 +193,14 @@ placed on another chain:
         rtems_chain_initialize_empty (out);
 
         node = rtems_chain_head (chain);
+
+        /* The node returned by rtems_chain_head() is owned by the chain */
+        node = rtems_chain_next(node);
+
         while (!rtems_chain_is_tail (chain, node))
         {
             bar = (foo*) node;
-            rtems_chain_node* next_node = node->next;
+            rtems_chain_node* next_node = rtems_chain_next(node);
             if (strcmp (match, bar->data) == 0)
             {
                 rtems_chain_extract (node);
-- 
2.30.2



More information about the devel mailing list