qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2] semihosting: fix memleak at semihosting_arg_fallback
@ 2023-10-20 11:33 Matheus Tavares Bernardino
  2023-10-20 12:55 ` Alex Bennée
  0 siblings, 1 reply; 2+ messages in thread
From: Matheus Tavares Bernardino @ 2023-10-20 11:33 UTC (permalink / raw)
  To: qemu-devel; +Cc: bcain, alex.bennee, qemu-trivial, Philippe Mathieu-Daudé

We duplicate "cmd" as strtok may modify its argument, but we forgot
to free it later. Furthermore, add_semihosting_arg doesn't take
responsibility for this memory either (it strdup's the argument).

Signed-off-by: Matheus Tavares Bernardino <quic_mathbern@quicinc.com>
Reviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org>
---
 semihosting/config.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/semihosting/config.c b/semihosting/config.c
index 249a377ae8..56283b5c3c 100644
--- a/semihosting/config.c
+++ b/semihosting/config.c
@@ -113,12 +113,13 @@ static int add_semihosting_arg(void *opaque,
 void semihosting_arg_fallback(const char *file, const char *cmd)
 {
     char *cmd_token;
+    g_autofree char *cmd_dup = g_strdup(cmd);
 
     /* argv[0] */
     add_semihosting_arg(&semihosting, "arg", file, NULL);
 
     /* split -append and initialize argv[1..n] */
-    cmd_token = strtok(g_strdup(cmd), " ");
+    cmd_token = strtok(cmd_dup, " ");
     while (cmd_token) {
         add_semihosting_arg(&semihosting, "arg", cmd_token, NULL);
         cmd_token = strtok(NULL, " ");
-- 
2.37.2



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

* Re: [PATCH v2] semihosting: fix memleak at semihosting_arg_fallback
  2023-10-20 11:33 [PATCH v2] semihosting: fix memleak at semihosting_arg_fallback Matheus Tavares Bernardino
@ 2023-10-20 12:55 ` Alex Bennée
  0 siblings, 0 replies; 2+ messages in thread
From: Alex Bennée @ 2023-10-20 12:55 UTC (permalink / raw)
  To: Matheus Tavares Bernardino
  Cc: qemu-devel, bcain, qemu-trivial, Philippe Mathieu-Daudé


Matheus Tavares Bernardino <quic_mathbern@quicinc.com> writes:

> We duplicate "cmd" as strtok may modify its argument, but we forgot
> to free it later. Furthermore, add_semihosting_arg doesn't take
> responsibility for this memory either (it strdup's the argument).
>
> Signed-off-by: Matheus Tavares Bernardino <quic_mathbern@quicinc.com>
> Reviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org>

Queued to semihosting/next, thanks.

-- 
Alex Bennée
Virtualisation Tech Lead @ Linaro


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

end of thread, other threads:[~2023-10-20 12:56 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-10-20 11:33 [PATCH v2] semihosting: fix memleak at semihosting_arg_fallback Matheus Tavares Bernardino
2023-10-20 12:55 ` Alex Bennée

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).