devicetree.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] of: dynamic: Fix potential memory leak in of_changeset_action()
@ 2023-09-08  7:03 Dan Carpenter
  2023-09-08  7:16 ` Geert Uytterhoeven
                   ` (2 more replies)
  0 siblings, 3 replies; 10+ messages in thread
From: Dan Carpenter @ 2023-09-08  7:03 UTC (permalink / raw)
  To: Rob Herring
  Cc: Rob Herring, Frank Rowand, Geert Uytterhoeven, devicetree,
	linux-kernel, kernel-janitors

Smatch complains that the error path where "action" is invalid leaks
the "ce" allocation:
    drivers/of/dynamic.c:935 of_changeset_action()
    warn: possible memory leak of 'ce'

Fix this by doing the validation before the allocation.

Fixes: 914d9d831e61 ("of: dynamic: Refactor action prints to not use "%pOF" inside devtree_lock")
Reported-by: kernel test robot <lkp@intel.com>
Closes: https://lore.kernel.org/r/202309011059.EOdr4im9-lkp@intel.com/
Signed-off-by: Dan Carpenter <dan.carpenter@linaro.org>
---
 drivers/of/dynamic.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/drivers/of/dynamic.c b/drivers/of/dynamic.c
index 0a3483e247a8..f63250c650ca 100644
--- a/drivers/of/dynamic.c
+++ b/drivers/of/dynamic.c
@@ -890,13 +890,13 @@ int of_changeset_action(struct of_changeset *ocs, unsigned long action,
 {
 	struct of_changeset_entry *ce;
 
+	if (WARN_ON(action >= ARRAY_SIZE(action_names)))
+		return -EINVAL;
+
 	ce = kzalloc(sizeof(*ce), GFP_KERNEL);
 	if (!ce)
 		return -ENOMEM;
 
-	if (WARN_ON(action >= ARRAY_SIZE(action_names)))
-		return -EINVAL;
-
 	/* get a reference to the node */
 	ce->action = action;
 	ce->np = of_node_get(np);
-- 
2.39.2


^ permalink raw reply related	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2023-09-13 12:44 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-09-08  7:03 [PATCH] of: dynamic: Fix potential memory leak in of_changeset_action() Dan Carpenter
2023-09-08  7:16 ` Geert Uytterhoeven
2023-09-08 15:18 ` Rob Herring
2023-09-08 15:34   ` Geert Uytterhoeven
2023-09-08 16:14     ` Dan Carpenter
2023-09-12 15:32       ` Rob Herring
2023-09-12 15:55         ` Geert Uytterhoeven
2023-09-13  6:28         ` Dan Carpenter
2023-09-13 12:44           ` Rob Herring
2023-09-11 14:58 ` Rob Herring

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).