[PATCH 4/4] dm: core: free old name in device_set_name to prevent leak

Peng Fan (OSS) peng.fan at oss.nxp.com
Fri Jul 3 11:11:27 CEST 2026


From: Peng Fan <peng.fan at nxp.com>

If device_set_name is called on a device that already has
DM_FLAG_NAME_ALLOCED set, the old dynamically-allocated name is leaked.
Free it before assigning the new name.

See: drivers/net/mdio_gpio.c:mdio_gpio_bind(). There is
device_set_name() here, however dm_mdio_post_bind() will also call
device_set_name() if "device-name" exists.

Signed-off-by: Peng Fan <peng.fan at nxp.com>
---
 drivers/core/device.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/drivers/core/device.c b/drivers/core/device.c
index d365204ba11..6024534ff93 100644
--- a/drivers/core/device.c
+++ b/drivers/core/device.c
@@ -1147,6 +1147,8 @@ int device_set_name(struct udevice *dev, const char *name)
 	name = strdup(name);
 	if (!name)
 		return -ENOMEM;
+	if (dev_get_flags(dev) & DM_FLAG_NAME_ALLOCED)
+		free((char *)dev->name);
 	dev->name = name;
 	device_set_name_alloced(dev);
 

-- 
2.51.0



More information about the U-Boot mailing list