From 55a072cb162766da1c489a2bffddb1d1d0d32b91 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcin=20=C5=9Alusarz?= Date: Tue, 15 Dec 2020 15:56:02 +0100 Subject: [PATCH] util/list: add list_is_linked MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit v2: verify node is valid in list_is_linked (Timothy) Signed-off-by: Marcin Ślusarz Reviewed-by: Timothy Arceri Reviewed-by: Marek Olšák Part-of: --- src/util/list.h | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/util/list.h b/src/util/list.h index fdc6fafb1a1..9de8a2842bc 100644 --- a/src/util/list.h +++ b/src/util/list.h @@ -111,12 +111,20 @@ static inline bool list_is_empty(const struct list_head *list) return list->next == list; } +static inline bool list_is_linked(const struct list_head *list) +{ + /* both must be NULL or both must be not NULL */ + assert((list->prev != NULL) == (list->next != NULL)); + + return list->next != NULL; +} + /** * Returns whether the list has exactly one element. */ static inline bool list_is_singular(const struct list_head *list) { - return list->next != NULL && list->next != list && list->next->next == list; + return list_is_linked(list) && list->next != list && list->next->next == list; } static inline unsigned list_length(const struct list_head *list) @@ -153,6 +161,7 @@ static inline void list_splicetail(struct list_head *src, struct list_head *dst) static inline void list_validate(const struct list_head *list) { struct list_head *node; + assert(list_is_linked(list)); assert(list->next->prev == list && list->prev->next == list); for (node = list->next; node != list; node = node->next) assert(node->next->prev == node && node->prev->next == node);