From 4bc924c9b543608df05e5768640cce44d62ee109 Mon Sep 17 00:00:00 2001 From: Yunmi Ha Date: Tue, 2 Jun 2020 13:57:10 +0900 Subject: [PATCH] Add DD_LIST_FOREACH_SAFE function DD_LIST_FOREACH_SAFE function allows you to safely delete a list item in a foreach statement. Change-Id: Ic6bc0bf11e62161f9ff2769a66439197d6b81b39 Signed-off-by: Yunmi Ha --- src/common.h | 4 ++++ src/dbus.c | 4 ++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/common.h b/src/common.h index 650e27a..0a206d5 100644 --- a/src/common.h +++ b/src/common.h @@ -52,6 +52,10 @@ typedef GList dd_list; a = g_list_remove(a, b) #define DD_LIST_FOREACH(head, elem, node) \ for (elem = head; elem && ((node = elem->data) != NULL); elem = elem->next, node = NULL) +#define DD_LIST_FOREACH_SAFE(head, elem, elem_next, node) \ + for (elem = head, elem_next=g_list_next(elem), node = NULL; \ + elem && ((node = elem->data) != NULL); \ + elem = elem_next, elem_next = g_list_next(elem), node=NULL) #endif #define FEEDBACK_DATA_DIR FEEDBACK_SYS_SHARE"/feedback" diff --git a/src/dbus.c b/src/dbus.c index ff20c1b..d315962 100644 --- a/src/dbus.c +++ b/src/dbus.c @@ -432,7 +432,7 @@ int unregister_signal_handler(feedback_restart_cb func) { GDBusConnection *conn; struct feedback_restart_callback *callback; - dd_list *elem; + dd_list *elem, *next; if (!func) return -EINVAL; @@ -445,7 +445,7 @@ int unregister_signal_handler(feedback_restart_cb func) //LCOV_EXCL_STOP } - DD_LIST_FOREACH(callback_list, elem, callback) { + DD_LIST_FOREACH_SAFE(callback_list, elem, next, callback) { if (callback->func != func) continue; if (callback->feedback_id > 0) -- 2.7.4