From 6fa2e48db9638800d27b220f40596df9ddb5b29d Mon Sep 17 00:00:00 2001 From: Elliott Hughes Date: Tue, 2 Nov 2021 18:33:09 -0700 Subject: [PATCH] cutils: only support safe list iteration. We've had two use-after-frees in the last month from this nonsense... Bug: http://b/204925347 Test: treehugger Change-Id: I5e1485253224e38ca51a7a077dbe65d19e39f817 --- libcutils/include/cutils/list.h | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/libcutils/include/cutils/list.h b/libcutils/include/cutils/list.h index dfdc53be6..7eb872547 100644 --- a/libcutils/include/cutils/list.h +++ b/libcutils/include/cutils/list.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2008-2013 The Android Open Source Project + * Copyright (C) 2008 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -14,8 +14,7 @@ * limitations under the License. */ -#ifndef _CUTILS_LIST_H_ -#define _CUTILS_LIST_H_ +#pragma once #include @@ -38,9 +37,6 @@ struct listnode .prev = &(name), \ } -#define list_for_each(node, list) \ - for ((node) = (list)->next; (node) != (list); (node) = (node)->next) - #define list_for_each_reverse(node, list) \ for ((node) = (list)->prev; (node) != (list); (node) = (node)->prev) @@ -49,6 +45,10 @@ struct listnode (node) != (list); \ (node) = (n), (n) = (node)->next) +#define list_for_each(node, list) \ + for (struct listnode* __n = ((node) = (list)->next)->next; (node) != (list); \ + (node) = __n, __n = (node)->next) + static inline void list_init(struct listnode *node) { node->next = node; @@ -84,5 +84,3 @@ static inline void list_remove(struct listnode *item) #ifdef __cplusplus }; #endif /* __cplusplus */ - -#endif