summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ccan/foreach/foreach.h6
-rw-r--r--ccan/foreach/test/compile_ok-const.c14
-rw-r--r--ccan/foreach/test/compile_ok-nonconst.c14
3 files changed, 31 insertions, 3 deletions
diff --git a/ccan/foreach/foreach.h b/ccan/foreach/foreach.h
index 73c4c693..8e08548f 100644
--- a/ccan/foreach/foreach.h
+++ b/ccan/foreach/foreach.h
@@ -44,7 +44,7 @@
*/
#define foreach_ptr(i, ...) \
for (unsigned _foreach_i \
- = (((i) = ((const void *[]){ __VA_ARGS__ })[0]), 0); \
+ = (((i) = (void *)((FOREACH_TYPEOF(i)[]){ __VA_ARGS__ })[0]), 0); \
(i); \
(i) = (void *)((FOREACH_TYPEOF(i)[]) \
{ __VA_ARGS__, NULL})[++_foreach_i], \
@@ -59,13 +59,13 @@
(i) = (int[]) { __VA_ARGS__, 0 }[_foreach_iter_inc(&(i))])
#define foreach_ptr(i, ...) \
- for ((i) = ((FOREACH_TYPEOF(i)[]){ __VA_ARGS__ })[0], \
+ for ((i) = (void *)((FOREACH_TYPEOF(i)[]){ __VA_ARGS__ })[0], \
_foreach_iter_init(&(i)); \
(i); \
(i) = (void *)((FOREACH_TYPEOF(i)[]){ __VA_ARGS__, NULL }) \
[_foreach_iter_inc(&(i))], \
_foreach_no_nullval(_foreach_iter(&(i)), i, \
- ((void *[]){ __VA_ARGS__})))
+ ((const void *[]){ __VA_ARGS__})))
void _foreach_iter_init(const void *i);
unsigned int _foreach_iter(const void *i);
diff --git a/ccan/foreach/test/compile_ok-const.c b/ccan/foreach/test/compile_ok-const.c
new file mode 100644
index 00000000..d1c858a0
--- /dev/null
+++ b/ccan/foreach/test/compile_ok-const.c
@@ -0,0 +1,14 @@
+#include <ccan/foreach/foreach.h>
+#include <ccan/foreach/foreach.c>
+
+/* Iterating over const pointers should work fine. */
+int main(int argc, char *argv[])
+{
+ const char *s1 = "hello", *s2 = "world", *p;
+ unsigned int i = 0;
+
+ foreach_ptr(p, s1, s2)
+ i++;
+
+ return i == 2 ? 0 : 1;
+}
diff --git a/ccan/foreach/test/compile_ok-nonconst.c b/ccan/foreach/test/compile_ok-nonconst.c
new file mode 100644
index 00000000..e533a9d3
--- /dev/null
+++ b/ccan/foreach/test/compile_ok-nonconst.c
@@ -0,0 +1,14 @@
+#include <ccan/foreach/foreach.h>
+#include <ccan/foreach/foreach.c>
+
+/* Iterating const over non-const pointers should work fine. */
+int main(int argc, char *argv[])
+{
+ char *p;
+ unsigned int i = 0;
+
+ foreach_ptr(p, argv[0], argv[1])
+ i++;
+
+ return i == 2 ? 0 : 1;
+}