diff mbox series

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

Message ID 20220214094003.3844268-17-amorenoz@redhat.com
State Superseded
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

Commit Message

Adrian Moreno Feb. 14, 2022, 9:40 a.m. UTC
Add SHORT version of SAFE loop macros and overload the current macro
name to keep backwards compatibility.

Signed-off-by: Adrian Moreno <amorenoz@redhat.com>
---
 lib/sset.c |  8 ++++----
 lib/sset.h | 16 +++++++++++++++-
 2 files changed, 19 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..3a028c012 100644
--- a/lib/sset.h
+++ b/lib/sset.h
@@ -87,13 +87,27 @@  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 (OVS_TYPEOF(NAME) 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_GET_SAFE_MACRO(_1, _2, _3, NAME, ...) NAME
+#define SSET_FOR_EACH_SAFE(...)                              \
+    SSET_GET_SAFE_MACRO(__VA_ARGS__,                         \
+                   SSET_FOR_EACH_SAFE_LONG,                  \
+                   SSET_FOR_EACH_SAFE_SHORT)(__VA_ARGS__)
+
 const char **sset_array(const struct sset *);
 const char **sset_sort(const struct sset *);