]> git.ipfire.org Git - thirdparty/u-boot.git/commitdiff
dm: core: free old name in device_set_name to prevent leak
authorPeng Fan <peng.fan@nxp.com>
Fri, 3 Jul 2026 09:11:27 +0000 (17:11 +0800)
committerTom Rini <trini@konsulko.com>
Thu, 16 Jul 2026 18:03:05 +0000 (12:03 -0600)
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@nxp.com>
Reviewed-by: Simon Glass <sjg@chromium.org>
drivers/core/device.c

index d365204ba1101ca7bff3b10bf6b6ede375c0d287..6024534ff9395f5df5f99a27e4a50e736ec69ab5 100644 (file)
@@ -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);