[PATCH] basic/macro: add macro to iterate variadic args
authorDan Streetman <ddstreet@ieee.org>
Thu, 2 Feb 2023 20:58:10 +0000 (15:58 -0500)
committerCarlos Henrique Lima Melara <charlesmelara@riseup.net>
Thu, 26 Jun 2025 00:44:53 +0000 (21:44 -0300)
(cherry picked from commit e179f2d89c9f0c951636d74de00136b4075cd1ac)
(cherry picked from commit cd4f43bf378ff33ce5cfeacd96f7f3726603bddc)

Origin: upstream, https://github.com/systemd/systemd-stable/commit/c288a3aafdf11cd93eb7a21e4d587c6fc218a29c
Forwarded: not-needed
Last-Update: 2025-06-23

Gbp-Pq: Name CVE-2025-4598-1.patch

src/basic/macro.h

index 278255375617df9b4f56f4e79b29299da555bedb..ee83541938cbaafd755dd265812bb8329825bc66 100644 (file)
@@ -654,4 +654,13 @@ static inline size_t size_add(size_t x, size_t y) {
         return y >= SIZE_MAX - x ? SIZE_MAX : x + y;
 }
 
+/* Iterate through each variadic arg. All must be the same type as 'entry' or must be implicitly
+ * convertable. The iteration variable 'entry' must already be defined. */
+#define VA_ARGS_FOREACH(entry, ...)                                     \
+        _VA_ARGS_FOREACH(entry, UNIQ_T(_entries_, UNIQ), UNIQ_T(_current_, UNIQ), ##__VA_ARGS__)
+#define _VA_ARGS_FOREACH(entry, _entries_, _current_, ...)         \
+        for (typeof(entry) _entries_[] = { __VA_ARGS__ }, *_current_ = _entries_; \
+             ((long)(_current_ - _entries_) < (long)ELEMENTSOF(_entries_)) && ({ entry = *_current_; true; }); \
+             _current_++)
+
 #include "log.h"