From 765c034845f1ac578ed894814c3e4dc3c30189cf Mon Sep 17 00:00:00 2001 From: "Enrico Weigelt, metux IT consult" Date: Tue, 27 May 2025 17:20:52 +0200 Subject: [PATCH] include: list.h: allow zero-initialization of struct xorg_list Up until now, xorg_list's always need to be explicitly initialized by calling xorg_list_init(), otherwise next access attempt will segfault. This is adding extra complexity and risk of subtle bugs. Adding some extra NULL check are trivial and their cost is so tiny that it's even hard to practically measure. Signed-off-by: Enrico Weigelt, metux IT consult --- include/list.h | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/include/list.h b/include/list.h index 9df812075..90efd8af5 100644 --- a/include/list.h +++ b/include/list.h @@ -130,6 +130,13 @@ xorg_list_init(struct xorg_list *list) list->next = list->prev = list; } +static inline void +__xorg_list_autoinit(struct xorg_list *head) +{ + if ((!head->prev) && (!head->next)) + xorg_list_init(head); +} + static inline void __xorg_list_add(struct xorg_list *entry, struct xorg_list *prev, struct xorg_list *next) @@ -158,6 +165,7 @@ __xorg_list_add(struct xorg_list *entry, static inline void xorg_list_add(struct xorg_list *entry, struct xorg_list *head) { + __xorg_list_autoinit(head); __xorg_list_add(entry, head, head->next); } @@ -179,6 +187,7 @@ xorg_list_add(struct xorg_list *entry, struct xorg_list *head) static inline void xorg_list_append(struct xorg_list *entry, struct xorg_list *head) { + __xorg_list_autoinit(head); __xorg_list_add(entry, head->prev, head); } @@ -222,7 +231,7 @@ xorg_list_del(struct xorg_list *entry) static inline int xorg_list_is_empty(struct xorg_list *head) { - return head->next == head; + return ((head->next == NULL) || (head->next == head)); } /** @@ -302,7 +311,7 @@ xorg_list_is_empty(struct xorg_list *head) #define xorg_list_for_each_entry(pos, head, member) \ for (pos = NULL, \ pos = __container_of((head)->next, pos, member); \ - &pos->member != (head); \ + (((head)->next != NULL) && &pos->member != (head)); \ pos = __container_of(pos->member.next, pos, member)) /** @@ -316,7 +325,7 @@ xorg_list_is_empty(struct xorg_list *head) for (pos = NULL, \ pos = __container_of((head)->next, pos, member), \ tmp = __container_of(pos->member.next, pos, member); \ - &pos->member != (head); \ + (((head)->next != NULL) && (&pos->member != (head))); \ pos = tmp, tmp = __container_of(pos->member.next, tmp, member)) /* NULL-Terminated List Interface