From 7ca6eafaa28848cb917a2cd4af78a9c85c3fd51a Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Thu, 9 Mar 2023 13:14:12 +0900 Subject: [PATCH] macro: support the case that the number of elements has const qualifier Follow-up for 5716c27e1f52d2aba9dd02916c01d6271d9d0b16. Addresses https://github.com/systemd/systemd/pull/26303#issuecomment-1460712007. (cherry picked from commit b9872fe1ddb61a7f930c652887c04e2f82b43be4) Related: #2190226 --- src/basic/macro.h | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/basic/macro.h b/src/basic/macro.h index b977730e54..72a2c7267e 100644 --- a/src/basic/macro.h +++ b/src/basic/macro.h @@ -329,12 +329,14 @@ static inline int __coverity_check_and_return__(int condition) { #endif #endif -#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) { \