]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
macro: support the case that the number of elements has const qualifier
authorYu Watanabe <watanabe.yu+github@gmail.com>
Thu, 9 Mar 2023 04:14:12 +0000 (13:14 +0900)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Thu, 9 Mar 2023 07:41:43 +0000 (08:41 +0100)
Follow-up for 5716c27e1f52d2aba9dd02916c01d6271d9d0b16.

Addresses https://github.com/systemd/systemd/pull/26303#issuecomment-1460712007.

src/basic/macro.h

index 3de319d880f95a8c3e1b367b8abbcae4d908ee18..63344f8d97362e10cade4fb04be1741b527afb29 100644 (file)
@@ -297,12 +297,14 @@ static inline int __coverity_check_and_return__(int condition) {
              p != (typeof(p)) POINTER_MAX;                                               \
              p = *(++_l))
 
-#define _FOREACH_ARRAY(i, array, num, m, s)                             \
-        for (typeof(num) m = (num); m > 0; m = 0)                       \
-                for (typeof(array[0]) *s = (array), *i = s; s && i < s + m; i++)
+#define _FOREACH_ARRAY(i, array, num, m, end)                           \
+        for (typeof(array[0]) *i = (array), *end = ({                   \
+                                typeof(num) m = (num);                  \
+                                (i && m > 0) ? i + m : NULL;            \
+                        }); end && i < end; i++)
 
 #define FOREACH_ARRAY(i, array, num)                                    \
-        _FOREACH_ARRAY(i, array, num, UNIQ_T(m, UNIQ), UNIQ_T(s, UNIQ))
+        _FOREACH_ARRAY(i, array, num, UNIQ_T(m, UNIQ), UNIQ_T(end, UNIQ))
 
 #define DEFINE_TRIVIAL_DESTRUCTOR(name, type, func)             \
         static inline void name(type *p) {                      \