From 5d79da6387bda512fbf9b9e5186e8782bafc2eaa Mon Sep 17 00:00:00 2001 From: Yuxuan Shui Date: Sun, 17 Jul 2022 18:08:23 +0100 Subject: [PATCH] c2: return if foreach function returned early Useful for error handling. Signed-off-by: Yuxuan Shui --- src/c2.c | 7 +++++-- src/c2.h | 2 +- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/c2.c b/src/c2.c index 287a66c..24e62f0 100644 --- a/src/c2.c +++ b/src/c2.c @@ -1678,12 +1678,15 @@ bool c2_match(session_t *ps, const struct managed_win *w, const c2_lptr_t *condl /// Iterate over all conditions in a condition linked list. Call the callback for each of /// the conditions. If the callback returns true, the iteration stops early. -void c2_list_foreach(const c2_lptr_t *condlist, c2_list_foreach_cb_t cb, void *data) { +/// +/// Returns whether the iteration was stopped early. +bool c2_list_foreach(const c2_lptr_t *condlist, c2_list_foreach_cb_t cb, void *data) { for (auto i = condlist; i; condlist = condlist->next) { if (cb(i, data)) { - break; + return true; } } + return false; } /// Return user data stored in a condition. diff --git a/src/c2.h b/src/c2.h index 7a9c354..1f3390b 100644 --- a/src/c2.h +++ b/src/c2.h @@ -27,7 +27,7 @@ bool c2_match(session_t *ps, const struct managed_win *w, const c2_lptr_t *condl bool c2_list_postprocess(session_t *ps, c2_lptr_t *list); typedef bool (*c2_list_foreach_cb_t)(const c2_lptr_t *cond, void *data); -void c2_list_foreach(const c2_lptr_t *list, c2_list_foreach_cb_t cb, void *data); +bool c2_list_foreach(const c2_lptr_t *list, c2_list_foreach_cb_t cb, void *data); /// Return user data stored in a condition. void *c2_list_get_data(const c2_lptr_t *condlist);