diff mbox series

[ovs-dev,09/16] cmap: use multi-variable iterators

Message ID 20220214094003.3844268-10-amorenoz@redhat.com
State Superseded
Headers show
Series Fix undefined behavior in loop macros | expand

Checks

Context Check Description
ovsrobot/apply-robot warning apply and check: warning
ovsrobot/github-robot-_Build_and_Test fail github build: failed

Commit Message

Adrian Moreno Feb. 14, 2022, 9:39 a.m. UTC
Re-write cmap's loops using multi-variable helpers.

For iterators based on cmap_cursor, we just need to make sure the NODE
variable is not used after the loop, so we set it to NULL.

Signed-off-by: Adrian Moreno <amorenoz@redhat.com>
---
 lib/cmap.h        | 24 ++++++++++++++----------
 tests/test-cmap.c |  3 +++
 2 files changed, 17 insertions(+), 10 deletions(-)

Comments

0-day Robot Feb. 14, 2022, 11:14 a.m. UTC | #1
Bleep bloop.  Greetings Adrian Moreno, I am a robot and I have tried out your patch.
Thanks for your contribution.

I encountered some error that I wasn't expecting.  See the details below.


checkpatch:
ERROR: Use ovs_assert() in place of assert()
#74 FILE: tests/test-cmap.c:77:
    assert(e == NULL);

ERROR: Use ovs_assert() in place of assert()
#82 FILE: tests/test-cmap.c:111:
        assert(e == NULL);

ERROR: Use ovs_assert() in place of assert()
#90 FILE: tests/test-cmap.c:135:
            assert(e == NULL);

Lines checked: 97, Warnings: 0, Errors: 3


Please check this out.  If you feel there has been an error, please email aconole@redhat.com

Thanks,
0-day Robot
diff mbox series

Patch

diff --git a/lib/cmap.h b/lib/cmap.h
index c502d2311..8c06d7123 100644
--- a/lib/cmap.h
+++ b/lib/cmap.h
@@ -108,6 +108,8 @@  size_t cmap_replace(struct cmap *, struct cmap_node *old_node,
  *
  * CMAP and HASH are evaluated only once.  NODE is evaluated many times.
  *
+ * After a normal exit of the loop (not through a "break;" statement) NODE is
+ * NULL.
  *
  * Thread-safety
  * =============
@@ -128,15 +130,17 @@  size_t cmap_replace(struct cmap *, struct cmap_node *old_node,
  * CMAP_FOR_EACH_WITH_HASH_PROTECTED may only be used if CMAP is guaranteed not
  * to change during iteration.  It may be very slightly faster.
  */
-#define CMAP_NODE_FOR_EACH(NODE, MEMBER, CMAP_NODE)                     \
-    for (INIT_CONTAINER(NODE, CMAP_NODE, MEMBER);                       \
-         (NODE) != OBJECT_CONTAINING(NULL, NODE, MEMBER);               \
-         ASSIGN_CONTAINER(NODE, cmap_node_next(&(NODE)->MEMBER), MEMBER))
-#define CMAP_NODE_FOR_EACH_PROTECTED(NODE, MEMBER, CMAP_NODE)           \
-    for (INIT_CONTAINER(NODE, CMAP_NODE, MEMBER);                       \
-         (NODE) != OBJECT_CONTAINING(NULL, NODE, MEMBER);               \
-         ASSIGN_CONTAINER(NODE, cmap_node_next_protected(&(NODE)->MEMBER), \
-                          MEMBER))
+#define CMAP_NODE_FOR_EACH(NODE, MEMBER, CMAP_NODE)                        \
+    for (INIT_MULTIVAR(NODE, MEMBER, CMAP_NODE);                           \
+         CONDITION_MULTIVAR(NODE, MEMBER, ITER_VAR(NODE) != NULL);         \
+         UPDATE_MULTIVAR(NODE,                                             \
+                         ITER_VAR(NODE) = cmap_node_next(ITER_VAR(NODE))))
+#define CMAP_NODE_FOR_EACH_PROTECTED(NODE, MEMBER, CMAP_NODE)              \
+    for (INIT_MULTIVAR(NODE, MEMBER, CMAP_NODE);                           \
+         CONDITION_MULTIVAR(NODE, MEMBER, ITER_VAR(NODE) != NULL);         \
+         UPDATE_MULTIVAR(NODE,                                             \
+            ITER_VAR(NODE) = cmap_node_next_protected(ITER_VAR(NODE))))
+
 #define CMAP_FOR_EACH_WITH_HASH(NODE, MEMBER, HASH, CMAP)   \
     CMAP_NODE_FOR_EACH(NODE, MEMBER, cmap_find(CMAP, HASH))
 #define CMAP_FOR_EACH_WITH_HASH_PROTECTED(NODE, MEMBER, HASH, CMAP)     \
@@ -223,7 +227,7 @@  unsigned long cmap_find_batch(const struct cmap *cmap, unsigned long map,
      ? (INIT_CONTAINER(NODE, (CURSOR)->node, MEMBER),   \
         cmap_cursor_advance(CURSOR),                    \
         true)                                           \
-     : false)
+     : (NODE = NULL, false))
 
 #define CMAP_CURSOR_FOR_EACH(NODE, MEMBER, CURSOR, CMAP)    \
     for (*(CURSOR) = cmap_cursor_start(CMAP);               \
diff --git a/tests/test-cmap.c b/tests/test-cmap.c
index bc1f45642..588a5dea6 100644
--- a/tests/test-cmap.c
+++ b/tests/test-cmap.c
@@ -74,6 +74,7 @@  check_cmap(struct cmap *cmap, const int values[], size_t n,
         cmap_values[i++] = e->value;
     }
     assert(i == n);
+    assert(e == NULL);
 
     /* Here we test iteration with cmap_next_position() */
     i = 0;
@@ -107,6 +108,7 @@  check_cmap(struct cmap *cmap, const int values[], size_t n,
             count += e->value == values[i];
         }
         assert(count == 1);
+        assert(e == NULL);
     }
 
     /* Check that all the values are there in batched lookup. */
@@ -130,6 +132,7 @@  check_cmap(struct cmap *cmap, const int values[], size_t n,
             CMAP_NODE_FOR_EACH (e, node, nodes[k]) {
                 count += e->value == values[i + k];
             }
+            assert(e == NULL);
         }
         assert(count == j); /* j elements in a batch. */
     }