From 472dd53a091e0e4b0d2db8fcd10da3f408873633 Mon Sep 17 00:00:00 2001
From: Zhiqiang Liu <liuzhiqiang26@huawei.com>
Date: Sun, 30 Jan 2022 11:37:51 +0800
Subject: [PATCH] dev_name() determine whether the dev->aliases linked list is
 empty before obtaining the dev name

If dev->aliases linked list is empty, then directly obtain the str
address of dm_str_list saved in dev_aliases.n, an unknown address
will be returned, which may cause segfault.

So we need to judge whether the dev->aliases linked list is empty before
getting it to avoid returning unknown address.

Signed-off-by: Zhiqiang Liu <liuzhiqiang26@huawei.com>
---
 lib/device/dev-cache.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/lib/device/dev-cache.c b/lib/device/dev-cache.c
index 33b75a9..1263820 100644
--- a/lib/device/dev-cache.c
+++ b/lib/device/dev-cache.c
@@ -1703,8 +1703,8 @@ int dev_fd(struct device *dev)
 
 const char *dev_name(const struct device *dev)
 {
-	return (dev && dev->aliases.n) ? dm_list_item(dev->aliases.n, struct dm_str_list)->str :
-	    unknown_device_name();
+	return (dev && dev->aliases.n && !dm_list_empty(&dev->aliases)) ?\
+		dm_list_item(dev->aliases.n, struct dm_str_list)->str : unknown_device_name();
 }
 
 bool dev_cache_has_md_with_end_superblock(struct dev_types *dt)
-- 
1.8.3.1

