Skip to content

Commit

Permalink
libmetal: add metal_list_for_each_safe() support
Browse files Browse the repository at this point in the history
Add a more secure way to traverse linked lists

Signed-off-by: Guiding Li <liguiding1@xiaomi.com>
  • Loading branch information
GUIDINGLI authored and yintao committed Oct 16, 2023
1 parent 549f13b commit 68cab27
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions lib/list.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,29 @@ static inline struct metal_list *metal_list_first(struct metal_list *list)
return metal_list_is_empty(list) ? NULL : list->next;
}

/**
* @brief Used for iterating over a list
*
* @param list Pointer to the head node of the list
* @param node Pointer to each node in the list during iteration
*/
#define metal_list_for_each(list, node) \
for ((node) = (list)->next; \
(node) != (list); \
(node) = (node)->next)

/**
* @brief Used for iterating over a list safely
*
* @param list Pointer to the head node of the list
* @param temp Pointer to the next node's address during iteration
* @param node Pointer to each node in the list during iteration
*/
#define metal_list_for_each_safe(list, temp, node) \
for ((node) = (list)->next, (temp) = (node)->next; \
(node) != (list); \
(node) = (temp), (temp) = (node)->next)

static inline bool metal_list_find_node(struct metal_list *list,
struct metal_list *node)
{
Expand Down

0 comments on commit 68cab27

Please sign in to comment.