[PATCH 3/4] driver: fix device name clashes

Sascha Hauer s.hauer at pengutronix.de
Mon Dec 8 04:00:53 PST 2025


It can happen that we end up with two devices having the same name.
This happens when one device is registered with

dev->name "foo0";
dev->id = DEVICE_ID_SINGLE;

and another one with

dev->name "foo";
dev->id = 0;

Fix this by not comparing both dev->name and dev->id when testing if a
device already exists, but instead by comparing the resulting device
name.

Signed-off-by: Sascha Hauer <s.hauer at pengutronix.de>
---
 drivers/base/driver.c | 15 ++++++++++-----
 1 file changed, 10 insertions(+), 5 deletions(-)

diff --git a/drivers/base/driver.c b/drivers/base/driver.c
index 1a5a3598be5d4b2c6ce8558b1ac8c3cba4d59485..c417e945ee0308028fc885101de18bd4a2adfe93 100644
--- a/drivers/base/driver.c
+++ b/drivers/base/driver.c
@@ -86,13 +86,18 @@ struct device *get_device_by_name(const char *name)
 static struct device *get_device_by_name_id(const char *name, int id)
 {
 	struct device *dev;
+	char *str = NULL;
 
-	for_each_device(dev) {
-		if(!strcmp(dev->name, name) && id == dev->id)
-			return dev;
-	}
+	if (id == DEVICE_ID_SINGLE)
+		return get_device_by_name(name);
 
-	return NULL;
+	str = basprintf("%s%u", name, id);
+
+	dev = get_device_by_name(str);
+
+	free(str);
+
+	return dev;
 }
 
 int get_free_deviceid_from(const char *name_template, int id_from)

-- 
2.47.3




More information about the barebox mailing list