From: ant.v.moryakov@gmail.com
To: u-boot@lists.denx.de
Cc: trini@konsulko.com, Anton Moryakov <ant.v.moryakov@gmail.com>
Subject: [PATCH] boot: fix memory leak in bootmeth-uclass.c
Date: Fri, 16 May 2025 14:54:29 +0300 [thread overview]
Message-ID: <20250516115429.8889-1-ant.v.moryakov@gmail.com> (raw)
From: Anton Moryakov <ant.v.moryakov@gmail.com>
The static analyzer (Svace) reported a memory leak in bootmeth_setup_iter_order():
Dynamic memory referenced by 'order' was allocated by calloc() at bootmeth-uclass.c:113
but could be lost when returning error codes.
This fix:
1. Adds proper error handling with goto/cleanup pattern
2. Frees allocated 'order' before returning error codes
3. Maintains all existing functionality
Identified issues fixed:
- Memory leak on !include_global (-EPERM)
- Memory leak on empty method list (-ENOENT)
- Memory leak on allocation failure (-ENOMEM)
Signed-off-by: Anton Moryakov <ant.v.moryakov@gmail.com>
---
boot/bootmeth-uclass.c | 17 ++++++++++++-----
1 file changed, 12 insertions(+), 5 deletions(-)
diff --git a/boot/bootmeth-uclass.c b/boot/bootmeth-uclass.c
index 014b7588e8d..b3870ba5364 100644
--- a/boot/bootmeth-uclass.c
+++ b/boot/bootmeth-uclass.c
@@ -135,8 +135,10 @@ int bootmeth_setup_iter_order(struct bootflow_iter *iter, bool include_global)
* We don't support skipping global bootmeths. Instead, the user
* should omit them from the ordering
*/
- if (!include_global)
- return log_msg_ret("glob", -EPERM);
+ if (!include_global) {
+ ret = log_msg_ret("glob", -EPERM);
+ goto err;
+ }
memcpy(order, std->bootmeth_order,
count * sizeof(struct bootmeth *));
@@ -190,9 +192,10 @@ int bootmeth_setup_iter_order(struct bootflow_iter *iter, bool include_global)
}
count = upto;
}
- if (!count)
- return log_msg_ret("count2", -ENOENT);
-
+ if (!count) {
+ ret = log_msg_ret("count2", -ENOENT);
+ goto err;
+ }
if (IS_ENABLED(CONFIG_BOOTMETH_GLOBAL) && include_global &&
iter->first_glob_method != -1 && iter->first_glob_method != count) {
iter->cur_method = iter->first_glob_method;
@@ -202,6 +205,10 @@ int bootmeth_setup_iter_order(struct bootflow_iter *iter, bool include_global)
iter->num_methods = count;
return 0;
+
+err:
+ free(order);
+ return ret;
}
int bootmeth_set_order(const char *order_str)
--
2.30.2
reply other threads:[~2025-05-16 11:54 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20250516115429.8889-1-ant.v.moryakov@gmail.com \
--to=ant.v.moryakov@gmail.com \
--cc=trini@konsulko.com \
--cc=u-boot@lists.denx.de \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.