[PATCH 3/3] ARM: mmp: remove check of list iterator against head past the loop body

From: Jakob Koschel
Date: Thu Mar 31 2022 - 17:49:56 EST


When list_for_each_entry() completes the iteration over the whole list
without breaking the loop, the iterator value will be a bogus pointer
computed based on the head element.

While it is safe to use the pointer to determine if it was computed
based on the head element, either with list_entry_is_head() or
&pos->member == head, using the iterator variable after the loop should
be avoided.

In preparation to limit the scope of a list iterator to the list
traversal loop, use a dedicated pointer to point to the found element [1].

Link: https://lore.kernel.org/all/CAHk-=wgRr_D8CB-D9Kg-c=EHreAsk5SqXPwr9Y7k9sA6cWXJ6w@xxxxxxxxxxxxxx/ [1]
Signed-off-by: Jakob Koschel <jakobkoschel@xxxxxxxxx>
---
arch/arm/mach-mmp/sram.c | 9 ++++++---
1 file changed, 6 insertions(+), 3 deletions(-)

diff --git a/arch/arm/mach-mmp/sram.c b/arch/arm/mach-mmp/sram.c
index ecc46c31004f..f0c175903c41 100644
--- a/arch/arm/mach-mmp/sram.c
+++ b/arch/arm/mach-mmp/sram.c
@@ -39,19 +39,22 @@ static LIST_HEAD(sram_bank_list);
struct gen_pool *sram_get_gpool(char *pool_name)
{
struct sram_bank_info *info = NULL;
+ struct sram_bank_info *iter;

if (!pool_name)
return NULL;

mutex_lock(&sram_lock);

- list_for_each_entry(info, &sram_bank_list, node)
- if (!strcmp(pool_name, info->pool_name))
+ list_for_each_entry(iter, &sram_bank_list, node)
+ if (!strcmp(pool_name, iter->pool_name)) {
+ info = iter;
break;
+ }

mutex_unlock(&sram_lock);

- if (&info->node == &sram_bank_list)
+ if (!info)
return NULL;

return info->gpool;
--
2.25.1