The current code fails to nvmem_cell_drop(cells[0]) - even worse, ifI agree, it would fail in case of zero.
the loop above fails already at i==0, we'll enter an essentially
infinite loop doing nvmem_cell_drop on cells[-1], cells[-2], ... which
is unlikely to end well.
No, this will not work.
Also, we're not freeing the temporary backing array cells on the error
path.
Signed-off-by: Rasmus Villemoes <linux@xxxxxxxxxxxxxxxxxx>
---
drivers/nvmem/core.c | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c
index 6fd4e5a5ef4a..1e65eccfea83 100644
--- a/drivers/nvmem/core.c
+++ b/drivers/nvmem/core.c
@@ -288,9 +288,11 @@ static int nvmem_add_cells(struct nvmem_device *nvmem,
return 0;
err:
- while (--i)
+ while (i--)
nvmem_cell_drop(cells[i]);
This change looks good.
+ kfree(cells);
+
return rval;
}