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

Joel Sherrill joel at rtems.org
Thu Mar 9 15:01:25 UTC 2023


Module:    rtems-docs
Branch:    master
Commit:    fd6d862033961da3c8d9190033121472b7692297
Changeset: http://git.rtems.org/rtems-docs/commit/?id=fd6d862033961da3c8d9190033121472b7692297

Author:    Kinsey Moore <kinsey.moore at oarcorp.com>
Date:      Wed Mar  8 14:39:25 2023 -0600

c-user/chains: Correct iteration example code

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. Instead, use rtems_chain_first which returns the
node after the head node. This also corrects node->next to
rtems_chain_next(node) which makes better use of the API.

---

 c-user/chains.rst | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/c-user/chains.rst b/c-user/chains.rst
index f518ef4..ca80b4b 100644
--- a/c-user/chains.rst
+++ b/c-user/chains.rst
@@ -192,11 +192,12 @@ placed on another chain:
 
         rtems_chain_initialize_empty (out);
 
-        node = rtems_chain_head (chain);
+        node = rtems_chain_first (chain);
+
         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);



More information about the vc mailing list