diff mbox series

[ovs-dev,v5,15/15] sset: add SHORT version of SAFE loop macros

Message ID 20220323115624.1469085-16-amorenoz@redhat.com
State Accepted
Commit b16270e69b9c72284fe6f0ab186ce17e41d7af7a
Headers show
Series Fix undefined behavior in loop macros | expand

Checks

Context Check Description
ovsrobot/apply-robot success apply and check: success
ovsrobot/github-robot-_Build_and_Test success github build: passed
ovsrobot/intel-ovs-compilation success test: success

Commit Message

Adrian Moreno March 23, 2022, 11:56 a.m. UTC
Add SHORT version of SAFE loop macros and overload the current macro
name to keep backwards compatibility.

Acked-by: Dumitru Ceara <dceara@redhat.com>
Acked-by: Eelco Chaudron <echaudro@redhat.com>
Signed-off-by: Adrian Moreno <amorenoz@redhat.com>
---
 lib/sset.c |  8 ++++----
 lib/sset.h | 15 ++++++++++++++-
 2 files changed, 18 insertions(+), 5 deletions(-)
diff mbox series

Patch

diff --git a/lib/sset.c b/lib/sset.c
index b2e3f43ec..c3197e305 100644
--- a/lib/sset.c
+++ b/lib/sset.c
@@ -212,9 +212,9 @@  sset_add_array(struct sset *set, char **names, size_t n)
 void
 sset_clear(struct sset *set)
 {
-    const char *name, *next;
+    const char *name;
 
-    SSET_FOR_EACH_SAFE (name, next, set) {
+    SSET_FOR_EACH_SAFE (name, set) {
         sset_delete(set, SSET_NODE_FROM_NAME(name));
     }
 }
@@ -320,9 +320,9 @@  sset_at_position(const struct sset *set, struct sset_position *pos)
 void
 sset_intersect(struct sset *a, const struct sset *b)
 {
-    const char *name, *next;
+    const char *name;
 
-    SSET_FOR_EACH_SAFE (name, next, a) {
+    SSET_FOR_EACH_SAFE (name, a) {
         if (!sset_contains(b, name)) {
             sset_delete(a, SSET_NODE_FROM_NAME(name));
         }
diff --git a/lib/sset.h b/lib/sset.h
index f0bb8b534..214d6fb41 100644
--- a/lib/sset.h
+++ b/lib/sset.h
@@ -87,13 +87,26 @@  void sset_intersect(struct sset *, const struct sset *);
          NAME != NULL;                          \
          (NAME) = SSET_NEXT(SSET, NAME))
 
-#define SSET_FOR_EACH_SAFE(NAME, NEXT, SSET)        \
+#define SSET_FOR_EACH_SAFE_LONG(NAME, NEXT, SSET)   \
     for ((NAME) = SSET_FIRST(SSET);                 \
          (NAME != NULL                              \
           ? (NEXT) = SSET_NEXT(SSET, NAME), true    \
           : false);                                 \
          (NAME) = (NEXT))
 
+#define SSET_FOR_EACH_SAFE_SHORT(NAME, SSET)           \
+    for (const char * NAME__next =                     \
+         ((NAME) = SSET_FIRST(SSET), NULL);            \
+         (NAME != NULL                                 \
+          ? (NAME__next = SSET_NEXT(SSET, NAME), true) \
+          : (NAME__next = NULL, false));               \
+         (NAME) = NAME__next)
+
+#define SSET_FOR_EACH_SAFE(...)                        \
+    OVERLOAD_SAFE_MACRO(SSET_FOR_EACH_SAFE_LONG,       \
+                        SSET_FOR_EACH_SAFE_SHORT,      \
+                        3, __VA_ARGS__)
+
 const char **sset_array(const struct sset *);
 const char **sset_sort(const struct sset *);