[PATCH] nvmem: core: Fix memleak in nvmem_add_cells_from_dt error path

From: Ruihai Zhou
Date: Wed Sep 11 2024 - 08:14:19 EST


In the nvmem_add_cells_from_dt(), if the devicetree 'bits' property
is outside of the valid range, the info.name alloc by kasprintf will
cause memleak. Just free before return from nvmem_add_cells_from_dt
in the error path.

Fixes: def3173d4f17 ("nvmem: core: Print error on wrong bits DT property")
Signed-off-by: Ruihai Zhou <zhou.ruihai@xxxxxx>
---
drivers/nvmem/core.c | 1 +
1 file changed, 1 insertion(+)

diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c
index 516dfd861b9f..ce71bee5e1a9 100644
--- a/drivers/nvmem/core.c
+++ b/drivers/nvmem/core.c
@@ -826,6 +826,7 @@ static int nvmem_add_cells_from_dt(struct nvmem_device *nvmem, struct device_nod
info.nbits = be32_to_cpup(addr);
if (info.bit_offset >= BITS_PER_BYTE || info.nbits < 1) {
dev_err(dev, "nvmem: invalid bits on %pOF\n", child);
+ kfree(info.name);
of_node_put(child);
return -EINVAL;
}
--
2.31.1