Skip to contentSkip to conversation

dm: core: misc fixes

PATCH In review Peng Fan (OSS) 4 participants 13 messages Jul 3 – Jul 16

lore
Share & tools
lore
#0 lore raw

cover dm: core: misc fixes

A few misc fixes to dm core. More information could be found in each commit. Patch list:
dm: core: free old name in device_set_name to prevent leak
dm: core: Correct calloc arguments
dm: core: fix wrong strlen check in of_add_subnode
dm: core: free fdt when fdt_create_empty_tree failure

---
Peng Fan (4):
dm: core: free fdt when fdt_create_empty_tree failure
dm: core: fix wrong strlen check in of_add_subnode
dm: core: Correct calloc arguments
dm: core: free old name in device_set_name to prevent leak

 drivers/core/device.c    | 2 ++
 drivers/core/of_access.c | 2 +-
 drivers/core/ofnode.c    | 4 +++-
 drivers/core/tag.c       | 4 ++--
 4 files changed, 8 insertions(+), 4 deletions(-)

---
base-commit: e800cc67f5b6cb50a20f37c993ec1cd4063bdbd3
change-id: 20260702-dm-core-e88b660f625e

Best regards,

Signature · 1 line
Peng Fan <[email protected]>
#1 lore raw

PATCH 1/4 dm: core: free fdt when fdt_create_empty_tree failure

From: Peng Fan <[email protected]>

If fdt_create_empty_tree fails, the malloc-ed fdt buffer is never freed. Add free before the error return.

---
 drivers/core/ofnode.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/core/ofnode.c +3 -1
index 1388b481031..b5d13c43db1 100644
@@ -130,8 +130,10 @@ int oftree_new(oftree *treep)
 		if (!fdt)
 			return log_msg_ret("fla", -ENOMEM);
 		ret = fdt_create_empty_tree(fdt, size);
-		if (ret)
+		if (ret) {
+			free(fdt);
 			return log_msg_ret("fla", -EINVAL);
+		}
 		oftree_list[oftree_count++] = fdt;
 		tree.fdt = fdt;
 	}
Signature · 1 line
2.51.0
Reviewed-by on v1 1/4
#3 lore raw

Re: [PATCH 1/4] dm: core: free fdt when fdt_create_empty_tree failure

In reply to Peng Fan (OSS)
Quoted 10 lines · Peng Fan

dm: core: free fdt when fdt_create_empty_tree failure

If fdt_create_empty_tree fails, the malloc-ed fdt buffer is never freed. Add free before the error return.

drivers/core/ofnode.c | 4 +++-

 1 file changed, 3 insertions(+), 1 deletion(-)
#4 lore raw

PATCH 2/4 dm: core: fix wrong strlen check in of_add_subnode

In reply to Peng Fan (OSS)

From: Peng Fan <[email protected]>

The duplicate-name check uses strlen on the search name instead of the child name, so a child named "trevor" would falsely match a search for "trev". Fix by checking strlen of child->name.

---
 drivers/core/of_access.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/core/of_access.c +1 -1
index 969492aae37..f910cc9a31b 100644
@@ -1071,7 +1071,7 @@ int of_add_subnode(struct device_node *parent, const char *name, int len,
 		 * make sure we don't use a child called "trevor" when we are
 		 * searching for "trev".
 		 */
-		if (!strncmp(child->name, name, len) && strlen(name) == len) {
+		if (!strncmp(child->name, name, len) && strlen(child->name) == len) {
 			*childp = child;
 			return -EEXIST;
 		}
Signature · 1 line
2.51.0
Reviewed-by on v1 2/4
#5 lore raw

Re: [PATCH 2/4] dm: core: fix wrong strlen check in of_add_subnode

Quoted 4 lines · Peng Fan (OSS)

From: Peng Fan <[email protected]>The duplicate-name check uses strlen on the search name instead of the child name, so a child named "trevor" would falsely match a search for "trev". Fix by checking strlen of child->name.

Hi,

Best regards,
Raphaël

Reviewed-by on v1 2/4
#6 lore raw

Re: [PATCH 2/4] dm: core: fix wrong strlen check in of_add_subnode

In reply to Peng Fan (OSS)
Quoted 11 lines · Peng Fan

dm: core: fix wrong strlen check in of_add_subnode

The duplicate-name check uses strlen on the search name instead of the child name, so a child named 'trevor' would falsely match a search for 'trev'. Fix by checking strlen of child->name.

drivers/core/of_access.c | 2 +-

 1 file changed, 1 insertion(+), 1 deletion(-)
#7 lore raw

PATCH 3/4 dm: core: Correct calloc arguments

In reply to Peng Fan (OSS)

From: Peng Fan <[email protected]>

Every other calloc in drivers/core uses calloc(count, size) with count first. Fix the two reversed calloc(sizeof, 1) calls for consistency.

---
 drivers/core/tag.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/core/tag.c +2 -2
index a3c5cb7e57c..78b5ee6dc07 100644
@@ -46,7 +46,7 @@ int dev_tag_set_ptr(struct udevice *dev, enum dm_tag_t tag, void *ptr)
 			return -EEXIST;
 	}
 
-	node = calloc(sizeof(*node), 1);
+	node = calloc(1, sizeof(*node));
 	if (!node)
 		return -ENOMEM;
 
@@ -70,7 +70,7 @@ int dev_tag_set_val(struct udevice *dev, enum dm_tag_t tag, ulong val)
 			return -EEXIST;
 	}
 
-	node = calloc(sizeof(*node), 1);
+	node = calloc(1, sizeof(*node));
 	if (!node)
 		return -ENOMEM;
 
Signature · 1 line
2.51.0
Reviewed-by on v1 3/4
#9 lore raw

Re: [PATCH 3/4] dm: core: Correct calloc arguments

In reply to Peng Fan (OSS)
Quoted 10 lines · Peng Fan

dm: core: Correct calloc arguments

Every other calloc in drivers/core uses calloc(count, size) with count first. Fix the two reversed calloc(sizeof, 1) calls for consistency.

drivers/core/tag.c | 4 ++--

 1 file changed, 2 insertions(+), 2 deletions(-)
#10 lore raw

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

In reply to Peng Fan (OSS)

From: Peng Fan <[email protected]>

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.

---
 drivers/core/device.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/core/device.c +2 -0
index d365204ba11..6024534ff93 100644
@@ -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);
 
Signature · 1 line
2.51.0
Reviewed-by on v1 4/4
#11 lore raw

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

Quoted 6 lines · Peng Fan

dm: core: free old name in device_set_name to prevent leak

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.

Quoted 8 lines

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.

drivers/core/device.c | 2 ++

 1 file changed, 2 insertions(+)
#12 lore raw
In reply to Peng Fan (OSS)
Quoted 8 lines · Peng Fan (OSS)

A few misc fixes to dm core. More information could be found in each commit. Patch list:
dm: core: free old name in device_set_name to prevent leak
dm: core: Correct calloc arguments
dm: core: fix wrong strlen check in of_add_subnode
dm: core: free fdt when fdt_create_empty_tree failure

[...]

Applied to u-boot/main, thanks!

[1/4] dm: core: free fdt when fdt_create_empty_tree failure

      commit: e871bb2463fbd6c06b6d771d9201c6d6f4abf947
[2/4] dm: core: fix wrong strlen check in of_add_subnode
      commit: 65e1e66a3a38e21128582bec3b93c35548e60687
[3/4] dm: core: Correct calloc arguments
      commit: 0af2ed57a3e9c34775602ea70d5c9829c4e830c6
[4/4] dm: core: free old name in device_set_name to prevent leak
      commit: e1f345abf4c0df94ef6c5eca400a440c16b47d81
Signature · 1 line
Tom